-
主机环境与软件版本
-
主机系统:CentOS7.3 64位
-
主机(master-1):IP地址: 193.5.1.22
-
主机(master-2):IP地址: 193.5.1.25
-
MySQL版本:mysql-5.7.22
-
MySQL 安装
-
参考:https://blog.csdn.net/sinat_39562444/article/details/83784611,要在两个机器中均安装成功。
-
MySQL 配置
-
master-1服务器中文件/etc/my.cnf 中添加如下配置
[mysqld] server-id=1 #server的唯一标识 auto_increment_offset=1 #自增id起始值 auto_increment_increment=2 #每次自增数字 log-bin = mysql-bin #打开二进制功能,MASTER主服务器必须打开此项 max_binlog_size=1024M #binlog单文件最大值 replicate-ignore-db = mysql #忽略不同步主从的数据库 replicate-ignore-db = information_schema port=3306 datadir=/usr/local/mysql/data socket=/usr/local/mysql/mysql.sock user=mysql max_connections=151 connection-control-failed-connections-threshold=5 #登陆失败次数限制 connection-control-min-connection-delay=108000 #限制重试时间,此处为毫秒,注意按需求换算 # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 # 设置忽略大小写 lower_case_table_names = 1 # 指定编码 character-set-server=utf8 collation-server=utf8_general_ci # 开启ip绑定 bind-address = 0.0.0.0 [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid #指定客户端连接mysql时的socket通信文件路径 [client] socket=/usr/local/mysql/mysql.sock default-character-set=utf8
-
master-2服务器中文件/etc/my.cnf 中添加如下配置:
[mysqld] server-id = 2 #server的唯一标识 auto_increment_offset = 2 #自增id起始值 auto_increment_increment = 2 #每次自增数字 log-bin = mysql-bin #打开二进制功能,MASTER主服务器必须打开此项 max_binlog_size=1024M #binlog单文件最大值 replicate-ignore-db = mysql #忽略不同步主从的数据库 replicate-ignore-db = information_schema port=3306 datadir=/usr/local/mysql/data socket=/usr/local/mysql/mysql.sock user=mysql max_connections=151 connection-control-failed-connections-threshold=5 #登陆失败次数限制 connection-control-min-connection-delay=108000 #限制重试时间,此处为毫秒,注意按需求换算 # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 # 设置忽略大小写 lower_case_table_names = 1 # 指定编码 character-set-server=utf8 collation-server=utf8_general_ci # 开启ip绑定 bind-address = 0.0.0.0 [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid #指定客户端连接mysql时的socket通信文件路径 [client] socket=/usr/local/mysql/mysql.sock default-character-set=utf8
-
重启mysql
service mysql restart(两台)
-
主从配置
-
添加主从同步账户
在master-1中添加repl用户:
mysql> grant replication slave on *.* to 'repl'@'193.5.1.22' identified by '123456';
在master-2中添加repl用户:
mysql> grant replication slave on *.* to 'repl'@'193.5.1.25' identified by '123456';
-
查看主库的状态
在master-1上执行如下命令:
mysql> show master status;
在master-2上执行如下命令:mysql> show master status;
-
配置同步数据
在master-1上执行如下命令:
mysql> change master to master_host='193.5.1.25',\ master_port=3306,master_user='repl',master_password='123456',\ master_log_file='mysql-bin.000005',master_log_pos=3123; mysql> start slave; mysql> show slave status\G;
下面这两个状态表示正常:
Slave_IO_Running: Yes Slave_SQL_Running: Yes
master2和master1执行相同的操作
说明:master_log_file: master的日志文件,则master-1互指master-2中的数据,根据上图可以得到此值。 master_log_pos: master的日志文件起始位置,则master-1互指master-2中的数据,根据上图可以得到此值。
测试主从是否同步
在正式环境使用时发现
Mysql创建函数报错This function has none of DETERMINISTIC, NO SQL, 的解决方案
3个解决方法如下:
1. mysql> set global log_bin_trust_function_creators = 1;
2. 系统启动时 --log-bin-trust-function-creators=1
3. 在my.ini(linux下为my.conf)文件中 [mysqld] 标记后加一行内容为 log-bin-trust-function-creators=1