Configuration
1、主数据库配置
修改MySQL配置文件,配置数据库为master;
nano /etc/mysql/conf.d/mysql.cnf
新增mysqld
字段,并按照如下方式配置
[mysqld]
# master id 数据主从不能一致,必须唯一
server-id=1
# 指定binlog格式
log-bin=mysql-bin
sync_binlog=1
binlog_format=ROW
log-slave-updates=1
slave-skip-errors=1
# 设置忽略的数据库
binlog-ignore-db=mysql
binlog-ignore-db=performance_schema
binlog-ignore-db=information_schema
# 设置同步的数据库,这里是radiusdb, 添加多个同步数据库要以逗号隔开
binlog-do-db=radiusdb
注意:
从MySQL 5.1.12开始,可以用以下三种模式来实现主从数据同步:
基于SQL语句的复制(statement-based replication, SBR);
基于行的复制(row-based replication, RBR);
混合模式复制(mixed-based replication, MBR)。
相应地,binlog的格式也有三种:STATEMENT,ROW,MIXED。
/* 此配置为临时配置,服务重启后失效 */
SET GLOBAL binlog_format = 'ROW';
show variables like 'binlog_format';
温馨提示:在主服务器上最重要的二进制日志设置是sync_binlog,这使得mysql在每次提交事务的时候把二进制日志的内容同步到磁盘上,即使服务器崩溃也会把事件写入日志中。
sync_binlog这个参数是对于MySQL系统来说是至关重要的,他不仅影响到Binlog对MySQL所带来的性能损耗,而且还影响到MySQL中数据的完整性。对于sync_binlog
参数的各种设置的说明如下:
sync_binlog=0,当事务提交之后,MySQL不做fsync之类的磁盘同步指令刷新binlog_cache中的信息到磁盘,而让Filesystem自行决定什么时候来做同步,或者cache满了之后才同步到磁盘。
sync_binlog=n,当每进行n次事务提交之后,MySQL将进行一次fsync之类的磁盘同步指令来将binlog_cache中的数据强制写入磁盘。
在MySQL中系统默认的设置是sync_binlog=0,也就是不做任何强制性的磁盘刷新指令,这时候的性能是最好的,但是风险也是最大的。因为一旦系统Crash,在binlog_cache中的所有binlog信息都会被丢失。而当设置为“1”的时候,是最安全但是性能损耗最大的设置。因为当设置为1的时候,即使系统Crash,也最多丢失binlog_cache中未完成的一个事务,对实际数据没有任何实质性影响。
对于高并发事务的系统来说,sync_binlog
设置为0和设置为1的系统写入性能差距可能高达5倍甚至更多。
查看数据库状态
show variables like 'log_bin';
#显示on
show variables like 'binlog_format';
#显示row
#如果不是修改配置并重启一下
2、创建同步用户
创建同步用户
CREATE USER 'radius_sync'@'hostxxx' IDENTIFIED BY 'password';
授予slave的权限
GRANT replication slave,replication client ON *.* TO 'radius_sync'@'hostxxx';
检查用户权限状态
show grants;
show grants for radius_sync@'hostxxx';
3、从数据库配置
修改MySQL配置文件
nano /etc/mysql/conf.d/mysql.cnf
[mysqld]
部分的配置按照如下就行修改,注意:server-id
在主从集群中唯一
[mysqld]
# server-id唯一
server-id=2
# 设置只读
read_only=0
# 设置同步的数据表,这里只同步radiusdb库下的radcheck表
replicate-do-table=radiusdb.radcheck
Running !!
1、主数据库上线
重启主数据库,重新加载配置文件,使master上线;
systemctl restart mysql.service
检查主数据库状态,并记录File和Position的值
mysql> show master status;
+------------------+----------+--------------+---------------------------------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+---------------------------------------------+-------------------+
| mysql-bin.000006 | 8397 | radiusdb | mysql,performance_schema,information_schema | |
+------------------+----------+--------------+---------------------------------------------+-------------------+
1 row in set (0.00 sec)
2、从数据库上线
/* slave连接主库 */
change master to master_host='yoursqlhost', master_user='radius_sync', master_password='password', master_log_file='mysql-bin.000006', master_log_pos=0;
/* 开启slave */
start slave;
show slave status;
master_log_file: master 状态中的File值
master_log_pos : master 状态中的Position值,配置为0表示由系统自动设置
3、相关命令
/* 查看主备状态 */
show master status;
show slave status \G;
/* 重设从数据库状态 */
reset slave;