A-A+
mysql双主配置
一、先修改配置文件
服务器A(192.168.1.254)配置如下
log-bin = mysql-bin server-id = 1 expire-logs-days = 100 replicate-do-db = test binlog-ignore-db = mysql binlog-ignore-db = information_schema auto-increment-increment = 2 auto-increment-offset = 1服务器B(192.168.1.252)配置
log-bin = mysql-bin server-id = 2 expire-logs-days = 100 replicate-do-db = test binlog-ignore-db = mysql binlog-ignore-db = information_schema auto-increment-increment = 2 auto-increment-offset = 2两台服务器都重启
mysql> service mysqld restart注:二都只有server-id不同和 auto-increment- offset不同 auto-increment-offset是用来设定数据库中自动增长的起点的,回为这两能服务器都设定了一次自动增长值2,所以它们的起点必须得不同,这样才能避免两台服务器数据同步时出现主键冲突 replicate-do-db 指定同步的数据库,我们只在两台服务器间同步test数据库 另:auto-increment-increment的值应设为整个结构中服务器的总数,本案例用到两台服务器,所以值设为2 二、同步数据 本文是用test做的实验,导出将test.sql文件从254服务器拷贝到252服务器 备份数据前先锁表,保证数据一致性
mysql> FLUSH TABLES WITH READ LOCK; # mysqldump -uroot -p123456 test> /tmp/test.sql; mysql> UNLOCK TABLES; scp /tmp/test.sql root@192.168.1.252:/tmp三、相互授权用户(在A服务器授权一个允许B访问的用户,反之亦然) 在服务器A(192.168.1.254)上
mysql> GRANT REPLICATION SLAVE ON *.* TO 'mysync'@'192.168.1.252' IDENTIFIED BY PASSWORD '123456'; mysql> flush privileges;在服务器B(192.168.1.252)上
mysql> GRANT REPLICATION SLAVE ON *.* TO 'mysync'@'192.168.1.254' IDENTIFIED BY PASSWORD '123456'; mysql> flush privileges;四、互告bin-log信息 在服务器A(192.168.1.254)
mysql> show master status; +------------------+----------+--------------+--------------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+--------------------------+ | mysql-bin.000006 | 106 | | mysql,information_schema | +------------------+----------+--------------+--------------------------+在服务器B(192.168.1.252)
mysql> show master status; +------------------+----------+--------------+--------------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+--------------------------+ | mysql-bin.000008 | 192 | | mysql,information_schema | +------------------+----------+--------------+--------------------------+在A服务器(192.168.1.254)上执行
mysql> change master to master_host='192.168.1.252',master_user='mysync',master_password='123456',master_log_file='mysql-bin.000008',master_log_pos=192;在B服务器(192.168.1.252)上执行
mysql> change master to master_host='192.168.1.254',master_user='mysync',master_password='123456',master_log_file='mysql-bin.000006',master_log_pos=106;五、在两服务器都执行以下命令
mysql> start slave;六、查看状态
mysql> show slave status\GA服务器(192.168.1.254)状态如下:
Slave_IO_State: Waiting for master to send event Master_Host: 192.168.1.252 Master_User: mysync Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000008 Read_Master_Log_Pos: 192 Relay_Log_File: mysqld-relay-bin.000009 Relay_Log_Pos: 337 Relay_Master_Log_File: mysql-bin.000008 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: testB服务器(192.168.1.252)状态如下:
Slave_IO_State: Waiting for master to send event Master_Host: 192.168.1.254 Master_User: mysync Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000006 Read_Master_Log_Pos: 106 Relay_Log_File: mysqld-relay-bin.000014 Relay_Log_Pos: 251 Relay_Master_Log_File: mysql-bin.000006 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: test当看到了两个yes,即: Slave_IO_Running: Yes Slave_SQL_Running: Yes 说明已经配置成功了