8

Let's say that I have database1 and database2.

database1 - contains data

database2 - is empty.


I want to copy all data from database1 to database2 via SSH - duplicate database1.

What command should I use?


I have tried

mysqldump -u user -p database1 > database1.sql
Enter password: mysqldump: Got error: 1045: Access denied for user 'user'@'localhost' (using password: NO) when trying to connect
2
  • 1
    Googling mysql duplicate database gives me everything necessary, e.g. rubyrobot.org/article/duplicate-a-mysql-database Commented Jan 15, 2012 at 11:58
  • 1
    The error you're getting indicates that you've submitted the wrong password. Commented Jan 15, 2012 at 15:54

4 Answers 4

7

Duplicate a MySQL database over SSH in just one command:

mysqldump -u <local-user> -p <local-db> | gzip | ssh user@hostname \
  "gunzip | mysql -u <remote-user> -p<password> <remote-db>"

Note that you must create the remote database first.

More advanced version:

mysqldump -u <local-user> -p <local-db> | xz | pv -W | ssh user@hostname \
"tee remote-dump.sql.xz | unxz | mysql -u <remote-user> -p<password> <remote-db>"

The advanced version:

  • has better compression using xz/unxz (Though take care that compression speed doesn't become a bottleneck - if xz is at 100% CPU then it has probably become a bottleneck and you might be better off with gzip)

  • shows a progress indicator using pv

  • saves a copy of the dump using tee

Only one problem I haven't solved is how to avoid specifying the password in the remote command. It would be really nice to be able to enter this password interactively on the command line – if anyone knows how, please chime in.

Sign up to request clarification or add additional context in comments.

2 Comments

One way to handle remote password is by using a option file on the remote server e.g. in ~/.my.cnf (see dev.mysql.com/doc/refman/5.7/en/password-security-user.html).
I think that the gzip/xz, etc, unnecessarily complicate this, and, you may not benefit from compression depending on circumstances. I use "ssh -C" if I think compression's needed. Here's the barebones version that I typically use: $ ssh remotehost mysql -e "'create database dbname'" $ mysqldump dbname | ssh remotehost mysql dbname Note that the silly double quotes are needed, one pair for each host involved. Also note that I have the needed bits in my ~/.my.cnf file to not require putting username/passwd on the command line.
5

This will copy database from S1 to S2

mysqldump --opt <database> | gzip -c | ssh user@wherever 'cat > /tmp/yourfile.sql.gz'

Unzip file

gunzip /tmp/yourfile.sql.gz

Next you'll have to import on S2

mysql -h<host> -u<user> -p<password> < /tmp/yourfile.sql

enjoy

3 Comments

What is "cat"? Should I replace it or what?
cat is a linux command for display on screen content of a file (try to read manuals: man cat )
basically there is no command to duplicate a database on separate servers. this action is made from several commands.
1

Use the following 2 comands:

mysqldump -u customers -p customers > customers.dump



mysql -u new_customers -p new_customers < customers.dump

(you may get access denied because you can not write to the directory so you may have to change directory using cd)

source: http://support.tigertech.net/mysql-duplicate

Comments

0

First thing you need to do is to ssh into server from which you want to duplicate or copy the database to another server.

The command which worked for me was:

mysqldump -u <database-user> -p<database-password> <database-name> | xz | ssh <target-ssh-username>@<target-hostname> \ "tee remote-dump.sql.xz | unxz | mysql -u <target-database-user> -p<target-database-password> <target-database-name>"

Note:- you can use .cnf file to store username and password of the database. Because my execution was small, I didn't used it.
Link for doing that: https://www.techiecorner.com/1619/how-to-setup-mysqldump-without-password-in-cronjob/

After entering your command. It will prompt you for

**The authenticity of host 'w01b3a99.kasserver.com (85.13.164.141)' can't be established.**

Are you sure you want to continue connecting (yes/no)?

type yes

Then it will prompt you to enter

*target-user@target-hostname* ssh password

Enter the password of target-user password

It will be silent execution running. After some time your command execution will be completed.

I haven't used pv -W because I was using the Cloudways Server. pv wasn't installed already. They don't allow root access to install any package.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.