Move MySQL database to a new server

前端 未结 3 1289
孤独总比滥情好
孤独总比滥情好 2021-01-04 06:43

What\'s the easiest way to move mysql schemas (tables, data, everything) from one server to another?

Is there an easy method move all this from one server running my

相关标签:
3条回答
  • 2021-01-04 07:12

    Dump the Database either using mysqldump or if you are using PHPMyAdmin then Export the structure and data.

    For mysqldump you will require the console and use the following command:

    mysqldump -u <user> -p -h <host> <dbname> > /path/to/dump.sql

    Then in the other server:

    mysql -u <user> -p <dbname> < /path/to/dump.sql

    0 讨论(0)
  • 2021-01-04 07:18

    If you're moving from the same architecture to the same architecture (x86->x86, x86_64 -> x86_64), you can just rsync your MySQL datadir from one server to the other. Obviously, you should not run this while your old MySQL daemon is running.

    If your databases are InnoDB-based, then you will want to make sure that your InnoDB log files have been purged and their contents merged to disk before you copy files. You can do this by setting innodb_fast_shutdown to 0 (the default is 1, which will not flush the logs to disk), which will cause the log file to be flushed on the next server shutdown. You can do this by logging on to MySQL as root, and in the MySQL shell, do:

    SET GLOBAL innodb_fast_shutdown=0

    Or by setting the option in your my.cnf and restarting the server to pull in the change, then shutting down to flush the log.

    Do something like:

    #On old server (notice the ending slash and lack thereof, it's very important)
    rsync -vrplogDtH /var/mysql root@other.server:/var/mysql/
    #Get your my.cnf
    scp /etc/my.cnf root@other.server:/etc/my.cnf
    

    After that you might want to run mysql_upgrade [-p your_root_password] to make sure the databases are up-to-date.

    I will say it's worked for me in the (very recent) past (moving from an old server to a new one, both running FreeBSD 8.x), but YMMV depending on how many versions you were in the past.

    0 讨论(0)
  • 2021-01-04 07:22

    If you are using SSH keys:

    $ mysqldump --all-databases -u[user] -p[pwd] | ssh [host/IP] mysql -u[user] -p[pwd]
    

    If you are NOT using SSH keys:

    $ mysqldump --all-databases -u[user] -p[pwd] | ssh user@[host/IP] mysql -u[user] -p[pwd]
    

    WARNING: You'll want to clear your history after this to avoid anyone finding your passwords.

    $ history -c
    
    0 讨论(0)
提交回复
热议问题