配置
假设有两个数据库服务器192.168.0.1和192.168.0.2,后面用1表示168.0.1,2表示168.0.2
首先配置1
用root进入mysql添加一个授权2可以访问的用户
代码:
mysql>GRANT all privileges ON *.* TO tongbu@'192.168.0.2' IDENTIFIED BY '123456';
退出mysql编辑mysql配置文件
代码:
vi /etc/my.cnf
添加或修改以下内容:
代码:
[client]
default-character-set=utf8
[mysqld]
default-character-set=utf8
log-bin=mysql-bin
server-id=1
master-host=192.168.0.2
master-user=tongbu
master-password=123456
master-port=3306
master-connect-retry=30 #断网后间隔30秒重连
binlog-do-db=mysql #要备份的库
replicate-do-db=mysql
skip-name-resolve #快速登录跳过用户解析
back_log = 500
max_connect_errors = 100
max_connections=1000
重启mysql服务
代码:
service mysqld restart
至此1配置完毕,
然后配置2,
步骤同1
代码:
mysql>GRANT all privileges ON *.* TO tongbu@'192.168.0.1' IDENTIFIED BY '123456';
代码:
vi /etc/my.cnf
代码:
[client]
default-character-set=utf8
[mysqld]
default-character-set=utf8
server-id=2
master-host=192.168.0.1
master-user=tongbu
master-password=123456
master-port=3306
binlog-do-db=mysql
replicate-do-db=mysql
skip-name-resolve
back_log = 500
max_connections = 1000
max_connect_errors = 100
slave_skip_error=1062
sync_binlog=1 #每作一次操作同步一次
log-bin=mysql-bin
代码:
service mysqld restart
至此2也配置完毕
注意:2里面的配置有些不同是因为我把1做为生产库,2只做备份
最后进入1的mysql查看master状态
代码:
mysql>show master status;
显示如下:
代码:
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000019 | 11809679 | mysql | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
我们把file=mysql-bin.000019和position=11809679 记下来
然后进入2的mysql
代码:
mysql>stop slave;
代码:
mysql>change master to master_log_file='mysql-bin.000019',master_log_pos=11809679
代码:
mysql>slave start;
查看同步备份状态
代码:
mysql>show slave status\G
其中的
代码:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
都是Yes则已经处于正常同步状态了