本文写于 2016 年。 介绍 Mysql 双主的安装方法。
运行环境 操作系统:CentOS 7 64bit
MySQL:5.6.34
需求说明 采用 yum 方式安装,配置异地双主 MySQL 服务器群集,实现数据异地同步。
安装过程 安装 MySQL 服务 1.安装 yum 数据源 Mysql 官网上最新版本已经更新到 5.7 ,我们要安装的是 5.6 版本
下载 yum 数据源文件
mysql57-community-release-el7-9 .noarch .rpm
安装 rpm 包
sudo yum localinstall mysql57-community-release-el7-9 .noarch .rpm
配置数据源,修改 enabled,关闭 mysql57,打开 mysql56
sudo vim /etc/yum.repos .d /mysql-community.repo
[mysql56-community] name =MySQL 5.6 Community Serverbaseurl =http://repo.mysql.com/yum/mysql-5.6-community/el/7/$basearch/enabled =1gpgcheck =1gpgkey =file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql[mysql57-community] name =MySQL 5.7 Community Serverbaseurl =http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/ enabled =0 gpgcheck =1 gpgkey =file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
2.安装 MySQL 服务 sudo yum install mysql-community-server sudo systemctl enable mysqld sudo systemctl start mysqld
3.安全配置初始化 mysql_secure_installation
4.修改数据文件保存位置 sudo systemctl stop mysqld sudo mkdir /storage/mysql sudo chown mysql: mysql /storage/mysql cd /var/lib /mysql sudo mv /var/lib /mysql /* /storage /mysql
修改 MySQL 配置文件,重启服务
[mysqld] datadir =/storage/mysql
sudo systemctl start mysqld
新建数据库 新建数据库 CREATE DATABASE dbname;新建用户 CREATE USER 'username' @'%' IDENTIFIED BY 'password' ;用户授权访问数据库 GRANT ALL PRIVILEGES ON dbname.* TO username@"%" ;权限更新 flush privileges ;
同步配置 新建账户 新建同步用户 GRANT REPLICATION SLAVE ON *.* TO 'replicate' @'%' IDENTIFIED BY 'password' ;权限更新 flush privileges ;
master1配置 新建 binlog 目录
sudo mkdir -p /storage/log sudo chown mysql:mysql /storage/log
修改 /etc/my.cnf
[mysqld] server-id =1 log-bin =/storage/log/mysql-bin.logread-only =0 auto-increment-offset = 1 auto-increment-increment = 2
重启 mysql 服务
sudo systemctl restart mysqld
检查 /storage/log 目录中是否有 binlog 文件生成
查看主服务器状态
mysql> show master status \G;
记录下 File 和 Position
master2配置 新建 binlog 目录
sudo mkdir -p /storage/log sudo chown mysql:mysql /storage/log
修改 /etc/my.cnf
[mysqld] server-id =2log-bin =/storage/log/mysql-bin.logrelay-log =/storage/log/mysqld-relay-bin.loglog-slave-updates replicate_wild_ignore_table =information_schema.%replicate_wild_ignore_table =mysql.%replicate_wild_ignore_table =performance_schema.%
重启 mysql 服务
sudo systemctl restart mysqld
检查 /storage/log 目录中是否有 binlog 文件生成
启动同步
stop slave; mysql> CHANGE MASTER TO -> MASTER_HOST ='master' , -> MASTER_USER ='replicate' , -> MASTER_PASSWORD ='password' , -> MASTER_CONNECT_RETRY =60, -> MASTER_LOG_FILE ='master1的File' , -> MASTER_LOG_POS =master1的Position; start slave;
检查状态
mysql> show slave status \G;
重点检查下列两项
Slave_IO_Running: Yes Slave_SQL_Running: Yes
若不为 Yes ,则需要排查原因
可能存在的问题
Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work.
原因为 master2 由 master1 的虚拟机复制而来,删除 master2 的 /storage/mysql/auto.cnf,重启 mysqld 服务即可
至此 master2 已配置为 master1 的备机
配置 master1 为 master1 的备机过程,同上面的方法类似master2 注意调整下子增长ID的步长 auto-increment-offset = 2 auto-increment-increment = 2
最终配置完成后,两台服务器的配置如下:
master1
[mysqld] server-id =1log-bin =/storage/log/mysql-bin.logread-only =0relay-log =/storage/log/mysqld-relay-bin.loglog-slave-updates replicate_wild_ignore_table =information_schema.%replicate_wild_ignore_table =mysql.%replicate_wild_ignore_table =performance_schema.%auto-increment-offset = 1 auto-increment-increment = 2
master2
[mysqld] server-id =2log-bin =/storage/log/mysql-bin.logread-only =0relay-log =/storage/log/mysqld-relay-bin.loglog-slave-updates replicate_wild_ignore_table =information_schema.%replicate_wild_ignore_table =mysql.%replicate_wild_ignore_table =performance_schema.%auto-increment-offset = 2 auto-increment-increment = 2
binlog-do-db、binlog-ignore-db、replicate-do-db、replicate-ignore-db使用存在一定的风险
推荐使用在主机端不设置 binlog 的生成条件,在备机端设置同步的对象replicate_wild_ignore_table