Using mysqldump and database users - mysql

I'm attempting to assemble all the options that I need for mysqldump to create everything used by my applications database into a single script. This includes the database itself and all the database users/passwords/privileges.
I've got it all figured out with the exception of the user piece... here's what I'm currently using:
mysqldump -h host -u root -p \
--add-drop-database --routines -B database_name > backup.sql
So, what am I missing?

The database users/passwords/privileges are kept in the mysql database, and won't get dumped with your dump command. You'll have to add that database as well to the list of DBs to dump:
mysqldump ... --routines --databases database_name mysql > backup.sql
or just dump everything:
mysqldump ... --routines --all-databases > backup.sql

When dumping the mysql database, don't forget:
--flush-privileges Emit a FLUSH PRIVILEGES statement after dumping the mysql
database. This option should be used any time the dump
contains the mysql database and any other database that
depends on the data in the mysql database for proper
restore.

So, I had a fundamental misunderstanding. Users are not specific to a database, but are rather created at the server level.
You can view all existing users with the following query:
SELECT * FROM mysql.user;
Knowing this, it's pretty obvious that mysqldump shouldn't do anything with users. However, if you need an answer to exporting/importing users and perms I suggest you check the following article - it helped me out.
http://pento.net/2009/03/12/backing-up-permissions-for-individual-databases/
My apologies for the noise on the board, but I figured I'd leave the post incase anyone else has the same misunderstanding.

Full process for me when migrating from one Mac OS X dev environment to a new one.
0) Get rid of newer version of MySQL on new MacBook
I had accidentally installed MySQL 8, so I had to remove it because it was newer than my old MacBook.
# Remove binaries
$ brew uninstall mysql
# Remove data/config that is leftover
$ rm -r /usr/local/var/mysql/
1) Install same version of MySQL on new MacBook
# Install version that matched old MacBook
$ brew install mysql#5.7
# Because it is an old version, you have to do a special configuration step
$ echo 'export PATH="/usr/local/opt/mysql#5.7/bin:$PATH"' >> ~/.bash_profile
$ source ~/.bash_profile
# Start server
$ mysql.server start
2) Dump data on old MacBook
$ mysqldump -uroot --flush-privileges --routines --all-databases > complete_dump.sql
3) Restore data on new MacBook
$ mysql -p -uroot < complete_dump.sql

You may want to take a look at mysqlpump, it can dump all databases, and can export the users as CREATE USER and GRANT statements instead of relying on the mysql system db + mysql_upgrade.
mysqlpump # Dumps (almost) all databases (see [2])
mysqlpump --exclude-databases=% --users # Dumps all user accounts
It's unclear from the docs if you can do mysqlpump --users and get both all databases and the users.
Some system tables get omitted by default, see restrictions.

mysqldump version 10.19 contains the option --system=users wich adds CREATE USER with the GRANT commands into the dump.
mysqldump -h host -u root -p \
--add-drop-database --routines -B database_name \
--system=users > backup.sql
source: https://mariadb.com/kb/en/mysqldump/

Connect to your database server and execute:
select concat('show grants for ','\'',user,'\'#\'',host,'\'') from mysql.user;
You'll get something like this:
+--------------------------------------------------------+
| concat('show grants for ','\'',user,'\'#\'',host,'\'') |
+--------------------------------------------------------+
| show grants for 'mariadb.sys'#'localhost' |
| show grants for 'mysql'#'localhost' |
| show grants for 'root'#'localhost' |
+--------------------------------------------------------+
Capture the output to some temporary file.
And then cycle through each line in that temporary file, sending it against your mysql server, capturing the output.
Output will be something that you can use to reconstruct users on another server:
GRANT ALL PRIVILEGES ON *.* TO `root`#`localhost` IDENTIFIED VIA mysql_native_password USING 'invalid' OR unix_socket WITH GRANT OPTION
GRANT PROXY ON ''#'%' TO 'root'#'localhost' WITH GRANT OPTION
Here's a script I'm currently using:
mysql -u${BACKUP_DB_USER} -p${BACKUP_DB_PASSWORD} -e"select concat('show grants for ','\'',user,'\'#\'',host,'\'') from mysql.user" > user_list_with_header.txt
sed '1d' user_list_with_header.txt > ./user.txt
while read user; do mysql -u${BACKUP_DB_USER} -p${BACKUP_DB_PASSWORD} -e"$user" > user_grant.txt; echo "-- ${user}" >> user_privileges.txt; sed '1d' user_grant.txt >> user_privileges.txt; done < user.txt
echo "flush privileges" >> user_privileges.txt;
awk '{print $0";"}' user_privileges.txt > all_user_privileges_final.sql
rm user.txt user_list_with_header.txt user_grant.txt user_privileges.txt
You'll have all grant statements in the file all_user_privileges_final.sql.
Of course, you can limit your initial query to list only user(s) you want.

