Centos7 Mysql主从复制

实验环境

MySQL-master:192.168.200.111
MySQL-slave1:192.168.200.112
MySQL-slave2:192.168.200.113

关闭防火墙和安全机制
[root@localhost ~]# iptables -F
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
3台主机yum安装mariadb
[root@localhost ~]# yum -y install mariadb mariadb-server mariadb-devel
建立时间同步环境,在主服务器上安装配置NTP时间同步服务器
[root@localhost ~]# yum -y install ntp

末行添加并重启

[root@localhost ~]# vim /etc/ntp.conf
server 127.127.1.0
fudge 127.127.1.0 startum 8
[root@localhost ~]# systemctl restart ntpd
在从服务器上同步时间
[root@localhost ~]# yum -y install ntpdate
[root@localhost ~]# ntpdate 192.168.200.111
15 Oct 12:02:12 ntpdate[10406]: adjust time server 192.168.200.111 offset 0.008413 sec

slave2服务器与slave1服务器相同

配置MySQL Master主服务器
[root@localhost mysql]# vim /etc/my.cnf
[mysqld]
server-id=1                      //三台不要相同
log-bin=mysql-bin
log-slave-updates=true   //手动添加,开启从日志
[root@localhost ~]# systemctl restart mariadb
给从服务器授权
[root@localhost ~]# mysql -uroot -p123456
MariaDB [(none)]> grant replication slave on *.* to 'root'@'192.168.200.%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000010 | 474 |                 |                        |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

注意:之前主服务器有数据需要先传给从服务器

主:mysqldump -uroot -all-databases > /root/alldbacup.sql

scp传给从服务器

从:mysql -uroot -p < /root/alldbacup.sql

配置从服务器
[root@localhost ~]# vim /etc/my.cnf
[mysqld]
server-id=2  //id号不能相同
relay-log=relay-log-bin
relay-log-index=slave-relay-bin.index 
[root@localhost ~]# systemctl restart mariadb
[root@localhost ~]# mysql -uroot -p123456
MariaDB [(none)]> stop slave;
Query OK, 0 rows affected, 1 warning (0.01 sec)
MariaDB [(none)]> change master to
-> master_host='192.168.200.111',
-> master_user='root',
-> master_password='123456',
-> master_log_file='mysql-bin.000010',
-> master_log_pos=474;
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.200.111
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000010
Read_Master_Log_Pos: 474
Relay_Log_File: relay-log-bin.000002
Relay_Log_Pos: 529
Relay_Master_Log_File: mysql-bin.000010
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
........................................

通过查看slave状态,确保Slave_IO_Running: Yes Slave_SQL_Running: Yes

slave2配置相同

在主服务器创建数据库,在从服务器查看是否同步

MariaDB [(none)]> create database db_test;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema   |
| db_test        |
| mysql        |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)

从:

MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema   |
| db_test        |
| mysql        |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)

成功实现主从复制

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT民工9727

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值