准备两台机器:10.200.94.42(mysql01),10.200.94.22(mysql02)
以下是mysql01操作
vi /etc/my.cnf
#关于主从服务开始,作为主从复制的唯一标识,集群中,不能重复
server_id=1
#开启二进制日志,指定文件目录和文件名前缀
log-bin = master-log
#开启中继日志
relay-log = slave-log
#表示自增长字段从那个数开始,取值范围是1 .. 65535。这个就是序号。如果有n台mysql机器,则从第一台开始分为设1,2...n
auto_increment_offset = 1
#表示自增长字段每次递增的量,其默认值是1,取值范围是1 .. 65535。如果有n台mysql机器,这个值就设置为n。
auto_increment_increment = 2
如果使用主主服务,多添加下面几条配置
#不同步mysql系统数据库,如果多个不同库,就此格式另写几行,也可以一行,中间逗号分开。
binlog-ignore-db=mysql,information_schema
sync_binlog = 1
binlog_checksum = none
slave-skip-errors = all
然后保存文件,关闭防火墙,重启数据库
systemctl restart mysqld
mysql -uroot -p
mysql>create user 'mysql01'@'10.200.94.42' identified by '123456';
如果报错:
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
mysql> set global validate_password_policy=LOW;
mysql> set global validate_password_length=6;
mysql> create user 'mysql01'@'10.200.94.42' identified by '123456';
开启22对数据库的访问连接
mysql>grant replication slave on *.* to 'mysql01'@'10.200.94.22' identified by '123456';
mysql> flush privileges;
执行后备用,等到mysql02启动后使用里面的数据
mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-log.000001 | 875 | | | |
+-------------------+----------+--------------+------------------+-------------------+
等待mysql02数据库启动后再执行,先放这里备用
change master to master_host='10.200.94.22', master_user='mysql02', master_password='123456', master_log_file='master-log.000001', master_log_pos=875;
mysql> stop slave;
mysql> start slave;
mysql> show slave status\G;
出现两个Yes表示成功
如果出现
Slave_IO_Running:Connecting
Slave_SQL_Running:Yes
可能的原因:
1.网络不通
2.账户密码错误
3.防火墙
4.mysql配置文件问题
5.连接服务器时语法
6.主服务器mysql权限
下面开始mysql02的操作
安装数据库后修改/etc/my.conf文件
#作为主从复制的唯一标识,集群中,不能重复
server_id=2
#开启二进制日志
log-bin = master-log
#开启中继日志
relay-log = slave-log
auto_increment_offset = 2
auto_increment_increment = 2
binlog_format = mixed
#下面是搭建主主服务需要配置,如果主从无需配置
binlog-ignore-db = mysql,information_schema
sync_binlog = 1
binlog_checksum = none
slave-skip-errors = all
关闭防火墙,重启mysql
systemctl restart mysqld
mysql -uroot -p
mysql> create user 'mysql02'@'10.200.94.22' identified by '123456';
mysql>grant replication slave on *.* to 'mysql02'@'10.200.94.42' identified by '123456';c
mysql> flush privileges;
#执行下面的命令,之后获取到file值就可以去编辑mysql01备用部分的命令了
mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-log.000001 | 875 | | | |
在mysql01执行完备用部分的命令后,继续执行mysql02这里的命令
mysql>change master to master_host='10.200.94.42', master_user='mysql01', master_password='123456', master_log_file='master-log.000001', master_log_pos=875;
mysql> stop slave;
mysql> start slave;
mysql> show slave status\G;
出现两个yes表示成功
重启分别重启就行,重启后大约半分钟左右 show slave status\G; 会是不正常的,半分钟左右后执行就会出现两个yes