① 环境筹备
本地装置两个 mysql,或者应用虚拟机,或者应用 docker 装置,须要筹备两个 mysql,本文应用 docker 装置,具体装置办法详见
https://www.jianshu.com/p/10769f985516
环境
宿主机 centos7
mysql:5.6
mysql1(master): 172.17.0.3:3307
mysql2(slave): 172.17.0.2:3308
② mysql 配置文件配置
mysql1(master): 172.17.0.3 my.cnf 配置文件设置,mysql
#mysql master1 config
[mysqld]
server-id = 1 # 节点 ID,确保惟一
# log config
log-bin = mysql-bin #开启 mysql 的 binlog 日志性能
sync_binlog = 1 #管制数据库的 binlog 刷到磁盘下来 , 0 不管制,性能最好,1 每次事物提交都会刷到日志文件中,性能最差,最平安
binlog_format = mixed #binlog 日志格局,mysql 默认采纳 statement,倡议应用 mixed
expire_logs_days = 7 #binlog 过期清理工夫
max_binlog_size = 100m #binlog 每个日志文件大小
binlog_cache_size = 4m #binlog 缓存大小
max_binlog_cache_size= 512m #最大 binlog 缓存大
binlog-ignore-db=mysql #不生成日志文件的数据库,多个疏忽数据库能够用逗号拼接,或者 复制这句话,写多行
auto-increment-offset = 1 # 自增值的偏移量
auto-increment-increment = 1 # 自增值的自增量
slave-skip-errors = all #跳过从库谬误
mysql2(slave): 172.17.0.2 mysql.cnf 配置
[mysqld]
server-id = 2
log-bin=mysql-bin
relay-log = mysql-relay-bin
replicate-wild-ignore-table=mysql.%
replicate-wild-ignore-table=test.%
replicate-wild-ignore-table=information_schema.%
重启两个 mysql,让配置失效
③ master 数据库,创立复制用户并受权
1. 进入 master 的数据库,为 master 创立复制用户
CREATE USER repl_user IDENTIFIED BY 'repl_passwd';
2. 赋予该用户复制的权力
grant replication slave on *.* to 'repl_user'@'172.17.0.2' identified by 'repl_passwd';
FLUSH PRIVILEGES;
3. 查看 master 的状态
show master status;
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000005 120| | mysql | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
4, 配置从库
mysql> CHANGE MASTER TO
MASTER_HOST = '172.17.0.3',
MASTER_USER = 'repl_user',
MASTER_PASSWORD = 'repl_passwd',
MASTER_PORT = 3307,
MASTER_LOG_FILE='mysql-bin.000005',
MASTER_LOG_POS=120,
MASTER_RETRY_COUNT = 60,
MASTER_HEARTBEAT_PERIOD = 10000;
# MASTER_LOG_FILE='mysql-bin.000005',# 与主库 File 保持一致
# MASTER_LOG_POS=120 , #与主库 Position 保持一致
启动从库 slave 过程
mysql> start slave;
Query OK, 0 rows affected (0.04 sec)