1. 配置
1.1. 公共配置
[mysqld]配置节点
# 通用 character-set-server=utf8mb4 init-connect='SET NAMES utf8mb4' lower_case_table_names=1 sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION skip-name-resolve=ON transaction-isolation=READ-COMMITTED # 链接数 max_connections=1100 max_user_connections=1000 max_connect_errors=1000 # 复制忽略表,读写分离 replicate-ignore-db=mysql,information_schema,performance_schema # replicate-wild-ignore-table=mysql.% # replicate-wild-ignore-table=performance_schema.% # replicate-wild-ignore-table=information_schema.% # 开启binlog日志 log-bin=mysql-bin binlog_cache_size=4M binlog_format=mixed expire_logs_days=10
[mysql]配置节点
default-character-set=utf8mb4
1.3. 从库
[mysqld]配置节点
# 从数据库配置 # 该配置需要根据实际情况配置,确保网络内唯一 server-id=2 # 是否把主同步过来的操作写到自己的log-bin中 log_slave_updates=1 # 复制log relay-log=mysql-relay-bin # 配置安全恢复,防止slave宕机出现复制错误 relay-log-recovery=1 relay-log-purge=1 # 对应表名为mysql.slave_relay_log_info relay-log-info-repository=table # 对应表名为mysql.slave_master_info master-info-repository=table # 设置只读 read_only=1
创建查询用户
create user '用户'@'%' identified by '密码'; grant Select on 数据库.* to '用户'@'%'; flush privileges;
4. 主主同步
-
如果a→b b→a 这样的双master架构下,a,b都打开log_slave_updates选项会不会出现无限循环的状态
mysql已经考滤到了这个问题,每条bin-log都会记录执行语句的源server_id.当slave读到语句的server_id等于本身的ID的时候,不会忽略执行,所以我们不用担心a,b会不会无限循环下去。
6. 星式同步
6.1. 从库提升为主库
-
确保所有的relay log全部更新完毕,在每个从库上执行
stop slave io_thread; show processlist;`直到看到 `Slave has read all relay log; waiting for more updates
,则表示从库更新都执行完毕了 -
登陆所有从库,查看master.info文件,对比选择pos最大的作为新的主库,这里我们选择192.168.1.102为新的主库
-
登陆192.168.1.102,执行stop slave; 并进入数据库目录,删除master.info和relay-log.info文件, 配置my.cnf文件,开启log-bin,如果有 log-slaves-updates和read-only则要注释掉,执行reset master
-
创建用于同步的用户并授权slave,同第五大步骤
-
登录另外一台从库,执行
stop slave
停止同步 -
根据第七大步骤连接到新的主库
-
执行start slave;
-
修改新的master数据,测试slave是否同步更新
参考:https://www.cnblogs.com/alinuxer/p/9890462.html
6.2. 导出视图报错
-
查看视图创建语句
show create table 视图名\G;
-
建立一个该视图对应的的用户,并授权
create user '用户名'@'%' identified by '密码'; grant all privileges on 数据库.* to '用户名'@'%'; flush privileges;
mysqldump -uroot -p密码 -h127.0.0.1 -P3306 --single-transaction --flush-logs --master-data=2 --databases 多个数据库以空格分开|gzip -9 > /home/backup.sql.gz
grant replication slave on *.* to 'replicate'@'IP' identified by '密码'; flush privileges;