Migrate Database from Old Server
You can use the full backup automatically made by the "db-backup.sh" script (/bak/mysql/db-all.sql) if you have that already running or you can create a manual backup. The steps below will document how to do it manually but you could just as easily use your existing backup file(s).
Here are the steps to import the databases from the old server into the new server:
- On the old server, make a full backup of the current databases by typing: mysqldump --all-databases > /tmp/db-all.sql
- Transfer db-all.sql to the new server (via WinSCP or other means)
- On the new server, make a full backup of the current databases by typing: mysqldump --all-databases > /tmp/db-all-before.sql
- Import the db-all.sql file by typing: mysql < /tmp/db-all.sql
- Make a full backup of the current databases by typing: mysqldump --all-databases > /tmp/db-all-after.sql
- Verify that your databases, tables, rows and users are now on the new server
- Archive the /tmp/*.sql files for safe-keeping until you no longer need them. If you leave them in /tmp, they will be automatically deleted upon next reboot.
- Now would be a good time to run your backup script: /var/scripts/prod/db-backup.sh
Now you can point your apps to the new database server or shutdown the old server and then change the IP of this server to match the old server.
NOTE: Saving the following lines in case I expand this further by showing how to extract an archive 1st and then restore from that.
- Make a restore folder by typing: mkdir /tmp/restore
- Extract the archive by typing: 7za x -so -w/tmp /tmp/*.7z | tar -C /tmp/restore --strip-components=3 -xf -
NOTE: If you see the following error in the syslog, especially after doing an "apt-get upgrade" and noticing the mysqld service stopped, you need to simply run "mysql_upgrade" to fix the problem. Reference Bug
Code:
Jun 1 14:25:48 srv-mysql mysqld_safe[23145]: ERROR: 1136 Column count doesn't match value count at row 1
Jun 1 14:25:48 srv-mysql mysqld_safe[23145]: 2017-06-01 14:25:48 140594627692800 [ERROR] Aborting
Jun 1 14:25:50 srv-mysql mysqld_safe[23145]: Installation of system tables failed!
Migrating Individual Databases from Old Server
If you export one database at a time, it will not include the grants. You will need to export the grants and then import the database, then import the grants on the new server.
Example: Let us assume the database we want to migrate is called "mydb"
On the old server:
Code:
mysqldump --databases mydb > /tmp/mydb.sql
mysql --skip-column-names -A -e"SELECT CONCAT('SHOW GRANTS FOR ''',user,'''@''',host,''';') FROM mysql.user WHERE user<>''" | mysql --skip-column-names -A | sed 's/$/;/g' > /tmp/mydb-grants.sql
Now edit the mydb-grants.sql file and delete everything not related to the DB you want.
Copy both .sql files to the new server and then execute the following commands:
Code:
mysql < /tmp/mydb.sql
mysql < /tmp/mydb-grants.sql
Restoring a database without generating a log
If you are restoring/migrating a huge database and do not want a log generated for the import, you can temporarily disable logging for your session as follows:
Code:
mysql
SET sql_log_bin = 0;
source /tmp/mydb.sql
exit
Bookmarks