You can only use the option --system=users to get the user's dump from mysql/MariaDB if mysql client is for MariaDB.
For example:
check mysql --version. If output is like mysql Ver 15.1 Distrib 10.6.8-MariaDB then
mysqldump -h <hostname> -u <usename> -p --system=users > mysqldb_users.sql

Related

How to backup mysql using mysqldump?

I am new to using mysql and i am trying to backup a mysql database using mysqldump.
So this is what i have done so far:-
I ssh'ed into my VM and run mysql to get into the mysql CLI
Then i ran the command mysql> SHOW DATABASES; which gives me the following output :-
information|
| rap_songs|
| celebrities|
Now i am trying to backup my rap songs database and i run the following command :-
mysql> mysqldump -u root -p rap_songs > rap_songs_backup.sql
But nothing happens after this step. What am i doing wrong? Any help will be appreciated. thank you.
Run mysqldump in SSH terminal not in mysql terminal
For all database::
mysqldump --all-databases --single-transaction -u root -p > /all_databases.sql
For one database (rap_song in this case)::
mysqldump -u root -p rap_songs > /your_backup.sql
Now you need to go to / to see your files, mysqldump usually show nothing once its done terminal again avail for new cmd. So if terminal is again avail it means its done!
Non-root user | Permission denied error!
Don't use /your_backup.sql
use ~/your_backup.sql instead
~/ is your user data directory
Mysqldump without password
You can create a .my.cnf in ~/
[mysqldump]
user=mysqluser
password=userpass
Then chmod 600 ~/.my.cnf
now in your script or crontab don't mention -p or --password and when your cron or script execute it will automatically pick password from here
Okay so I had to run this query in order to circumvent the errors :-
mysqldump --complete-insert --routines --triggers --single-transaction
rap_songs> rap_songs_backup.sql

Backup all privileges related to specific user of MySQL

I want to backup all privileges related to specific user (for example u_1) from a MySQL database and restore that in another server. As described here, The 'mysql' database contains users/privileges/passwords. So I have to backup all related data from all tables of mysql database (mysql.user,mysql.db, ...). I can run this command:
$ mysqldump -u root --flush-privileges -r mysql.sql -p mysql
But of course the mysql.sql contains all users and all privileges.
Also I tried this command:
$ mysqldump -u root -p --where="user.user='u_1'" mysql user> mysql.sql
But as expected, it only contains a row of mysql.user table.
Is there a way to strip out other users except of u_1?
Try these options (line breaks for clarity):
$ mysqldump -u root -p
--where="user='u_1'"
--complete-insert
--extended-insert
--no-create-info
mysql
user db tables_priv columns_priv procs_priv proxies_priv
> mysql.sql
Or... let's call the above solution "the hard way."
This should be the easy way:
$ mysql -u root -p
--skip-column-names
-e "SHOW GRANTS FOR 'u_1';"
> grants.sql
I would use:
pt-show-grants --only u_1
pt-show-grants is a tool in the free Percona Toolkit.
See https://www.percona.com/doc/percona-toolkit/LATEST/pt-show-grants.html

