网站有一个后台业务,叫searchEngine项目,基于Lucene 构建。主要提供索引构建和检索的功能,搜索引擎查询mysql 数据库然后根据数据状态来构建索引,这里采用的是 程序每隔一段时间主动轮询 mysql 查询 数据列 增删改的状态,对应的去增删改 Lucene 索引,然后会将索引的状态更新到数据列中,方便轮询的时候区分哪些是未索引的数据。
由于mysql主要采用myisam引擎,导致java程序构建索引 轮询数据库的时候,频繁锁表,页面查询的时候无法响应。这里做了下 mysql 主从同步,将所有业务上的更新和查找在 master 上进行,而Lucene后台服务操作slave库,同时也起到备份的作用。这里整理下做主从备份的一些配置。
主库 master:192.168.0.102,mysql 5.6.12,centos
从库 slave:192.168.0.100,mysql 5.5.13,centos
都是采用源码编译,安装过程可以查看我的这篇博文。master、slave 所有的数据表结构都一致。之前我做了一个双master 的配置,可以查看这里
这里的配置只截取了部分 同步需要的配置,其他的优化方面的暂不考虑
my.cnf:
[client] port=3306 [mysqld] socket=/usr/local/mysql/mysql.sock basedir=/usr/local/mysql datadir=/usr/local/mysql/data log-error=/usr/local/mysql/data/mysql_error.log pid-file=/usr/local/mysql/data/mysql.pid log-bin = /usr/local/mysql/mysql-bin.log #二进制文件必须开启 explicit_defaults_for_timestamp=true innodb_flush_log_at_trx_commit=2 #日志flush到磁盘的,2表示写入到缓存,提高性能,操作系统崩溃会丢失部分数据 #master server_id=1 expire_logs_days = 10 max_binlog_size = 1G binlog-do-db = webportal #同步数据库 binlog-ignore-db=information_schema binlog-ignore-db=performance_schema binlog-ignore-db=mysql binlog-ignore-db=test
创建同步用户,在主服务器上为从服务器建立一个连接帐户,该帐户必须授予REPLICAITON SLAVE权限。在主服务器登陆mysql上执行
grant replication slave on *.* to 'replication'@'192.168.0.100' identified by '123456';
注: replication@192.168.0.100这里是客户端的ip 可以使用 % 代替,表示允许任意的客户端,例如:
192.168.0.% 。表示该段地址的主机都可作为客户端。
查看master 状态:
mysql> show master status\G; *************************** 1. row *************************** File: mysql-bin.000001 Position: 416 Binlog_Do_DB: webportal Binlog_Ignore_DB: information_schema,performance_schema,mysql,test Executed_Gtid_Set: 1 row in set (0.00 sec)注意上面的 File 和 Position :slave 配置的时候会使用。
[mysqld] server_id=2 log-bin=mysql-bin.log replicate-do-db=webportal用change master语句指定同步位置
mysql>change master to master_host='192.168.0.102', master_user='replication', master_password='123456', master_log_file='mysql-bin.000001', master_log_pos=525; 注:master_log_file,master_log_pos由上面master查出的状态值中确定。master_log_file对应File,master_log_pos对应Position。启用slave;
start slave;
关闭slave;
stop slave;
查看状态:
show slave status;
这里出现了这样一个错误:
Last_IO_Error: Got fatal error 1236 from master when reading data from >> binary log: 'Slave can not handle replication events with the checksum that >> master is configured to log; the first event 'mysql-bin.000001'这是由于 master 用的 mysql5.6 , binlog_checksum 默认设置的是 crc32。 如果slave用的 5.5 或者更早的版本,请将master的 binglog_checksum设置为 none。
binlog_checksum=none
重启master : ./mysqld restart
查看slave :show slave status\G;
mysql> show slave status\G; *************************** 1. row *********** Slave_IO_State: Waiting for mas Master_Host: 192.168.0.102 Master_User: replication Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.00000 Read_Master_Log_Pos: 120 Relay_Log_File: YZ-relay-bin.00 Relay_Log_Pos: 266 Relay_Master_Log_File: mysql-bin.00000 Slave_IO_Running: Yes #必须为yes Slave_SQL_Running: Yes #必须为yes Replicate_Do_DB: webportal 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: 120 Relay_Log_Space: 419 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 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1 1 row in set (0.00 sec)
注:Slave_IO及Slave_SQL进程必须正常运行,即YES状态,否则都是错误的状态(如:其中一个NO均属错误)。