To set up a master/slave database, perform the following steps from the command line.
bind-address = 127.0.0.1
This IP address will be set to whichever IP address the master server uses.
server-id
, which is most probably located at /etc/my.cn
or /etc/mysql/mysql.conf.d/mysqld.cnf
.sed
command can perform this easily:$ sed -i s/#server-id/server-id/g /etc/mysql/my.cnf
server-id
:$ sed -i s/#log_bin/log_bin/g /etc/mysql/my.cnf
$ sudo service mysql restart
MYSQLUSER MYSQLPASSWORD MASTERDATABASE MASTERDATABASEUSER MASTERDATABASEPASSWORD SLAVEDATABASE SLAVEDATABASEUSER SLAVEDATABASEPASSWORD
The following are the steps for setting up a master server:
$ echo "GRANT REPLICATION SLAVE ON *.* TO 'DATABASEUSER'@'%' IDENTIFIED BY 'DATABASESLAVEPASSWORD';" | mysql -u MYSQLUSER -p"MYSQLPASSWORD"
$ echo "FLUSH PRIVILEGES;" | mysql -u MYSQLUSER -p"MYSQLPASSWORD"
$ echo "USE MASTERDATABASE;" | mysql -u MYSQLUSER -p"DATABASEPASSWORD"
$ echo "FLUSH TABLES WITH READ LOCK;" | mysql -u MYSQLUSER -p"MYSQLPASSWORD"
$ echo "SHOW MASTER STATUS;" | mysql -u MYSQLUSER -p"MYSQLPASSWORD"
Take note of the position and filename from the output:
POSITION FILENAME
$ mysqldump -u root -p"MYSQLPASSWORD" --opt "MASTERDATABASE" > dumpfile.sql
$ echo "UNLOCK TABLES;" | mysql -u MYSQLUSER -p"MYSQLPASSWORD"
The following are the steps for setting up a slave server:
$ echo "CREATE DATABASE SLAVEDATABASE;" | mysql -u MYSQLUSER -p"MYSQLPASSWORD"
$ mysql -u MYSQLUSER -p"MYSQLPASSWORD" "MASTERDATABASE" < dumpfile.sql
server-id = 2
#log_bin = /var/log/mysql/mysql-bin.log expire_logs_days = 10 max_binlog_size = 100M #binlog_do_db = include_database_name
log_bin = /var/log/mysql/mysql-bin.log expire_logs_days = 10 max_binlog_size = 100M binlog_do_db = include_database_name
binglog_do_db
:relay-log = /var/log/mysql/mysql-relay-bin.log
$ sudo service mysql restart
MASTER_PASSWORD='password', MASTER_LOG_FILE='FILENAME', MASTER_LOG_POS= POSITION;