首页 > 代码库 > mysql互为主从复制配置笔记--未读,稍后学习
mysql互为主从复制配置笔记--未读,稍后学习
MySQL-master1:192.168.72.128
MySQL-master2:192.168.72.129
OS版本:CentOS 5.4
MySQL版本:5.5.9(主从复制的master和slave服务器中的mysql版本相同或者master版本高于slave版本)
一、MySQL master-master配置
1、修改MySQL配置文件
两台MySQL均如要开启binlog日志功能,开启方法:在MySQL-master1 配置文件/etc/my.cnf
[MySQLd]段中加上
user = mysql
log-bin=mysql-bin
server-id = 1
binlog-do-db=test
binlog-ignore-db=mysql
replicate-do-db=test
replicate-ignore-db=mysql
log-slave-updates
slave-skip-errors=all
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=1
在MySQL-master2 配置文件/etc/my.cnf
[MySQLd]段中加上
user = mysql
log-bin=mysql-bin
server-id= 2
binlog-do-db=test
binlog-ignore-db=mysql
replicate-do-db=test
replicate-ignore-db=mysql
log-slave-updates
slave-skip-errors=all
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=2
将两个配置文件保存,分别重启mysql服务器
server_id值必须为2到232–1之间的一个正整数值。ID值唯一的标识了复制群集中的主从服务器,因此它们必须各不相同。
binlog-do-db=database 是要记录日志的数据库;
同步多个数据库重复设置选项binlog-do-db=test 和replicate-do-db=test
例如
binlog-do-db=test1
replicate-do-db=test1
binlog-do-db=test2
replicate-do-db=test2
binlog-ignore-db 是不要记录日志的数据库名,多个数据库中间用逗号(,)隔开;
mysql配置文件my.cnf中 log-slave-updates表示 如果一个MASTER 挂掉的话,另外一个马上接管。
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=1 指的是服务器频繁的刷新日志。这个保证了在其中一台挂掉的话,日志刷新到另外一台。从而保证了数据的同步 。
auto_increment_offset = 1
auto_increment_increment = 2
这样A的auto_increment字段产生的数值是:1, 3, 5, 7, …等奇数ID了
auto_increment_offset = 2
auto_increment_increment = 2
这样B的auto_increment字段产生的数值是:2, 4, 6, 8, …等偶数ID了
你的auto_increment字段在不同的服务器之间绝对不会重复,所以Master-Master结构就没有任何问题了。当然,你还可以使用3 台,4台,或者N台服务器,只要保证auto_increment_increment = N 再设置一下auto_increment_offset为适当的初始值就可以了,那样,我们的MySQL可以同时有几十台主服务器,而不会出现自增长ID 重复。
2、将192.168.72.128设为192.168.72.129的主服务器
在192.168.72.128上新建授权用户
MySQL> grant replication slave on *.* to ‘replication‘@‘%‘ identified by ‘replication‘;
Query OK, 0 rows affected (0.00 sec)
MySQL>flush privileges;
Query OK, 0 rows affected (0.00 sec)
MySQL> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| MySQL-bin.000003 | 374 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
------------------------------------------------------------------------------
在192.168.72.129将192.168.72.128为自己的主服务器
MySQL> change master to master_host=‘192.168.72.128‘,master_user=‘replication‘,master_password=‘replication‘,master_log_file=‘MySQL-bin.000003‘,master_log_pos=374;
Query OK, 0 rows affected (0.05 sec)
MySQL> start slave;
Query OK, 0 rows affected (0.00 sec)
MySQL> show slave status/G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.72.128
Master_User: replication
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: MySQL-bin.000003
Read_Master_Log_Pos: 374
Relay_Log_File: MySQL-master2-relay-bin.000002
Relay_Log_Pos: 235
Relay_Master_Log_File: MySQL-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 374
Relay_Log_Space: 235
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
1 row in set (0.00 sec)
红色标注部分为192.168.72.128主机中show master status命令结果中的file postion两个值
3、将192.168.72.129设为192.168.72.128的主服务器
在192.168.72.129上新建授权用户
MySQL> grant replication slave on *.* to ‘replication‘@‘%‘ identified by ‘replication‘;
Query OK, 0 rows affected (0.00 sec)
MySQL>flush privileges;
Query OK, 0 rows affected (0.00 sec)
MySQL> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| MySQL-bin.000003 | 374 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
----------------------------------------------------------------------------------
在192.168.72.128将192.168.72.129为自己的主服务器
MySQL> change master to master_host=‘192.168.72.129‘,master_user=‘replication‘,master_password=‘replication‘,master_log_file=‘MySQL-bin.000003‘,master_log_pos=374;
Query OK, 0 rows affected (0.05 sec)
MySQL> start slave;
Query OK, 0 rows affected (0.00 sec)
MySQL> show slave status/G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.72.129
Master_User: replication
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: MySQL-bin.000003
Read_Master_Log_Pos: 374
Relay_Log_File: MySQL-master2-relay-bin.000002
Relay_Log_Pos: 235
Relay_Master_Log_File: MySQL-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 374
Relay_Log_Space: 235
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
1 row in set (0.00 sec)
如果主数据库有数据的话
数据库锁表操作,不让数据再进行写入动作。mysql> FLUSH TABLES WITH READ LOCK ;
用命令mysqldump
备份数据库。
在master服务器执行
shell> mysqldump -uroot -p123456 --master-data --opt test1 > backup-file.sql
看主数据库的状态mysql> show master status;
记录File 和Position 项的值
注:由于没有锁定主服务器,这里记录的主服务器二进制日志position值可能会大于做mysqldump时的值,这将导致从服务器丢失在此期间的更新。如果可以保证在此期间主服务器不会出现创建新表的更新,那么丢失的影响不大;否则,将导致从服务器复制线程失败,这时必须在做mysqldump时锁定主服务器。
-----------------------------------------------
从服务器中my.cnf文件中加入选项
skip-slave-start
skip-slave-start 表示从mysql服务器启动时不启动同步线程,这就要在启动从服务器之后,手工启动同步线程,在mysql> 提示符下面运行“start slave”就可以
保存my.cnf后
执行
shell> mysqladmin -uroot -p123456 create test1
shell> mysql -uroot -p123456 test1 < backup-file.sql
启动从服务器线程
mysql>start slave;
取消主数据库锁定mysql>UNLOCK TABLES;
4、其他命令
1、查看复制进度
需要在主库上运行
mysql>show processlist \G;
2、主服务器上的相关命令:
show processlist;
show master status
show slave hosts
show {master|binary} logs
show binlog events
purge {master|binary} logs to ‘log_name‘
purge {master|binary} logs before ‘date‘
reset master(老版本flush master)
set sql_log_bin={0|1}
3、从服务器上的相关命令:
slave start
slave stop
slave stop IO_THREAD //此线程把master段的日志写到本地
slave start IO_THREAD
slave stop SQL_THREAD //此线程把写到本地的日志应用于数据库
slave start SQL_THREAD
reset slave
set global sql_slave_skip_counter
load data from master
show slave status(SUPER,REPLICATION CLIENT)
CHANGE MASTER TO MASTER_HOST=, MASTER_PORT=,MASTER_USER=, MASTER_PASSWORD= //动态改变master信息
PURGE MASTER [before ‘date‘] 删除master端已同步过的日志
=========================================================================
skip-slave-start
server-id = 1
log-bin=C:/Program Files/MySQL/MySQL Server 5.0/00/repbinlog
log-error=C:/Program Files/MySQL/MySQL Server 5.0/rep/rep.err
relay-log=C:/Program Files/MySQL/MySQL Server 5.0/rep/beltal_relay_log
sync_binlog=1
log-slave-updates
innodb_flush_log_at_trx_commit=1
binlog-do-db=sync
#slave
master-host = 192.168.1.144
master-user = replicate
master-password = replicate
master-port = 3306
master-connect-retry=60
replicate-do-db = sync
report-host=192.168.1.80
摘自 roockee的博客
mysql互为主从复制配置笔记--未读,稍后学习