exporting sql results having trouble with permissions - mysql

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

Related

MySQL to CSV Pythonanywhere

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.

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

SQL syntax error near gunzip when restoring a database using .sql.gz file

I am trying to restore a mysql db using a .sql.gz file. I am using mySql console to run a command because file size is too large for phpMyAdmin. Command I am using is
gunzip C:/Vik/Gya/Source/beed_2013-04-06.sql.gz | mysql -u root -p bd
where root is the user id. There is no password for root. bd is the database to which I am trying to import. mysql is running on my local machine (Windows 8). I have a wamp setup.
This is the error I am getting:
ERROR 1064 (42000): You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near 'gunzip
C:/Vikalp/Gyankosh/Source/beedictionary_2013-04-06.sql | mysql -u root
-p' at line 1.
You need -c option (output to stdout)
gunzip -c xxx.sql.gz |mysql -u root -p
While Kisoft´s answer is the correct one, I just wanted to point out that you don´t need the -c, it works just fine as it is.
this command will unzip the database dump and import it into the database at the same time.
gunzip < output.sql.gz | mysql -u <username> -p<password> <database>
If you type gunzip and you get a SQL syntax error that complaints about gunzip, you are already logged into the mysql console. The mysql console is not a general purpose shell!
You are using Windows and I suspect you haven't installed gzip in your computer (it isn't a builtin utility). It's a classical Unix tool but you can find binaries for Windows. Install it and run your original command with a couple of tweaks:
Make sure you're in Windows prompt (C:\>)
Redirect gunzip result to stdout rather than a file:
gunzip --stdout C:/Vik/Gya/Source/beed_2013-04-06.sql.gz | mysql -u root -p bd
Alternatively, you can run the dump from within MySQL promt (mysql>) if you uncompress it first (you don't need specifically command-line gzip, most GUI archivers such as 7-Zip support this format):
mysql> \. C:/Vikalp/Gyankosh/Source/beedictionary_2013-04-06.sql
you do not need to gunzip
just:
zcat myfile.gz | mysql -uuser -ppassword mydatabase
it is faster this way
Your answer is already here
phpMyAdmin: Can't import huge database file, any suggestions?
Under php.ini file, normally located in c:\xampp\php or wampp whatever you called
post_max_size=128M
upload_max_filesize=128M
Changing value there will get you what you want.Good luck
Dont forget to restart , apache and mysql .
Try this following steps to restore db using .gz files:
1. Run command : gunzip C:/Vik/Gya/Source/beed_2013-04-06.sql.gz
This will uncompress the .gz file and will just store beed_2013-04-06.sql in the same location.
2. Type the following command to import sql data file:
mysql -u username -p bd < C:/Vik/Gya/Source/beed_2013-04-06.sql

importing table using mysql console on localhosted WAMP

this is what I use in MySQL console (WAMP) (no password, as it is local)
mysql> mysql -u root -p dbase < table.sql
or this
mysql> mysql -u root -p dbase < C:\path\to\table.sql
this is what I get:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'mysql
-u root -p dbase < table.sql' at line 1
what may cause this problem?
The command you're using is meant to be run outside of the mysql shell. If you just exit out of the mysql shell and enter the same command, it should work.
OK - step by step - since, if someone is not used to it, any omission may get that "someone" lost.
remote nix server:
connect to mysql cli (command line interface) using e.g. Putty - type in: mysql -u user -pyourpassword
now you are connected to MySQL server and you can use e.g. LOAD INFILE command etc.
"exit" gets you back to "-bash" so you can use, e.g. database importing - just type: mysql -u root -p hmvc < table.sql
local WAMP - (setup: WAMP2.2 - Apache2.2.21 - MySQL5.5.20)
case I: using Windows "command prompt"
change directory to the one, where your mysql resides within WAMP root, e.g.:
C:\wamp\bin\mysql\mysql5.5.20\bin
connect to mysql cli:
mysql -u user -pyourpassword
now you are in, so you can use e.g. LOAD INFILE command etc.
"exit" gets you back to "C:\wamp\bin\mysql\mysql5.5.20\bin>" so you can use, e.g. database importing:
mysql -u root -p hmvc < table.sql
etc.
case 2: using mysql cli directly
WAMP offers a link to it in its tray icon command collection
link is called: MySQL console
it calls mysql server diretly by invoking C:\wamp\bin\mysql\mysql5.5.20\bin\mysql.exe
you can use all MySQL commands right away, like e.g. LOAD INFILE
you cannot import/dump database using this console
if you try to exit, MySQL console will do just that - it will exit (window gets closed)
I hope it will you some time.

Import SQL file into mysql

I have a database called nitm. I haven't created any tables there. But I have a SQL file which contains all the necessary data for the database. The file is nitm.sql which is in C:\ drive. This file has size of about 103 MB. I am using wamp server.
I have used the following syntax in MySQL console to import the file:
mysql>c:/nitm.sql;
But this didn't work.
From the mysql console:
mysql> use DATABASE_NAME;
mysql> source path/to/file.sql;
make sure there is no slash before path if you are referring to a relative path... it took me a while to realize that! lol
Finally, i solved the problem. I placed the `nitm.sql` file in `bin` file of the `mysql` folder and used the following syntax.
C:\wamp\bin\mysql\mysql5.0.51b\bin>mysql -u root nitm < nitm.sql
And this worked.
If you are using wamp you can try this. Just type use your_Database_name first.
Click your wamp server icon then look for MYSQL > MSQL Console then run it.
If you dont have password, just hit enter and type :
mysql> use database_name;
mysql> source location_of_your_file;
If you have password, you will promt to enter a password. Enter you password first then type:
mysql> use database_name;
mysql> source location_of_your_file;
location_of_your_file should look like C:\mydb.sql
so the commend is mysql>source C:\mydb.sql;
This kind of importing sql dump is very helpful for BIG SQL FILE.
I copied my file mydb.sq to directory C: .It should be capital C: in order to run
and that's it.
In windows, if the above suggestion gives you an error (file not found or unknown db) you may want to double the forward slashes:
In the mysql console:
mysql> use DATABASE_NAME;
mysql> source C://path//to//file.sql;
Ok so, I'm using Linux but I think this holds true for Windows too.
You can do this either directly from the command prompt
> mysql -u <user name> -p<password> <database name> < sqlfilename.sql
Or from within the mysql prompt, you can use:
mysql>source sqlfilename.sql
But both these approaches have their own benefits in the results they display.
In the first approach, the script exits as soon as it encounters an error. And the better part, is that it tells you the exact line number in the source file where the error occurred. However, it ONLY displays errors. If it didn't encounter any errors, the scripts displays NOTHING. Which can be a little unnerving. Because you're most often running a script with a whole pile of commands.
Now second approach (from within the mysql prompt) has the benefit that it displays a message for every different MySQL command in the script. If it encounters errors, it displays the mysql error message but continues on through the scripts. This can be good, because you can then go back and fix all the errors before you run the script again. The downside is that it does NOT display the line numbers in the script where the errors were encountered. This can be a bit of a pain. But the error messages are as descriptive so you could probably figure out where the problem is.
I, for one, prefer the directly-from-OS-command line approach.
If you are using xampp
C:\xampp\mysql\bin\mysql -uroot -p nitm < nitm.sql
You are almost there
use
mysql> \. c:/nitm.sql;
You may also access help by
mysql> \?
For localhost on XAMPP. Open a cmd window and type
cd C:\xampp\mysql\bin
mysql.exe -u root -p
Attention! No semi-colon after -p
Enter your password and type
use database_name;
to select the database you need.
Check if your table is there
show tables;
Import from your sql file
source sqlfile.sql;
I have put my file on C:\xampp\mysql\bin location in order to don't mix up with locations of sql file.
Try:
mysql -u username -p database_name < file.sql
Check MySQL Options.
Note: It is better to use the full path of the SQL file file.sql.
Don't forget to use
charset utf8
If your sql file is in utf-8 :)
So you need to do:
cmd.exe
mysql -u root
mysql> charset utf8
mysql> use mydbname
mysql> source C:\myfolder\myfile.sql
Good luck ))
In Linux I navigated to the directory containing the .sql file before starting mysql. The system cursor is now in the same location as the file and you won't need a path. Use source myData.sql where my date is replaced with the name of your file.
cd whatever directory
mysql - p
connect targetDB
source myData.sql
Done
from the command line (cmd.exe, not from within mysql shell) try something like:
type c:/nite.sql | mysql -uuser -ppassword dbname
Does your dump contain features that are not supported in your version of MySQL? You can also try to remove the starting (and ending) MySQL commented SET-statements.
I don't know if your dump comes from a Linux version of MySQL (line endings)?
I have installed my wamp server in D: drive so u have to go to the following path from ur command line->(and if u have installed ur wamp in c: drive then just replace the d: wtih c: here)
D:\>cd wamp
D:\wamp>cd bin
D:\wamp\bin>cd mysql
D:\wamp\bin\mysql>cd mysql5.5.8 (whatever ur verserion will be displayed here use keyboard Tab button)
D:\wamp\bin\mysql\mysql5.5.8>cd bin
D:\wamp\bin\mysql\mysql5.5.8\bin>mysql -u root -p password db_name < "d:\backupfile.sql"
here root is user of my phpmyadmin
password is the password for phpmyadmin so if u haven't set any password for root just nothing type at that place,
db_name is the database (for which database u r taking the backup)
,backupfile.sql is the file from which u want ur backup of ur database and u can also change the backup file location(d:\backupfile.sql) from to any other place on your computer
mysql>c:/nitm.sql;
That would write the output of the mysql command to 'nitm.sql;' (What's the ';' supposed to do?) Assuming you've got a copy of the original file (before you overwrote it) then:
mysql < c:/nitm.sql
Export Particular DataBases
djimi:> mysqldump --user=root --host=localhost --port=3306 --password=test -B CCR KIT >ccr_kit_local.sql
this will export CCR and KIT databases...
Import All Exported DB to Particular Mysql Instance (You have to be where your dump file is)
djimi:> mysql --user=root --host=localhost --port=3306 --password=test < ccr_kit_local.sql
In Windows OS the following commands works for me.
mysql>Use <DatabaseName>
mysql>SOURCE C:/data/ScriptFile.sql;
No single quotes or double quotes around file name. Path would contain '/' instead of '\'.
For those of you struggling with getting this done trying every possible answer you can find on SO. Here's what worked for me on a VPS running Windows 2012 R2 :
Place your sql file wherever the bin is for me it is located at C:\Program Files\MySQL\MySQL Server 8.0\bin
Open windows command prompt (cmd)
Run C:\Program Files\MySQL\MySQL Server 8.0\bin > mysql -u [username] -p
Enter your password
Run command use [database_name];
Import your file with command source C://Program Files//MySQL//MySQL Server 8.0//bin//mydatabasename.sql
It did it for me as everything else had failed. It might help you too.