1.A服务器上创建数据库并导入数据库
[[email protected] /]# mysql -u root -p
mysql> show databases;
mysql> create database cdn;
mysql> use cdn;
2.B服务器上创建数据库并导入数据库。
[[email protected] /]# mysql -u root -p
mysql> show databases;
mysql> create database cdn;
mysql> use cdn;
3.在A和B服务器上,增加一个用户账号(backup)作为同步用户账号.
A: mysql> Grant replication slave on *.* to [email protected]‘10.16.0.247‘ identified by ‘1234‘;
B: mysql> Grant replication slave on *.* to [email protected]‘10.16.0.246‘ identified by ‘1234‘;
4.在B服务器上用如下命令测试backup用户能连上A服务器
[[email protected] /]# mysql -u backup -p -h 10.16.0.246
5.停掉A和B服务器上的mysql服务。
6.A 服务器
[[email protected] /]#vi /etc/my.cnf
对文件进行编辑或修改:
server-id=1
log-bin=mysql-bin
binlog-do-db=cdn
重起数据库服务。
B 服务器
[[email protected] /]#vi /etc/my.cnf
server-id=2
master-host=10.16.0.246
master-user=backup
master-password=1234
master-port=3306
master-connect-retry=60
replicate-do-db=cdn
7.B 服务器
[[email protected] /]#vi /etc/my.cnf
对文件进行编辑或修改:
log-bin=mysql-bin
binlog-do-db=cdn
A 服务器
[[email protected] /]#vi /etc/my.cnf
master-host=10.16.0.247
master-user=backup
master-password=1234
master-port=3306
master-connect-retry=60
replicate-do-db=cdn
8.启动A、B服务器mysql服务。
9.A上输入show master status;
mysql> show master status;
+---------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-----------------+----------+--------------+------------------+
| mysql-bin.000008 | 106 | cdn | manual,mysql |
+-----------------+----------+--------------+------------------+
记录File 和 Position 项目的值mysql-bin.000008和106。
B上输入show master status;
mysql> show master status;
+---------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-----------------+----------+--------------+------------------+
| mysql-bin.000008 | 106 | cdn | manual,mysql |
+-----------------+----------+--------------+------------------+
记录File 和 Position 项目的值mysql-bin.000008和106。
*两个服务器File名可以不一样,现在两边的mysql服务就不要重启了 否则File名会改变。
10.在A服务器上,停止slave的服务,并配置连接B服务器参数。
mysql> slave stop;
mysql> CHANGE MASTER TO
-> MASTER_HOST="10.16.0.247",
-> MASTER_USER="backup",
-> MASTER_PASSWORD="1234",
-> MASTER_LOG_FILE="mysql-bin.000008",
-> MASTER_LOG_POS=106;
开启slave的服务
mysql> slave start;
在B服务器上,停止slave的服务,并配置连接A服务器参数。
mysql> slave stop;
mysql> CHANGE MASTER TO
-> MASTER_HOST="10.16.0.246",
-> MASTER_USER="backup",
-> MASTER_PASSWORD="1234",
-> MASTER_LOG_FILE="mysql-bin.000008",
-> MASTER_LOG_POS=106;
开启slave的服务
mysql> slave start;
11.在A、B服务器上输入 show slave status\G 查看状态。
Slave_IO_Running: Yes 必须YES
Slave_SQL_Running: Yes 必须YES