I'm currently trying to execute a dump on mysql schema, but it keeps showing a message of sql syntax.
The command that I'm using is:
mysqldump -u root -p password "logicstore" > "c:\backup.sql";
Is that a punctuation issue or something like that?
PS: I've already tried different syntaxes. I've seen on others questions like these.
You can't set password in command line. And it is much more safe to use -r instead of pipe...
mysqldump -u root -p <database_name> -r <file_name>
If you want to use it without asking for password, you have to edit cardinals in .my.cnf file:
[mysqldump]
user=root
password=<password>
Related
I am trying to export a database using mysqldump from command line. I am using the following syntax:
mysqldump -u root -ppassword databasename > outputfile.sql
I've tried several variations on this, but I always end up with the following as the contents of the output file:
Usage: mysqldump [OPTIONS] database [tables]
OR mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]
OR mysqldump [OPTIONS] --all-databases [OPTIONS]
I can get mysqldump to export all of my databases if I exclude the database name, but it will not export just a single database.
Am I overlooking something here?
Troubleshooting from the comments above:
That is correct syntax. I'd guess that mysqldump is picking up some other options somewhere. Maybe it's a shell alias with an option like -A included in the alias definition? Try running \mysqldump ... to run it un-aliased.
Your reply:
#BillKarwin you were on the right track with -A. I tried mysqldump --print-defaults and apparently --all-databases is in the default arguments. I ran it with --no-defaults and it worked like a charm.
The problem is that --all-databases was configured as a default option. When you try using that option together with an argument specifying one database, it outputs the usage error you described.
http://dev.mysql.com/doc/refman/5.6/en/mysqldump.html says that all-databases can be either a command-line flag, or an option in the config file.
I'd suggest looking in your /etc/my.cnf or $HOME/.my.cnf for the all-databases option. It can appear either in the [mysqldump] group or the [client] group.
How about (without the space between u and root)
mysqldump -uroot -ppassword databasename > outputfile.sql
Putting your root password in a command line is a really bad idea. At the very least, create a .my.cnf in your home directory, setting permissions to 600 (rw for you only) containing:
[mysqldump]
user=root
password=yourpassword
This will allow you to perform that particular command without a password. Since there's no particular reason your root user needs to be doing the dump, why not just create a user that can do this?
Presumably, you're doing the mysqldump to back things up. To make life even easier on you, set it in cron as in the example below which executes at midnight. Because of the presence of the .my.cnf file containing the password, it doesn't need a password in the command
0 0 * * * /usr/bin/mysqldump -u root -h localhost databasename > /home/someuser/outputfile.sql 2>&1
i found this code but do not quite understand what the command is doing.
sudo -u test-user mysql -U test_traffic traffic < ./phoenix/data/sql/lib.model.schema.sql
i know the last part is using lib.model.schema.sql to create the tables and fields
the first part i dont quite understand: sudo -u test-user mysql -U test_traffic traffic
i know the command sudo and mysql
please explain?
thanks
Let's look at it bit by bit. Firstly the format
sudo -u username command
is an instruction to run command (which might be simple or complex) as the user username. So in your example, you are running the mysql command as the user test-user. You should note that this includes all the parameters to the mysql command - that's the entire rest of the line.
The command
mysql -U test_traffic traffic < ./phoenix/data/sql/lib.model.schema.sql
appears corrupt (certainly running it on 5.0.51a fails). It would make sense if the -U was a -u which would indicate that that the command was to be executed for mysql user test_traffic. If it was a -u you would then have an instruction to import the sql file into the traffic database.
So the combined instruction says, import the lib.model.schema.sql file into the database test_traffic using the mysql user test_traffic and executing the entire command as if you were logged-in as the user test-user.
Try Below steps for mysql:
mysql > -h hostname -u username -p password
mysql > use databasename;
mysql > source path/to/scriptfile
If you want to inject theschema.sql file into your database, with a shell script, simply use :
mysql -h [host] -u [username] -p[password] -D [database] < your_file
If you want to dynamicly tell which file should be loaded, replace your_file by $1 and pass the name of the file as an argument to your script.
Take care also to the -p option. There is no space between the -p and your password.
I want to execute a text file containing SQL queries, in MySQL.
I tried to run source /Desktop/test.sql and received the error:
mysql> . \home\sivakumar\Desktop\test.sql ERROR: Failed to open file
'\home\sivakumar\Desktop\test.sql', error: 2
Any idea on what I am doing wrong?
If you’re at the MySQL command line mysql> you have to declare the SQL file as source.
mysql> source \home\user\Desktop\test.sql;
You have quite a lot of options:
use the MySQL command line client: mysql -h hostname -u user database < path/to/test.sql
Install the MySQL GUI tools and open your SQL file, then execute it
Use phpmysql if the database is available via your webserver
you can execute mysql statements that have been written in a text file using the following command:
mysql -u yourusername -p yourpassword yourdatabase < text_file
if your database has not been created yet, log into your mysql first using:
mysql -u yourusername -p yourpassword
then:
mysql>CREATE DATABASE a_new_database_name
then:
mysql -u yourusername -p yourpassword a_new_database_name < text_file
that should do it!
More info here: http://dev.mysql.com/doc/refman/5.0/en/mysql-batch-commands.html
My favorite option to do that will be:
mysql --user="username" --database="databasename" --password="yourpassword" < "filepath"
I use it this way because when you string it with "" you avoiding wrong path and mistakes with spaces and - and probably more problems with chars that I did not encounter with.
With #elcuco comment I suggest using this command with [space] before so it tell bash to ignore saving it in history, this will work out of the box in most bash.
in case it still saving your command in history please view the following solutions:
Execute command without keeping it in history
extra security edit
Just in case you want to be extra safe you can use the following command and enter the password in the command line input:
mysql --user="username" --database="databasename" -p < "filepath"
All the top answers are good. But just in case someone wants to run the query from a text file on a remote server AND save results to a file (instead of showing on console), you can do this:
mysql -u yourusername -p yourpassword yourdatabase < query_file > results_file
Hope this helps someone.
I came here searching for this answer as well, and here is what I found works the best for me: Note I am using Ubuntu 16.x.x
Access mysql using:
mysql -u <your_user> - p
At the mysql prompt, enter:
source file_name.sql
Hope this helps.
Give the path of .sql file as:
source c:/dump/SQL/file_name.sql;
mysql> source C:\Users\admin\Desktop\fn_Split.sql
Do not specify single quotes.
If the above command is not working, copy the file to c: drive and try again.
as shown below,
mysql> source C:\fn_Split.sql
instead of redirection I would do the following
mysql -h <hostname> -u <username> --password=<password> -D <database> -e 'source <path-to-sql-file>'
This will execute the file path-to-sql-file
Never is a good practice to pass the password argument directly from the command line, it is saved in the ~/.bash_history file and can be accessible from other applications.
Use this instead:
mysql -u user --host host --port 9999 database_name < /scripts/script.sql -p
Enter password:
mysql -uusername -ppassword database-name < file.sql
So many ways to do it.
From Workbench: File > Run SQL Script -- then follow prompts
From Windows Command Line:
Option 1: mysql -u usr -p
mysql> source file_path.sql
Option 2: mysql -u usr -p '-e source file_path.sql'
Option 3: mysql -u usr -p < file_path.sql
Option 4: put multiple 'source' statements inside of file_path.sql (I do this to drop and recreate schemas/databases which requires multiple files to be run)
mysql -u usr -p < file_path.sql
If you get errors from the command line, make sure you have previously run
cd {!!>>mysqld.exe home directory here<<!!}
mysqld.exe --initialize
This must be run from within the mysqld.exe directory, hence the CD.
Hope this is helpful and not just redundant.
From linux 14.04 to MySql 5.7, using cat command piped with mysql login:
cat /Desktop/test.sql | sudo mysql -uroot -p
You can use this method for many MySQL commands to execute directly from Shell. Eg:
echo "USE my_db; SHOW tables;" | sudo mysql -uroot -p
Make sure you separate your commands with semicolon (';').
I didn't see this approach in the answers above and thought it is a good contribution.
Very likely, you just need to change the slash/blackslash:
from
\home\sivakumar\Desktop\test.sql
to
/home/sivakumar/Desktop/test.sql
So the command would be:
source /home/sivakumar/Desktop/test.sql
use the following from mysql command prompt-
source \\home\\user\\Desktop\\test.sql;
Use no quotation. Even if the path contains space(' ') use no quotation at all.
Since mysql -u yourusername -p yourpassword yourdatabase < text_file did not work on a remote server (Amazon's EC2)...
Make sure that the Database is created first.
Then:
mysql --host=localhost --user=your_username --password=your_password your_database_name < pathTofilename.sql
For future reference, I've found this to work vs the aforementioned methods, under Windows in your msql console:
mysql>>source c://path_to_file//path_to_file//file_name.sql;
If your root drive isn't called "c" then just interchange with what your drive is called. First try backslashes, if they dont work, try the forward slash. If they also don't work, ensure you have your full file path, the .sql extension on the file name, and if your version insists on semi-colons, ensure it's there and try again.
If you are here LOOKING FOR A DRUPAL ENVIRONMENT
You can run with drush command on your project directory
drush sqlc
If you are trying this command :
mysql -u root -proot -D database < /path/to/script.sql
You may get an error like this : if you have special characters, mainly '`'
ERROR 1064 (42000) at line 1: 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 '/path/to/script.sql' at line 1
So I would suggest to use a command like this :
echo "source /path/to/script.sql" | mysql -u root -proot -D database
This command will execute source /path/to/script.sql once connected to the server, which execute your script.
I had this error, and tried all the advice i could get to no avail.
Finally, the problem was that my folder had a space in the folder name which appearing as a forward-slash in the folder path, once i found and removed it, it worked fine.
I use Bash's Here Strings for an instant SQL execution:
mysql -uroot -p <<<"select date(now())"
https://www.gnu.org/software/bash/manual/html_node/Redirections.html#Here-Strings
This should be quick and simple, but after researching on Google quite a bit I am still stumped. I am mostly newbie with: server admin, CLI, MySQL.
I am developing my PHP site locally, and now need to move some new MySQL tables from my local dev setup to the remote testing site. First step for me is just to dump the tables, one at a time.
I successfully login to my local MySQL like so:
Govind% /usr/local/mysql/bin/mysql -uroot
but while in this dir (and NOT logged into MySQL):
/usr/local/mysql/bin
...when I try this
mysqldump -uroot -p myDBname myTableName > myTestDumpedTable.sql
..then I keep getting this:
"myTestDumpedTable.sql: Permission denied."
Same result if I do any variation on that (try to dump the whole db, drop the '-p', etc.)
I am embarrassed as I am sure this is going to be incredibly simple, or just reveal a gaping (basic) hole in my knowledge. .. but please help ;-)
The answer came from a helpful person on the MySQL list:
As you guys (Anson and krazybean) were thinking - I did not have permission to be writing to the /usr/local/mysql/bin/ dir. But starting from any other directory, calls to mysqldump were failing because my shell PATH var (if I said that right) is not yet set up to handle mysqldump from another dir. Also, for some reason I do not really understand yet, I also needed to use a full path on the output, even if I was calling mysqldump effectively, and even if I had permission to write to the output dir (e.g. ~/myTestDumpedTable.sql. So here was my ticket, for now (quick answer):
Govind% /usr/local/mysql/bin/mysqldump -uroot -p myDBname myTableName > /Users/Govind/myTestDumpedTable.sql
You can write to wherever your shell user has permission to do so. I just chose my user's home dir.
Hope this helps someone someday.
Cheers.
Generally I stick with defining the hostname anyways, but as you being root doesn't seem like it would be the problem, I would question where are you writing this to? What happens when you dump to > ~/myTestDumpedTable.sql
Even I was facing the same problem, the issue is with user access to 'root/bin' dir.
switch your user access as root
sudo -s
Then execute the command
mysqldump -uroot -p homestayadvisorDB > homestayadvisor_backup.sql
This will resolve the issue. Let me know if this doesn't work.
Take a look at the man page for mysqldump for correct argument usage. You need a space between the -u flag and the username, like so:
mysqldump -u root -p myDBname myTableName > myTestDumpedTable.sql
Alternatively you can do
mysqldump --user=root -p myDBname myTableName > myTestDumpedTable.sql
Since you're not providing a password in the list of arguments, you should be prompted for one. You can always provide the password in the list of arguments, but the downside to that is it appears in cleartext and will show up in the shell's command history.
In my case I'd created the directory with $ sudo mkdir /directory/to/store/sql/files. The owner of that directory is root. So changing the owner by using $ sudo chown me:me /directory/to/store/sql/files and also changing permissions to maybe $ sudo chmod 744 /directory/to/store/sql/files did the trick for me.
mysqldump don't work with sudo, if you are using
sudo mysqldump then try below solution:
sudo su
mysqldump -u[username] -p[password] db_name > newbackupfile.bkp
You should provide with a full path for SQL backup file, such as
mysqldump -u root -p databasexxx > /Users/yourusername/Sites/yoursqlfile.sql
I think you're missing the ./ from the command, try:
being inside
/usr/local/mysql/bin$ ./mysqldump -u root -p myDBname > "/Users/yourUserName/Documents/myTestDumpedTable.sql"
So it is a script, and in linux you execute a script with ./myscript.
I found it just today, and for me, in my mac OSX, I didn't use the -p, maybe because password not needed, don't know already. I mean, try also:
./mysqldump -u root myDBname > "/Users/yourUserName/Documents/myTestDumpedTable.sql"
I am trying to dump a large database using mysqldump command. I would like to avoid 'use database' command in the generated sql file.
This is because I want to create the same database with a different name. Since the sql file size is large I am unable to open the sql file and edit it.
I tried --no-create-db but still I am getting use command in the dump file
Please help.
Maybe you used something like this:
mysqldump -u -p <other options> --database your_database > file.sql
I discovered that when you use --database, the script is generated with that 'use your_database' line. So, don't use that option and the line is gone:
mysql -u -p <other options> your_database > file.sql
You should maybe post this on serverfault, but if you are on a linux box, you could consider sed (or perl/python scripts) to replace the name of the database, or remove the "use " line.
The way to do this is to run mysqldump once for each database. They way I did it is mysqldump -u user -p --tables databasename. This dumps all the tables for a database and removes the USE database statement.
--no-create-db is your friend:
http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_no-create-db