Migrate/Move MySQL_db

Migrate/Move MySQL Database And Users To New Server

Migrate user accounts from old Linux / UNIX server to a new server including mails and home directories. However, in reality you also need to move MySQL database which may host your blog, forum or just your data stored in MySQL database. The mysqldump command will only export the data and the table structure but it will not include a users grants and privileges. The main function of the MySQL privilege system (which is stored in mysql.user table) is to authenticate a user who connects from a given host and to associate that user with privileges on a database such as SELECT, INSERT, UPDATE, and DELETE.

Our Sample Setup

| db1 | —————————;
+—–+| |
old mysql server |
( |
+—–+ ///////////////////////////////
| db2 | ———————-// Internet (ISP router //
+—–+ // with port 80 forwarding) //
new mysql server ///////////////////////////////
( |
+—–+ |
| www1| —————————-;
Apache web server
You need to move db1 server database called blogdb and its users to db2 server.

Install MySQL On DB2

Use the apt-get or yum command to install mysql on DB2 server:
$ sudo apt-get install mysql-server mysql-client
$ sudo service mysql start
# set root password for new installation
$ mysqladmin -u root password NEWPASSWORD
$ sudo yum install mysql-server mysql
$ sudo chkconfig mysql on
$ sudo service mysql start
# set root password for new installation
$ mysqladmin -u root password NEWPASSWORD
Make sure OpenSSH server is also installed on DB2.

Get Current MySQL, Usernames, Hostname, And Database Names

Type the following command at shell prompt to list username and hostname list, enter:

mysql -u root -B -N -p -e “SELECT user, host FROM user” mysql
Sample outputs:

root localhost
root db1.ab.testdb.in
The first column is mysql username and second one is network host names. Now, type the following command to get exact details about grants and password for each user from above list:

mysql -u root -p -B -N -e”SHOW GRANTS FOR ‘userName’@hostName”
mysql -u root -p -B -N -e”SHOW GRANTS FOR ‘vivek’@″
Sample outputs:

GRANT USAGE ON *.* TO ‘colin’@’’ IDENTIFIED BY PASSWORD ‘somePasswordMd5’
GRANT ALL PRIVILEGES ON `blog_db`.* TO ‘colin’@’′

colin – MySQL login username – Another server or workstation to access this mysql server
somePasswordMd5 – Password stored in mysql database which is not in a clear text format
blog_db – Your database name
Now, you’ve all info and you can move database and users to a new server called db2 as follows using the combination of OpenSSH ssh client and mysql clients as follows:

ssh user@db2 mysql -u root -p’password’ -e “create database IF NOT EXISTS blog_db;”
ssh user@db2 mysql -u root -p’password’ -e “GRANT USAGE ON *.* TO ‘colin’@’’ IDENTIFIED BY PASSWORD ‘somePasswordMd5′;”
ssh user@db2 mysql -u root -p’password’ -e “GRANT ALL PRIVILEGES ON `blog_db`.* TO ‘vivek’@’′;”
mysqldump -u root -p’password’ -h ‘localhost’ blogdb | ssh user@db2 mysql -u root -p’password’ blogdb
You can test it as follows from Apache web server:
$ mysql -u colin -h -p blog_db -e ‘show tables;’

A Note About Web Applications

Finally, you need to make changes to your application to point out to new a database server called DB2. For example, change the following from:

$DB_SERVER = “db1.ab.testdb.in”;
$DB_USER = “colin”;
$DB_PASS = “your-password”;
$DB_NAME = “blog_db”;


$DB_SERVER = “db2.ab.testdb.in”;
$DB_USER = “colin”;
$DB_PASS = “your-password”;
$DB_NAME = “blog_db”;

A Sample Shell Script To Migrate Database

# This script is licensed under GNU GPL version 2.0 or above
# ————————————————————
# SETME First – local mysql user/pass

# SETME First – remote mysql user/pass

# SETME First – remote mysql ssh info
# Make sure ssh keys are set

# sql file to hold grants and db info locally

#### No editing below #####

# Input data

# Die if no input given
[ $# -eq 0 ] &# Make sure you can connect to local db server
mysqladmin -u “$_lusr” -p”$_lpass” -h “$_lhost” ping &>/dev/null || { echo “Error: Mysql server is not online or set correct values for _lusr, _lpass, and _lhost”; exit 2; }

# Make sure database exists
mysql -u “$_lusr” -p”$_lpass” -h “$_lhost” -N -B -e’show databases;’ | grep -q “^${_db}$” || { echo “Error: Database $_db not found.”; exit 3; }

##### Step 1: Okay build .sql file with db and users, password info ####
echo “*** Getting info about $_db…”
echo “create database IF NOT EXISTS $_db; ” > “$_tmp”

# Build mysql query to grab all privs and user@host combo for given db_username
mysql -u “$_lusr” -p”$_lpass” -h “$_lhost” -B -N \
-e “SELECT DISTINCT CONCAT(‘SHOW GRANTS FOR ”’,user,”’@”’,host,”’;’) AS query FROM user” \
mysql \
| mysql -u “$_lusr” -p”$_lpass” -h “$_lhost” \
| grep “$_user” \
| sed ‘s/Grants for .*/#### &/’ >> “$_tmp”

##### Step 2: send .sql file to remote server ####
echo “*** Creating $_db on ${rsshhost}…”
scp “$_tmp” ${_rsshusr}@${_rsshhost}:/tmp/

#### Step 3: Create db and load users into remote db server ####
ssh ${_rsshusr}@${_rsshhost} mysql -u “$_rusr” -p”$_rpass” -h “$_rhost” < "$_tmp"

#### Step 4: Send mysql database and all data ####
echo "*** Exporting $_db from $HOSTNAME to ${_rsshhost}…"
mysqldump -u "$_lusr" -p"$_lpass" -h "$_lhost" "$_db" | ssh ${_rsshusr}@${_rsshhost} mysql -u -u "$_rusr" -p"$_rpass" -h "$_rhost" "$_db"

rm -f "$_tmp"

How Do I Use This Script?

Download the above script and edit it to set the following as per your setup:

# SETME First – local mysql DB1 admin user/password

# SETME First – remote mysql DB2 admin user/password

# Remote SSH Server (DB2 SSH Server)
# Make sure ssh keys are set
In this example, migrate a database called wiki with wikiuser username:
$ ./script.sh wiki wikiuser


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s