MySQL to CSV Pythonanywhere - mysql

I want to export a mysql database to CSV on pythonanywhere. I opened a mysql console and even tried "select * from ". It shows me the correct output. But when I do "select * from into outfile 'db.csv';" it shows: "Access denied for user ''#'%' (using password: YES)".

As per the MySQL documentation:
The SELECT ... INTO OUTFILE statement is intended to enable dumping a
table to a text file on the server host. To create the resulting file
on some other host, SELECT ... INTO OUTFILE normally is unsuitable
because there is no way to write a path to the file relative to the
server host file system, unless the location of the file on the remote
host can be accessed using a network-mapped path on the server host
file system.
Alternatively, if the MySQL client software is installed on the remote
host, you can use a client command such as mysql -e "SELECT ..." >
file_name to generate the file on that host.
So, to put that another way -- you're connecting to a MySQL server running on a different computer to the one where you're running the MySQL console, and then running a command that is trying to write a file on that other computer, where you don't have privileges to write a file (and would not be able to access it if you did).
Instead, you should start a Bash console, then run a command like this:
mysql -u yourusername -p -h yourdatabasehost -e "select * from yourtable" 'yourdatabasename' > db.csv
...replacing yourusername, yourdatabasehost, yourtable and yourdatabasename appropriately. The single quotes around yourdatabasename are important because on PythonAnywhere, the database name will be something like yourusername$something, and the dollar sign will be interpreted by the shell as an environment variable if it is not quoted.

Related

exporting sql results having trouble with permissions

I am having permission issues with mariadb when exporting sql result.
select * from referalList
INTO OUTFILE '/home/joe/testOutFileReferalList.csv';
SQL Error [1] [HY000]: (conn=63) Can't create/write to file '/home/joe/testOutFileReferalList.csv' (Errcode: 13 "Permission denied")
I am using DBeaver as front-end. But, I get the same result with cli however I sign on:
sudo mysql -u root -p
sudo mysql -u joe -p
mariadb -u joe -p
etc.
with or without sudo...
Then, without a path, the sql run puts the file into /var/lib/mysql/referals/ (referals is the name of my db).
select * from referalList
INTO OUTFILE 'testOutFileReferalList.csv';
It is created with permissions o mysql:mysql
I have to change the permissions on every SQL result I export. How can I get permissions to be me, i.e. joe:joe? And how can I export files to anywhere I like, such as my home directory ~ ?
Also, any way to get mariadb to overwrite a file if it already exists?
mariadb version is
Server version: 10.6.5-MariaDB-1:10.6.5+maria~focal mariadb.org binary distribution
I am on Linux Ubuntu 20.04.4
Thank you,
SELECT INTO outfile is usually used to output a table on the server to a text file. This is done directly on the server, which is why the user under whom the server is running must have appropriate access rights to the file.
The easiest way to output a table on the client side in a text file is to use the command line client:
$> mysql -ujoe -pjoe yourdb -e "SELECT * FROM referalList" > /home/joe/testOutFileReferalList.csv

Restore MySQL database from .mysql file

I have a backup.mysql file that I created using mysqldump.
How can I use that to restore the database? I opened the file in Sublime Text, and here is what its beginning looks like!
The dump you produced is a SQL script you can run with any tool you want. As OP from this question How do I restore a dump file from mysqldump?, if you try to restore it with MySQL Administrator or other software providing specific backup/restore functions, it can throw an error because it asks for a particular format (his own format); but if the dump has been produced correctly, you can run it with any MySQL client, for example with the command line tool :
mysql -h hostname -u username -p yourschema < ./path/to/the/script.mysql
Copy your backup.sql file to the working directory: C:\xampp\mysql\bin. (or whatever your path)
In your command prompt or terminal switch to the MySQL directory. cd c:\xampp\mysql\bin (or whatever your path)
Open the MySQL Database system mysql –h localhost –u root (or whatever your credentials)
You need to re-grant permissions: GRANT ALL ON your_db_here.* to 'root'#'localhost'; (or whatever your credentials)
You need to re-create the database: CREATE DATABASE your_db_here;
Start using the database: USE your_db_here;
Import the dump file that you created: source backup.mysql
Using DESCRIBE commands and "SELECT * FROM" commands ensure that your database has been restored properly.

How to connect to MYSQL database on server via Terminal on Macbook?

