主从复制,添加从服务器
三台MysqL服务器。一台为主,一台为从,实现自动备份。再添加一台新的从服务器
系统:centos7.6
mariadb:5.5.60
主服务器:
[root@Centos7 ~]#vim /etc/my.cnf
[MysqLd]
server_id=7 #设置服务器ID
log_bin #开启二进制日志
启动MysqL,查看二进制位置信息,创建slave服务器可以使用的的用户
[root@Centos7 ~]#systemctl start mariadb
[root@Centos7 ~]#MysqL
MariaDB [(none)]> show master logs;
+--------------------+-----------+
| Log_name | File_size |
+--------------------+-----------+
| mariadb-bin.000001 | 245 |
+--------------------+-----------+
1 row in set (0.00 sec)
MariaDB [(none)]> grant replication slave on *.* to repluser@'192.168.37.%' identified by 'centos';
Query OK, 0 rows affected (0.01 sec)
主服务器设置完成
从服务器
[root@Centos7 ~]#vim /etc/my.cnf
[MysqLd]
server_id=17 #设置服务器ID
read_only #从服务器建议加上,只读
配置同步信息:用主服务给的用户实现复制主服务器的数据到本机
[root@Centos7 ~]#MysqL
.
.
.
MariaDB [(none)]> CHANGE MASTER TO
-> MASTER_HOST='192.168.37.7', #主服务器IP
-> MASTER_USER='repluser', #用户
-> MASTER_PASSWORD='centos', #密码
-> MASTER_PORT=3306, #端口
-> MASTER_LOG_FILE='mariadb-bin.000001', #二进制文件
-> MASTER_LOG_POS=245; #开始复制的位置
Query OK, 0 rows affected (0.03 sec)
MariaDB [(none)]> start slave; #开启从服务器线程
MariaDB [(none)]> show slave status\G #查看状态
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.37.7 #主服务器IP
Master_User: repluser #使用的用户
Master_Port: 3306 #使用端口
Connect_Retry: 60
Master_Log_File: mariadb-bin.000001
Read_Master_Log_Pos: 400
Relay_Log_File: mariadb-relay-bin.000002
Relay_Log_Pos: 686
Relay_Master_Log_File: mariadb-bin.000001
Slave_IO_Running: Yes #同步成功
Slave_sql_Running: Yes #同步成功
.
.
.
Replicate_Ignore_Server_Ids:
Master_Server_Id: 7 #主服务器ID
1 row in set (0.00 sec)
.
.
.
测试:在主服务器上增删改数据,在从服务器上自动同步
给已有数据的主服务器,添加一台从服务器
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| @R_360_4045@ion_schema |
| hellodb |
| MysqL |
| performance_schema |
| testdb1 |
| testdb2 |
| testdb3 |
+--------------------+
7 rows in set (0.00 sec)
完全备份主服务器的数据,查看主服务器日志记录位置,并传送给新的从服务器
[root@Centos7 ~]#MysqLdump -A --single-transaction -F --master-data=1 > /data/backup/all.sql
[root@Centos7 ~]#scp -r /data/backup/* 192.168.37.27:/data/
[root@Centos7 ~]#MysqL
.
.
.
MariaDB [(none)]> show master logs;
+--------------------+-----------+
| Log_name | File_size |
+--------------------+-----------+
| mariadb-bin.000001 | 8436 |
| mariadb-bin.000002 | 245 | #从最新的位置开始同步,
+--------------------+-----------+
新的从服务器设置
[root@Centos7 ~]#vim /etc/my.cnf
[MysqLd]
server_id=37
read_only
.
.
.
[root@Centos7 ~]#vim /data/all.sql
.
.
.
CHANGE MASTER TO
MASTER_HOST='192.168.36.7',
MASTER_USER='repluser',
MASTER_PASSWORD='centos',
MASTER_PORT=3306,
MASTER_LOG_FILE='mariadb-bin.000002',
MASTER_LOG_POS=245;
.
.
.
启动MysqL服务,导入备份的数据,进入MysqL,开启slave线程
[root@Centos7 ~]#systemctl start mariadb
[root@Centos7 ~]#MysqL < /data/all.sql
[root@Centos7 ~]#MysqL
MariaDB [(none)]> start slave; #开启线程
Query OK, 0 rows affected (0.02 sec)
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.37.7
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mariadb-bin.000002
Read_Master_Log_Pos: 245
Relay_Log_File: mariadb-relay-bin.000002
Relay_Log_Pos: 531
Relay_Master_Log_File: mariadb-bin.000002
Slave_IO_Running: Yes #成功
Slave_sql_Running: Yes #成功
.
.
.
Master_Server_Id: 7
1 row in set (0.00 sec)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。