本文共 1876 字,大约阅读时间需要 6 分钟。
确保主从服务器时间一致是MySQL复制的基础。建议使用NTPdate进行时间同步,以下是配置步骤:
yum -y install ntpdatecrontab -e */5 * * * * /usr/sbin/ntpdate cn.pool.ntp.org > /dev/null
注意事项:如果执行crontab
时出现command not found
错误,需先安装crontab:
yum -y install vixie-croncrontab -lchkconfig --list crondservice crond star
为了确保从服务器仅限只读,需启用read-only
选项。以下是详细步骤:
vim /etc/my.cnf:+62read-only=ON
service mysqld restartmysql -e "show global variables like '%read_only%'"
grant all on hellodb.* to 'testuser'@'192.168.%.%' identified by 'testpass';
mysql -u testuser -h 192.168.81.133 -p
默认情况下,MySQL采用异步复制机制。为了提高事务安全,可以通过以下方式进行优化:
autocommit
,在主服务器上手动提交事务:set global autocommit=0;
sync_binlog=1
半同步复制是一种混合性复制机制,结合了同步和异步的优点。以下是其核心概念和工作原理:
mysql -e "install plugin semisync_master.so"
set global rpl_semi_sync_master_enabled=ON;set global rpl_semi_sync_master_timeout=1000;
show global variables like '%semi%';
mysql -e "install plugin semisync_slave.so"
set global rpl_semi_sync_slave_enabled=ON;
show global variables like '%semi%';
use hellodb;create table t1 (id int);
mysql -u testuser -h 192.168.81.133 -p show databases;use hellodb;show tables;
STOP SLAVE IO_THREAD;START SLAVE IO_THREAD;
use hellodb;create table t4 (Name char(10));
show global status like '%semi%';
mysql -u testuser -h 192.168.81.133 -p show global variables like '%semi%';
通过以上步骤,可以实现MySQL的高效、安全的半同步复制配置。
转载地址:http://etbfk.baihongyu.com/