Search code examples
mysqldatabaseclone

Clone MySQL database when had read-only permission


I have database1 (~500MB), which can be access in a read-only mode by user1.
I have empty database database2, with full privilege by user2.

Now, I want to clone database1 into database2.

I have tried mysqldump, but it says access denied.

Any better way? Thanks.


Solution

  • By default, mysqldump tries to lock all tables before dumping them - in order to create a consistent snapshot. I assume that user1 does not have the privilege to do that.

    You can use either --single-transaction or --skip-lock-tables on the command line. The first option uses a single transaction for the whole operation. This works perfectly if all tables are transactional and the database is small. If the database is huge, this can have serious effects on other applications.

    If in doubt, try --skip-lock-tables first. However, the dump may be inconsistent, if other applications make changes during the dump.

    Complete example to clone database:

    mysqldump -h host1 -u user1 -ppass1 --single-transaction database1 |
    mysql -h host2 -u user2 -ppass2 database2