Is there a way to connect to my mysql database and do sth on tables via terminal?
Yes. In your terminal start the mysql prompt using
mysql --user=user_name --password=your_password db_name
Where db_name is the name of your database and user_name and password are your username and password.
You can then run SQL statements/queries from .sql files
mysql db_name < script.sql > output.tab
Where db_name is your database name, script.sql is a file containing your script, and output.tab (optional) is a file in which to dump the output of the query
You then simply place an SQL query in a file and run it.
If you get the error mysql: command not found, this is because the mysql executable cannot be found in your system PATH. If so, you need to run the following command to add the mySQL folder to the PATH, so that OS X knows to look there for the executable
export PATH=${PATH}:/usr/local/mysql/bin
Where /usr/local/mysql is the location of your mysql installation.
You can add this to your .bash_profile file (located at ~\.bash_profile, or you can create it) in order to have it run every time you start a new terminal. Otherwise you'll have to enter it manually before using the mysql command
Once you've entered this command (or added it to .bash_profile) you can use the mysql command as above
Alternately navigate to /usr/local/mysql/bin (or the location of your mysql install) and use the command
./mysql command
Instead of
mysql command
As above (where command is the command described in the first half of this post). This runs the mysql binary directly, rather than searching for it in the PATH

MySQL database load to server

In order to load databases to mysql using terminal, as shown here, the sql file must be already in the server? Or I can upload from my local pc?
If you have mysql installed in your local machine: then you can at once run the import command from local machine's command line
mysql -hmy_server -umy_user -p database_name < full/path/to/file.sql
or you can just go to the directory where your sql file exists and run
mysql -hmy_server -umy_user -p database_name < file.sql
(password will be required after hitting Enter). This way the file will be imported into the remote database from your local computer.
No mysql is installed in your local machine: in this case you have to manually upload the file into the server with some ftp client and then connect remotely(e.g. by Putty) and run similar command
mysql -umy_user -p database_name < path/to/sql/file.sql
Yes. You can SCP or SFTP it to the server, but that command expects a file on the machine. In fact, that particular command expects it to be in the same directory that you are in.
Assuming the file contains SQL statements it can go anywhere. What is important is that the MySQL client you use to read the file can access the database server - usually on port 3306 for MySQL.
On the server, unless your local directory is accessible via network to the server (i.e. network share on another machine which is mapped to some address on your server - convoluted but essentially possible).
Basically, just upload the file to the server and run the command locally there.
NO, the SQL file or SQL script file generally can be present in your machine. When you execute the command, said sql script gets executed against the database.
mysql -u user -p -h servername DB < [Full Path]/test.sql

Create MySQL Database with .SQL File

I don't know much about MySQL at all. But I am trying to reverse engineer a MySQL database using Visio. I know what steps I should take to do this, but I can't get my database to show in the 'Databases' section (as below):
How do I create the MySQL database using the .SQL file and get it to show up in this list? I have tried this code:
mysql -u username -p password database_name < filename.sql
using my own credentials of course. But that doesn't seem to work. In what folder should the .SQL file be placed if this statement is to work?
1) Create a file "filename.sql"
2) Create a database in your DB in which you want to import this file.
3) From command-prompt/terminal, move to the directory where you have created a "filename.sql".
4) Run the command: mysql -u username -p password database_name < filename.sql. (You can also give the proper path of your file and run this command from anywhere). It might be the case that you don't have a password set for MySQL. If so, mysql -u username database_name < filename.sql will also work.
In your case if you have created a database with name ojs and also created a file with name ojs.sql in C: drive then run the following command:
Edit: Put the path inside quotes.
mysql -u username -p password ojs < "C:\ojs.sql"
There is another way of importing tables in mysql. You can do it this way as well:
1) Connect your database
2) Type command "use ojs;"
3) Type command "source C:/ojs.sql"
Most MySQL SQL files that create databases create the database 'on-the-fly', so you typically needn't do anything except:
log-in
mysql -u [username] -p[password]
(Note: make sure you do NOT include a space (' ') character between the -p and the [password].
MySQL will think that [password] is the name of the database you want to connect to.
The 'general' log-in (above) does not assume you want to connect to any particular schema.)
source the file (do not use quotes around filename)
mysql> source [database_creation_file].sql
you can simply do it using mysql workbench
1> create a new query tab
2> CREATE DATABASE database_name;
3> USE database_name;
4> open the filename.sql file and execute it ctrl + shift + enter
5> all the tables in the filename.sql are created
To create a MySQL database using a SQL file, you can follow these steps:
Log in to your MySQL server using the mysql command-line tool and the appropriate credentials.
Use the CREATE DATABASE command to create a new database with the desired name:
CREATE DATABASE database_name;
Use the USE command to switch to the newly created database:
USE database_name;
Use the SOURCE command to import the SQL file into the database:
SOURCE path/to/sql/file;
The database will now be created and populated with the data from the SQL file. You can verify this by running some SQL queries against the database.
It's important to note that this process assumes that the SQL file contains valid SQL statements compatible with the version of MySQL you are using. If the SQL file contains any errors or unsupported statements, they will be displayed in the mysql command-line tool, and the import process will be interrupted.