I have the following commands in a shell script where I do a mysql dump, then I load that SQL file over ssh into a remote database, and then I update the timestamp.
1. mysqldump -u root files path | gzip -9 > $SQL_FILE
2. cat $SQL_FILE | ssh -i ~/metadata.pem ubuntu#1.2.3.4
"zcat | mysql -u 'root' -h 1.2.3.4 metadata"
3. TIMESTAMP=`date "+%Y-%m-%d-%T"`
4. mysql -u 'root' -h 1.2.3.4 metadata -e "UPDATE path_last_updated SET timestamp=DEFAULT"
Is there any way to improve the above commands. For example, what happens if line 2 fails (for example, due to a connectivity issue), but line 4 succeeds?
How would I make line 4 running conditional on the success of line 2?
You could chain all in one block:
mysqldump -u root files path |
gzip -9 |
ssh -i ~/metadata.pem ubuntu#1.2.3.4 "zcat |\
mysql -u 'root' -h 1.2.3.4 metadata" &&
mysql -u 'root' -h 1.2.3.4 metadata -e "
UPDATE path_last_updated SET timestamp=DEFAULT"
So last mysql command won't be executed if something fail before.
You can use $? for get return code of last command, if it's not 0, it failed.
Or you can use && for example : cmd1 && cmd2.
Or you can use set -e to stop script if occur an error.
Related
I want to copy a Database without copying its data, I mean I just want to copy the stucture and tables and foreign key and ... not the data in it.
The answer is here but I do not know where should I copy it ? In shell? In workbench? In query?
I entered it in query in workbenck and it has error !
Thank you in advance!
Edit
When I run it in my mysql shell I get this:
MySQL JS > mysqldump -u myusername -pmypassword -d olddb | mysql -u myusername -pmypassword -D newdb
SyntaxError: Unexpected identifier.
You'll need to run it on the command line for your OS (not the shell for MySQL as you tried earlier).
Under Linux (including Macs) it would look something like:
smm#smm-HP-ZBook-15-G2:~/$ mysqldump -u myusername -pmypassword -d olddb | mysql -u myusername -pmypassword -D newdb
Under Windows:
C:\> mysqldump -u myusername -pmypassword -d olddb | mysql -u myusername -pmypassword -D newdb
This is assuming mysqldump is in the PATH for your command line (it isn't if you get a command not found error). How to use a command line and set up the PATH depends on the OS and is beyond the scope of this answer.
Refer following links..
1) Create dump file
2) Reload dump file
I'm under VPN and I don't have SSH access to remote server.
I can connect to remote database by console
mysql -u username -p -h remote.site.com
Now I'm trying to clone the remote database to local computer
mysqldump -u username -p -h remote.site.com mysqldump | mysql -u root -ppassword webstuff
And I've got error
mysqldump: Got error: 1045: Access denied for user 'webstaff'#'10.75.1.2'
(using password: YES) when trying to connect
How to copy mysql database from remote server to local computer?
Assuming the following command works successfully:
mysql -u username -p -h remote.site.com
The syntax for mysqldump is identical, and outputs the database dump to stdout. Redirect the output to a local file on the computer:
mysqldump -u username -p -h remote.site.com DBNAME > backup.sql
Replace DBNAME with the name of the database you'd like to download to your computer.
Check syntax and execute one command at a time, then verify output.
mysqldump -u remoteusername -p remotepassword -h your.site.com databasename > dump.sql
mysql -u localusername -p localpassword databasename < dump.sql
Once you've matched all passwords, you can use pipe.
Often our databases are really big and the take time to take dump directly from remote machine to other machine as our friends other have suggested above.
In such cases what you can do is to take the dump on remote machine using MYSQLDUMP Command
MYSQLDUMP -uuser -p --all-databases > file_name.sql
and than transfer that file from remote server to your machine using Linux SCP Command
scp user#remote_ip:~/mysql_dump_file_name.sql ./
This can have different reasons like:
You are using an incorrect password
The MySQL server got an error when trying to resolve the IP address of the client host to a name
No privileges are granted to the user
You can try one of the following steps:
To reset the password for the remote user by:
SET PASSWORD FOR some_user#ip_addr_of_remote_client=PASSWORD('some_password');
To grant access to the user by:
GRANT SELECT, INSERT, UPDATE, DELETE, LOCK TABLES ON YourDB.* TO user#Host IDENTIFIED by 'password';
Hope this helps you, if not then you will have to go through the documentation
Please check this gist.
https://gist.github.com/ecdundar/789660d830d6d40b6c90
#!/bin/bash
# copymysql.sh
# GENERATED WITH USING ARTUR BODERA S SCRIPT
# Source script at: https://gist.github.com/2215200
MYSQLDUMP="/usr/bin/mysqldump"
MYSQL="/usr/bin/mysql"
REMOTESERVERIP=""
REMOTESERVERUSER=""
REMOTESERVERPASSWORD=""
REMOTECONNECTIONSTR="-h ${REMOTESERVERIP} -u ${REMOTESERVERUSER} --password=${REMOTESERVERPASSWORD} "
LOCALSERVERIP=""
LOCALSERVERUSER=""
LOCALSERVERPASSWORD=""
LOCALCONNECTION="-h ${LOCALSERVERIP} -u ${LOCALSERVERUSER} --password=${LOCALSERVERPASSWORD} "
IGNOREVIEWS=""
MYVIEWS=""
IGNOREDATABASES="select schema_name from information_schema.SCHEMATA where schema_name != 'information_schema' and schema_name != 'mysql' and schema_name != 'performance_schema' ;"
# GET A LIST OF DATABASES
databases=`$MYSQL $REMOTECONNECTIONSTR -e "${IGNOREDATABASES}" | tr -d "| " | grep -v schema_name`
# COPY ALL TABLES
for db in $databases; do
# GET LIST OF ITEMS
views=`$MYSQL $REMOTECONNECTIONSTR --batch -N -e "select table_name from information_schema.tables where table_type='VIEW' and table_schema='$db';"
IGNOREVIEWS=""
for view in $views; do
IGNOREVIEWS=${IGNOREVIEWS}" --ignore-table=$db.$view "
done
echo "TABLES "$db
$MYSQL $LOCALCONNECTION --batch -N -e "create database $db; "
$MYSQLDUMP $REMOTECONNECTIONSTR $IGNOREVIEWS --compress --quick --extended-insert --skip-add-locks --skip-comments --skip-disable-keys --default-character-set=latin1 --skip-triggers --single-transaction $db | mysql $LOCALCONNECTION $db
done
# COPY ALL PROCEDURES
for db in $databases; do
echo "PROCEDURES "$db
#PROCEDURES
$MYSQLDUMP $REMOTECONNECTIONSTR --compress --quick --routines --no-create-info --no-data --no-create-db --skip-opt --skip-triggers $db | \
sed -r 's/DEFINER=`[^`]+`#`[^`]+`/DEFINER=CURRENT_USER/g' | mysql $LOCALCONNECTION $db
done
# COPY ALL TRIGGERS
for db in $databases; do
echo "TRIGGERS "$db
#TRIGGERS
$MYSQLDUMP $REMOTECONNECTIONSTR --compress --quick --no-create-info --no-data --no-create-db --skip-opt --triggers $db | \
sed -r 's/DEFINER=`[^`]+`#`[^`]+`/DEFINER=CURRENT_USER/g' | mysql $LOCALCONNECTION $db
done
# COPY ALL VIEWS
for db in $databases; do
# GET LIST OF ITEMS
views=`$MYSQL $REMOTECONNECTIONSTR --batch -N -e "select table_name from information_schema.tables where table_type='VIEW' and table_schema='$db';"`
MYVIEWS=""
for view in $views; do
MYVIEWS=${MYVIEWS}" "$view" "
done
echo "VIEWS "$db
if [ -n "$MYVIEWS" ]; then
#VIEWS
$MYSQLDUMP $REMOTECONNECTIONSTR --compress --quick -Q -f --no-data --skip-comments --skip-triggers --skip-opt --no-create-db --complete-insert --add-drop-table $db $MYVIEWS | \
sed -r 's/DEFINER=`[^`]+`#`[^`]+`/DEFINER=CURRENT_USER/g' | mysql $LOCALCONNECTION $db
fi
done
echo "OK!"
Copy mysql database from remote server to local computer
I ran into the same problem. And I could not get it done with the other answers. So here is how I finally did it (yes, a beginner tutorial):
Step 1: Create a new database in your local phpmyadmin.
Step 2: Dump the database on the remote server into a sql file (here I used Putty/SSH):
mysqldump --host="mysql5.domain.com" --user="db231231" --password="DBPASSWORD" databasename > dbdump.sql
Step 3: Download the dbdump.sql file via FTP client (should be located in the root folder)
Step 4: Move the sql file to the folder of your localhost installation, where mysql.exe is located. I am using uniform-server, this would be at C:\uniserver\core\mysql\bin\, with XAMPP it would be C:\xampp\mysql\bin
Step 5: Execute the mysql.exe as follows:
mysql.exe -u root -pYOURPASSWORD YOURLOCALDBNAME < dbdump.sql
Step 6: Wait... depending on the file size. You can check the progress in phpmyadmin, seeing newly created tables.
Step 7: Done. Go to your local phpmyadmin to check if the database has been filled with the entire data.
Hope that helps. Good luck!
Note 1: When starting the uniformer-server you can specify a password for mysql. This is the one you have to use above for YOURPASSWORD.
Note 2: If the login does not work and you run into password problems, check your password if it contains special characters like !. If so, then you probably need to escape them \!.
Note 3: In case not all mysql data can be found in the local db after the import, it could be that there is a problem with the mysql directives of your dbdump.sql
Better yet use a oneliner:
Dump remoteDB to localDB:
mysqldump -uroot -pMypsw -h remoteHost remoteDB | mysql -u root -pMypsw localDB
Dump localDB to remoteDB:
mysqldump -uroot -pmyPsw localDB | mysql -uroot -pMypsw -h remoteHost remoteDB
C:\Users\>mysqldump -u root -p -h ip address --databases database_name -r sql_file.sql
Enter password: your_password
This answer is not remote server but local server. The logic should be the same. To copy and backup my local machine MAMP database to my local desktop machine folder, go to console then
mysqldump -h YourHostName -u YourUserNameHere -p YourDataBaseNameHere > DestinationPath/xxxwhatever.sql
In my case YourHostName was localhost. DestinationPath is the path to the download; you can drag and drop your desired destination folder and it will paste the path in.
Then password may be asked:
Enter password: xxxxxxxx
This question already has answers here:
Downloading MySQL dump from command line
(15 answers)
How do I import an SQL file using the command line in MySQL?
(54 answers)
Closed 4 years ago.
Not Duplicate! looking for some feature have phpmyadmin during export in command line
I want to export and import a .sql file to and from a MySQL database from command line.
Is there any command to export .sql file in MySQL? Then how do I import it?
When doing the export/import, there may be constraints like enable/disable foreign key check or export only table structure.
Can we set those options with mysqldump?
some example of Options
Type the following command to import sql data file:
$ mysql -u username -p -h localhost DATA-BASE-NAME < data.sql
In this example, import 'data.sql' file into 'blog' database using vivek as username:
$ mysql -u vivek -p -h localhost blog < data.sql
If you have a dedicated database server, replace localhost hostname with with actual server name or IP address as follows:
$ mysql -u username -p -h 202.54.1.10 databasename < data.sql
To export a database, use the following:
mysqldump -u username -p databasename > filename.sql
Note the < and > symbols in each case.
If you're already running the SQL shell, you can use the source command to import data:
use databasename;
source data.sql;
mysqldump will not dump database events, triggers and routines unless explicitly stated when dumping individual databases;
mysqldump -uuser -p db_name --events --triggers --routines > db_name.sql
Well you can use below command to export,
mysqldump --databases --user=root --password your_db_name >
export_into_db.sql
and the generated file will be available in the same directory where you had ran this command.
Now login to mysql using command,
mysql -u[username] -p
then use "source" command with the file path.
Dump an entire database to a file:
mysqldump -u USERNAME -p password DATABASENAME > FILENAME.sql
Try
mysqldump databaseExample > file.sql
since I have no enough reputation to comment after the highest post, so I add here.
use '|' on linux platform to save disk space.
thx #Hariboo, add events/triggers/routints parameters
mysqldump -x -u [uname] -p[pass] -C --databases db_name --events --triggers --routines | sed -e 's/DEFINER[ ]*=[ ]*[^*]*\*/\*/ ' | awk '{ if (index($0,"GTID_PURGED")) { getline; while (length($0) > 0) { getline; } } else { print $0 } }' | grep -iv 'set ##' | trickle -u 10240 mysql -u username -p -h localhost DATA-BASE-NAME
some issues/tips:
Error: ......not exist when using LOCK TABLES
# --lock-all-tables,-x , this parameter is to keep data consistency because some transaction may still be working like schedule.
# also you need check and confirm: grant all privileges on *.* to root#"%" identified by "Passwd";
ERROR 2006 (HY000) at line 866: MySQL server has gone away
mysqldump: Got errno 32 on write
# set this values big enough on destination mysql server, like: max_allowed_packet=1024*1024*20
# use compress parameter '-C'
# use trickle to limit network bandwidth while write data to destination server
ERROR 1419 (HY000) at line 32730: You do not have the SUPER privilege and binary logging is enabled (you might want to use the less safe log_bin_trust_function_creators variable)
# set SET GLOBAL log_bin_trust_function_creators = 1;
# or use super user import data
ERROR 1227 (42000) at line 138: Access denied; you need (at least one of) the SUPER privilege(s) for this operation
mysqldump: Got errno 32 on write
# add sed/awk to avoid some privilege issues
hope this help!
You can use this script to export or import any database from terminal given at this link: https://github.com/Ridhwanluthra/mysql_import_export_script/blob/master/mysql_import_export_script.sh
echo -e "Welcome to the import/export database utility\n"
echo -e "the default location of mysqldump file is: /opt/lampp/bin/mysqldump\n"
echo -e "the default location of mysql file is: /opt/lampp/bin/mysql\n"
read -p 'Would like you like to change the default location [y/n]: ' location_change
read -p "Please enter your username: " u_name
read -p 'Would you like to import or export a database: [import/export]: ' action
echo
mysqldump_location=/opt/lampp/bin/mysqldump
mysql_location=/opt/lampp/bin/mysql
if [ "$action" == "export" ]; then
if [ "$location_change" == "y" ]; then
read -p 'Give the location of mysqldump that you want to use: ' mysqldump_location
echo
else
echo -e "Using default location of mysqldump\n"
fi
read -p 'Give the name of database in which you would like to export: ' db_name
read -p 'Give the complete path of the .sql file in which you would like to export the database: ' sql_file
$mysqldump_location -u $u_name -p $db_name > $sql_file
elif [ "$action" == "import" ]; then
if [ "$location_change" == "y" ]; then
read -p 'Give the location of mysql that you want to use: ' mysql_location
echo
else
echo -e "Using default location of mysql\n"
fi
read -p 'Give the complete path of the .sql file you would like to import: ' sql_file
read -p 'Give the name of database in which to import this file: ' db_name
$mysql_location -u $u_name -p $db_name < $sql_file
else
echo "please select a valid command"
fi
How can i use mysqldump to backup and restore database to a remote server?
Both have root access. I am using putty to perform this.
So far I tried the following:
mysqldump -u root -p >z*x311a!# masdagn_joom15 | mysql \ -u root -p g2154hE6-AsXP --host=207.210.71.26 -C masdagn_joom15temp \g
but it refused
the local password is: >z*x311a!#
the remote password is: g2154hE6-AsXP
This link provides information on backing up and restoring with mysqldump. It also gives some examples with a remote server.
The important commands from that link being:
backup:
mysqldump -u root -p[root_password] [database_name] > dumpfilename.sql
restore:
mysql -u root -p[root_password] [database_name] < dumpfilename.sql
[local-server]# mysqldump -u root -prootpswd db | mysql \
-u root -ptmppassword --host=remote-server -C db1
[Note: There are two -- (hyphen) in front of host]
Please note that you should first create the db1 database on the remote-server before executing the following command.
mysqldump --user=username --password=pwd db_name | bzip2 -c > /backup_dir/db_name.sql.bz2
you can embed this part in a script, afterward you can use FTP to transfer to the other location.
To restore, you can
bzip2 -d db_name.sql.bz2
mysql --user=username --password=pwd db_name < db_name.sql
Your local password contains the > character, which is interpreted as a redirect character by most shells. As a general rule, it will make your life considerably easier if you keep your MySQL passwords alphanumeric [A-Za-z0-9]. And it will make your system more secure if you avoid publicly posting your passwords.
here is what I do for a quick dump to another remote server...
assuming that you have setup an ssh key between the 2 servers
create file dump-to-server.sh
chmod to executable (chmod 0755 dump-to-server.sh)
run your sync ./dump-to-server.sh schema_name root#remote.server.net
dump-to-server.sh
\#!/bin/bash
if [[ -z "$1" || -z "$2" ]]; then
echo "--------- usage ---------";
echo "./dump-to-server.sh schema_name root#remote.server.net";
echo "";
else
mysqldump --opt "$1" | gzip -c | ssh "$2" "gunzip -c | mysql $1"
fi
For a single DB, Taking backup from a remote server is :
mysqldump -u<user> -p<pwd> -h<remote-host> [database-name] > dump.sql
Restore is:
mysql -u<user> -p<pwd> -h<remote-host> [database-name] < dump.sql
more details about options of mysqldump are available here:
https://dev.mysql.com/doc/refman/8.0/en/mysqldump.html
Is it posible to duplicate an entire MySQL database on a linux server?
I know I can use export and import but the original database is >25MB so that's not ideal.
Is it possible using mysqldump or by directly duplicates the database files?
First create the duplicate database:
CREATE DATABASE duplicateddb;
Make sure the user and permissions are all in place and:
mysqldump -u admin -p originaldb | mysql -u backup -pPassword duplicateddb;
To remote server
mysqldump mydbname | ssh host2 "mysql mydbcopy"
To local server
mysqldump mydbname | mysql mydbcopy
I sometimes do a mysqldump and pipe the output into another mysql command to import it into a different database.
mysqldump --add-drop-table -u wordpress -p wordpress | mysql -u wordpress -p wordpress_backup
Create a mysqldump file in the system which has the datas and use pipe to give this mysqldump file as an input to the new system. The new system can be connected using ssh command.
mysqldump -u user -p'password' db-name | ssh user#some_far_place.com mysql -u user -p'password' db-name
no space between -p[password]
Making a Copy of a Database
# mysqldump -u root -p password db1 > dump.sql
# mysqladmin -u root -p password create db2
# mysql -u root -p password db2 < dump.sql
Here's a windows bat file I wrote which combines Vincent and Pauls suggestions. It prompts the user for source and destination names.
Just modify the variables at the top to set the proper paths to your executables / database ports.
:: Creates a copy of a database with a different name.
:: User is prompted for Src and destination name.
:: Fair Warning: passwords are passed in on the cmd line, modify the script with -p instead if security is an issue.
:: Uncomment the rem'd out lines if you want script to prompt for database username, password, etc.
:: See also: http://stackoverflow.com/questions/1887964/duplicate-entire-mysql-database
#set MYSQL_HOME="C:\sugarcrm\mysql\bin"
#set mysqldump_exec=%MYSQL_HOME%\mysqldump
#set mysql_exec=%MYSQL_HOME%\mysql
#set SRC_PORT=3306
#set DEST_PORT=3306
#set USERNAME=TODO_USERNAME
#set PASSWORD=TODO_PASSWORD
:: COMMENT any of the 4 lines below if you don't want to be prompted for these each time and use defaults above.
#SET /p USERNAME=Enter database username:
#SET /p PASSWORD=Enter database password:
#SET /p SRC_PORT=Enter SRC database port (usually 3306):
#SET /p DEST_PORT=Enter DEST database port:
%MYSQL_HOME%\mysql --user=%USERNAME% --password=%PASSWORD% --port=%DEST_PORT% --execute="show databases;"
#IF NOT "%ERRORLEVEL%" == "0" GOTO ExitScript
#SET /p SRC_DB=What is the name of the SRC Database:
#SET /p DEST_DB=What is the name for the destination database (that will be created):
%mysql_exec% --user=%USERNAME% --password=%PASSWORD% --port=%DEST_PORT% --execute="create database %DEST_DB%;"
%mysqldump_exec% --add-drop-table --user=%USERNAME% --password=%PASSWORD% --port=%SRC_PORT% %SRC_DB% | %mysql_exec% --user=%USERNAME% --password=%PASSWORD% --port=%DEST_PORT% %DEST_DB%
#echo SUCCESSFUL!!!
#GOTO ExitSuccess
:ExitScript
#echo "Failed to copy database"
:ExitSuccess
Sample output:
C:\sugarcrm_backups\SCRIPTS>copy_db.bat
Enter database username: root
Enter database password: MyPassword
Enter SRC database port (usually 3306): 3308
Enter DEST database port: 3308
C:\sugarcrm_backups\SCRIPTS>"C:\sugarcrm\mysql\bin"\mysql --user=root --password=MyPassword --port=3308 --execute="show databases;"
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sugarcrm_550_pro |
| sugarcrm_550_ce |
| sugarcrm_640_pro |
| sugarcrm_640_ce |
+--------------------+
What is the name of the SRC Database: sugarcrm
What is the name for the destination database (that will be created): sugarcrm_640_ce
C:\sugarcrm_backups\SCRIPTS>"C:\sugarcrm\mysql\bin"\mysql --user=root --password=MyPassword --port=3308 --execute="create database sugarcrm_640_ce;"
C:\sugarcrm_backups\SCRIPTS>"C:\sugarcrm\mysql\bin"\mysqldump --add-drop-table --user=root --password=MyPassword --port=3308 sugarcrm | "C:\sugarcrm\mysql\bin"\mysql --user=root --password=MyPassword --port=3308 sugarcrm_640_ce
SUCCESSFUL!!!
This won't work for InnoDB.
Use this workaround only if you are trying to copy MyISAM databases.
If locking the tables during backup, and, possibly, pausing MySQL during the database import is acceptable, mysqlhotcopy may work faster.
E.g.
Backup:
# mysqlhotcopy -u root -p password db_name /path/to/backup/directory
Restore:
cp /path/to/backup/directory/* /var/lib/mysql/db_name
mysqlhotcopy can also transfer files over SSH (scp), and, possibly, straight into the duplicate database directory.
E.g.
# mysqlhotcopy -u root -p password db_name /var/lib/mysql/duplicate_db_name
This worked for me with command prompt, from OUTSIDE mysql shell:
# mysqldump -u root -p password db1 > dump.sql
# mysqladmin -u root -p password create db2
# mysql -u root -p password db2 < dump.sql
This looks for me the best way. If zipping "dump.sql" you can symply store it as a compressed backup. Cool! For a 1GB database with Innodb tables, about a minute to create "dump.sql", and about three minutes to dump data into the new DB db2.
Straight copying the hole db directory (mysql/data/db1) didn't work for me, I guess because of the InnoDB tables.
For me the following lines of code did the trick
mysqldump --quote-names -q -u username1 --password='password1' originalDB | mysql -u username2 --password='password2' duplicateDB
Once upon a time in MySQL you could just copy all the table files to another directory in the mysql tree
mysql cli - create database db2
linux cli - cp db1 db2