How do I import an SQL file using the command line in MySQL?

I have a .sql file with an export from phpMyAdmin. I want to import it into a different server using the command line.
I have a Windows Server 2008 R2 installation. I placed the .sql file on the C drive, and I tried this command
database_name < file.sql
It is not working. I get syntax errors.
How can I import this file without a problem?
Do I need to create a database first?
Try:
mysql -u username -p database_name < file.sql
Check MySQL Options.
Note 1: It is better to use the full path of the SQL file file.sql.
Note 2: Use -R and --triggers with mysqldump to keep the routines and triggers of the original database. They are not copied by default.
Note 3 You may have to create the (empty) database from MySQL if it doesn't exist already and the exported SQL doesn't contain CREATE DATABASE (exported with --no-create-db or -n option) before you can import it.
A common use of mysqldump is for making a backup of an entire database:
mysqldump db_name > backup-file.sql
You can load the dump file back into the server like this:
Unix
mysql db_name < backup-file.sql
The same in the Windows command prompt:
mysql -p -u [user] [database] < backup-file.sql
PowerShell
cmd.exe /c "mysql -u root -p db_name < backup-file.sql"
MySQL command line
mysql> use db_name;
mysql> source backup-file.sql;
Regarding the time taken for importing huge files: most importantly, it takes more time because the default setting of MySQL is autocommit = true. You must set that off before importing your file and then check how import works like a gem.
You just need to do the following thing:
mysql> use db_name;
mysql> SET autocommit=0 ; source the_sql_file.sql ; COMMIT ;
Among all the answers, for the problem above, this is the best one:
mysql> use db_name;
mysql> source file_name.sql;
Easiest way to import into your schema:
Login to mysql and issue below mention commands.
mysql> use your_db_name;
mysql> source /opt/file.sql;
We can use this command to import SQL from the command line:
mysql -u username -p password db_name < file.sql
For example, if the username is root and password is password. And you have a database name as bank and the SQL file is bank.sql. Then, simply do like this:
mysql -u root -p password bank < bank.sql
Remember where your SQL file is. If your SQL file is in the Desktop folder/directory then go the desktop directory and enter the command like this:
cd ~/Desktop
mysql -u root -p password bank < bank.sql
And if you are in the Project directory and your SQL file is in the Desktop directory. If you want to access it from the Project directory then you can do like this:
cd ~/Project
mysql -u root -p password bank < ~/Desktop/bank.sql
If you already have the database, use the following to import the dump or the sql file:
mysql -u username -p database_name < file.sql
if you don't you need to create the relevant database(empty) in MySQL, for that first log on to the MySQL console by running the following command in terminal or in cmd
mysql -u userName -p;
And when prompted provide the password.
Next, create a database and use it:
mysql>create database yourDatabaseName;
mysql>use yourDatabaseName;
Then import the sql or the dump file to the database from
mysql> source pathToYourSQLFile;
Note: if your terminal is not in the location where the dump or sql file exists, use the relative path in above.
Open the MySQL command line
Type the path of your mysql bin directory and press Enter
Paste your SQL file inside the bin folder of mysql server.
Create a database in MySQL.
Use that particular database where you want to import the SQL file.
Type source databasefilename.sql and Enter
Your SQL file upload successfully.
A solution that worked for me is below:
Use your_database_name;
SOURCE path_to_db_sql_file_on_your_local;
While most answers here just mention the simple command
mysql -u database_user -p [db_name] < database_file.sql
today it's quite common that databases and tables have utf8-collation where this command is not sufficient.
Having utf8-collation in the exported tables it's required to use this command:
mysql -u database_user -p --default-character-set=utf8 [db_name] < database_file.sql
An according export can be done with
mysqldump -u database_user -p --default-character-set=utf8 [db_name] > database_file.sql
Surely this works for other charsets too, how to show the right notation can be seen here:
https://dev.mysql.com/doc/refman/5.7/en/show-collation.html
One comment mentioned also that if a database never exists an empty database had to be created first. This might be right in some cases but depends on the export file. If the exported file includes already the command to create the database then the database never has to be created in a separate step, which even could cause an error on import. So on import, it's advisable to have a look first in the file to know which commands are included there, on export, it's advisable to note the settings, especially if the file is very large and hard to read in an editor.
There are still more parameters for the command which are listed and explained here:
https://dev.mysql.com/doc/refman/5.7/en/mysql-command-options.html
If you use another database version consider searching for the corresponding version of the manual too. The mentioned links refer to MySQL version 5.7.
EDIT:
The same parameters are working for mysqldump too. So while the commands for export and import are different, the mentioned parameters are not.
Nevertheless there exists a special site in the manual that describes the options for mysqldump: https://dev.mysql.com/doc/refman/5.7/en/mysqldump.html
To dump a database into an SQL file use the following command.
mysqldump -u username -p database_name > database_name.sql
To import an SQL file into a database (make sure you are in the same directory as the SQL file or supply the full path to the file), do:
mysql -u username -p database_name < database_name.sql
I think it's worth mentioning that you can also load a gzipped (compressed) file with zcat like shown below:
zcat database_file.sql.gz | mysql -u username -p -h localhost database_name
Go to the directory where you have the MySQL executable. -u for username and -p to prompt for the password:
C:\xampp\mysql\bin>mysql -u username -ppassword databasename < C:\file.sql
To import a single database, use the following command.
mysql -u username -p password dbname < dump.sql
To import multiple database dumps, use the following command.
mysql -u username -p password < dump.sql
To import a database, use the following command.
mysql> create new_database;
mysql> use new_database;
mysql> source (Here you need to import the path of the SQL file);
E.g.:
mysql> source E:/test/dump.sql;
You need to use forward slashes (/) even on Windows, e.g., E:/test/dump.sql instead of E:\test\dump.sql
Or double backslashes (\\) because of escaping, i.e., E:\\test\\dump.sql
mysql --user=[user] --password=[password] [database] < news_ml_all.sql
I kept running into the problem where the database wasn't created.
I fixed it like this:
mysql -u root -e "CREATE DATABASE db_name"
mysql db_name --force < import_script.sql
For exporting a database:
mysqldump -u username -p database_name > file.sql
For importing a database:
mysql -u username -p database_name < file.sql
For importing multiple SQL files at one time, use this:
# Unix-based solution
for i in *.sql ; do mysql -u root -pPassword DataBase < $i ; done
For simple importing:
# Unix-based solution
mysql -u root -pPassword DataBase < data.sql
For WAMP:
REM mysqlVersion - replace with your own version
C:\wamp\bin\mysql\mysqlVersion\bin\mysql.exe -u root -pPassword DataBase < data.sql
For XAMPP:
C:\xampp\mysql\bin\mysql -u root -pPassword DataBase < data.sql
You do not need to specify the name of the database on the command line if the .sql file contains CREATE DATABASE IF NOT EXISTS db_name and USE db_name statements.
Just make sure you are connecting with a user that has the permissions to create the database, if the database mentioned in the .sql file does not exist.
Import a database
Go to drive:
d:
MySQL login
c:\xampp\mysql\bin\mysql -u root -p
It will ask for pwd. Enter it:
pwd
Select the database
use DbName;
Provide the file name
\.DbName.sql
Use:
mysql -u root -p password -D database_name << import.sql
Use the MySQL help for details - mysql --help.
I think these will be useful options in our context:
[~]$ mysql --help
mysql Ver 14.14 Distrib 5.7.20, for osx10.12 (x86_64) using EditLine wrapper
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Usage: mysql [OPTIONS] [database]
-?, --help Display this help and exit.
-I, --help Synonym for -?
--bind-address=name IP address to bind to.
-D, --database=name Database to use.
--delimiter=name Delimiter to be used.
--default-character-set=name Set the default character set.
-f, --force Continue even if we get an SQL error.
-p, --password[=name] Password to use when connecting to server.
-h, --host=name Connect to host.
-P, --port=# Port number to use for connection or 0 for default to, in order of preference, my.cnf, $MYSQL_TCP_PORT, /etc/services, built-in default (3306).
--protocol=name The protocol to use for connection (tcp, socket, pipe,
-s, --silent Be more silent. Print results with a tab as separator, each row on new line.
-v, --verbose Write more. (-v -v -v gives the table output format).
-V, --version Output version information and exit.
-w, --wait Wait and retry if connection is down.
What is fun, if we are importing a large database and not having a progress bar. Use Pipe Viewer and see the data transfer through the pipe
For Mac, brew install pv
For Debian/Ubuntu, apt-get install pv.
For others, refer to pv - Pipe Viewer
pv import.sql | mysql -u root -p password -D database_name
1.45GiB 1:50:07 [339.0KiB/s] [=============> ] 14% ETA 11:09:36
1.46GiB 1:50:14 [ 246KiB/s] [=============> ] 14% ETA 11:09:15
1.47GiB 1:53:00 [ 385KiB/s] [=============> ] 14% ETA 11:05:36
Go to the directory where you have MySQL.
c:\mysql\bin\> mysql -u username -p password database_name <
filename.sql
Also to dump all databases, use the -all-databases option, and no databases’ name needs to be specified anymore.
mysqldump -u username -ppassword –all-databases > dump.sql
Or you can use some GUI clients like SQLyog to do this.
You can try this query.
Export:
mysqldump -u username –-password=your_password database_name > file.sql
Import:
mysql -u username –-password=your_password database_name < file.sql
and detail following this link:
https://chartio.com/resources/tutorials/importing-from-and-exporting-to-files-using-the-mysql-command-line/
Add the --force option:
mysql -u username -p database_name --force < file.sql
The following command works for me from the command line (cmd) on
Windows 7 on WAMP.
d:/wamp/bin/mysql/mysql5.6.17/bin/mysql.exe -u root -p db_name < database.sql
Providing credentials on the command line is not a good idea. The above answers are great, but neglect to mention
mysql --defaults-extra-file=etc/myhost.cnf database_name < file.sql
Where etc/myhost.cnf is a file that contains host, user, password, and you avoid exposing the password on the command line. Here is a sample,
[client]
host=hostname.domainname
user=dbusername
password=dbpassword
Import into the database:
mysql -u username -p database_name < /file path/file_name.sql
Export from the database:
mysqldump -u username -p database_name > /file path/file_name.sql
After these commands, a prompt will ask for your MySQL password.
Similarly to vladkras's answer to How do import an SQL file using the command line in MySQL?.
Key differences for me:
The database has to exist first
No space between -p and the password
shell> mysql -u root -ppassword #note: no space between -p and password
mysql> CREATE DATABASE databasename;
mysql> using databasename;
mysql> source /path/to/backup.sql
I am running Fedora 26 with MariaDB.
I thought it could be useful for those who are using Mac OS X:
/Applications/xampp/xamppfiles/bin/mysql -u root -p database < database.sql
Replace xampp with mamp or other web servers.

How to use MySQL dump from a remote machine

How can I backup a mysql database which is running on a remote server, I need to store the back up file in the local pc.
Try it with Mysqldump
#mysqldump --host=the.remotedatabase.com -u yourusername -p yourdatabasename > /User/backups/adump.sql
Have you got access to SSH?
You can use this command in shell to backup an entire database:
mysqldump -u [username] -p[password] [databasename] > [filename.sql]
This is actually one command followed by the > operator, which says, "take the output of the previous command and store it in this file."
Note: The lack of a space between -p and the mysql password is not a typo. However, if you leave the -p flag present, but the actual password blank then you will be prompted for your password. Sometimes this is recommended to keep passwords out of your bash history.
No one mentions anything about the --single-transaction option. People should use it by default for InnoDB tables to ensure data consistency. In this case:
mysqldump --single-transaction -h [remoteserver.com] -u [username] -p [password] [yourdatabase] > [dump_file.sql]
This makes sure the dump is run in a single transaction that's isolated from the others, preventing backup of a partial transaction.
For instance, consider you have a game server where people can purchase gears with their account credits. There are essentially 2 operations against the database:
Deduct the amount from their credits
Add the gear to their arsenal
Now if the dump happens in between these operations, the next time you restore the backup would result in the user losing the purchased item, because the second operation isn't dumped in the SQL dump file.
While it's just an option, there are basically not much of a reason why you don't use this option with mysqldump.
This topic shows up on the first page of my google result, so here's a little useful tip for new comers.
You could also dump the sql and gzip it in one line:
mysqldump -u [username] -p[password] [database_name] | gzip > [filename.sql.gz]
mysqldump -h [domain name/ip] -u [username] -p[password] [databasename] > [filename.sql]
Tried all the combinations here, but this worked for me:
mysqldump -u root -p --default-character-set=utf8mb4 [DATABASE TO BE COPIED NAME] > [NEW DATABASE NAME]
If you haven't install mysql_client yet and using Docker container instead:
sudo docker exec MySQL_CONTAINER_NAME /usr/bin/mysqldump --host=192.168.1.1 -u username --password=password db_name > dump.sql
You can directly pipe it to the remote server where you wish to copy your data to:
mysqldump -u your_db_user_name -p --set-gtid-purged=OFF --triggers --routines --events --compress --skip-lock-tables --verbose your_local_sql_db_name | mysql -u your_db_user_name -p -h your_remote_server_ip your_remote_server_db_name
You need to have created the db on your remote sql server.
Using the above command, I was able to copy from my local sql server version 8.0.23 to my remote sqlserver running 8.0.25
This is how you would restore a backup after you successfully backup your .sql file
mysql -u [username] [databasename]
And choose your sql file with this command:
source MY-BACKED-UP-DATABASE-FILE.sql

Copying a mysql database from localhost to remote server using mysqldump.exe

I want to copy a mysql database from my local computer to a remote server.
I am trying to use the mysql dump command. All the examples on the internet suggest doing something like
The initial mysql> is just the prompt I get after logging in.
mysql> mysqldump -u user -p pass myDBName | NewDBName.out;
But when I do this I get You have an error in your SQL syntax; check the manual that corresponds ... to use near 'mysqldump -u user -p pass myDBName | NewDBName.out'
Since I have already logged in do I need to use -u and -p? Not doing so gives me the same error. Can you see what is wrong?
In addition to what Alexandre said, you probably don't want to pipe (|) output to NewDBName.out, but rather redirect it there (>).
So from the Windows/Unix command line:
mysqldump -u user -p pass myDBName > NewDBName.out
Note that if you have large binary fields (e.g. BLOBS) in some columns you may need to set an additional option (I think it was --hex-blob, but there might have been another option too). If that applies to you, add a comment and I'll research the setting.
mysqldump is not an SQL statement that you execute inside a mysql session but a distinct binary that should be started from your OS shell.
The are a few ways to use this. One of them is to pipe the output of mysqldump to another MySQL instance:
echo CREATE DATABASE remote_db | mysql -h remote_host -u remote_user -premote_password
mysqldump -h source_host -u root -ppassword source_db | mysql -h remote_host -u remote_user -premote_password -D remote_db
I have had to dump large sets of data recently. From what I have found on a 200Mb database with 10,000+ records in many of the tables is the following. I used the linux 'time' command to get actual time.
12 minutes using:
mysqldump -u user -p pass myDBName > db-backups.sql
7 minutes to clone the database:
mysqldump -u user -p pass myDBName | mysql -u user -p pass cloneDBName
And in less than a second:
mysqlhotcopy -u user -p pass myDBName cloneDBName
The last one blew my mind, but you have to be logged in locally where the database server resides. Personally I think this is much faster than remotely doing a dump, the you can compress the .sql file and transfer it manually.