标签:nec 设置 bin start server mysq run back sla
主从复制,添加从服务器[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 |
+--------------------+
| information_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)
测试,在主服务器上增删修改数据,查看是否可以自动同步
标签:nec 设置 bin start server mysq run back sla
原文地址:https://blog.51cto.com/13749470/2391229