MySQL主从复制(一主两从)

 

 

 

 

 

   主库开启bin-log二进制日志功能,并建立slave账号,并授权从库连接主库,从库通过change master得到主库的相关同步信息,

然后连接主库进行验证,主库产生的新数据会导入到bin-log二进制文件中,同时主库会开启lo线程,从库也会开启lo线程以及

sql线程,从库中的lo线程与主库的lo线程连接一旦主库库数据有所变更则从库将变更的数据复制到relary-bin(中继日志)中,

sql线程会读取relay log(中继日志)文件中的日志,并解析成具体操作,至此整个同步过程完成

 

 

设备:

master:192.168.200.125

slave1:192.168.200.124

slave2:192.168.200.111

 

Master主操作:

关闭防火墙:

[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# iptables -F
[root@localhost ~]# setenforce 0

安装mariadbb:

[root@localhost ~]# yum install mariadb mariadb-server -y                #此处安装了mariadb数据库系统

安装ntp时间同步软件:主要为了让主服务器和从服务器之间时间一致。那么从服务器中继日志和主的二进制日志数据一致

[root@localhost ~]# yum install ntp -y

[root@localhost ~]# vim /etc/ntp.conf 

在末尾加入如下两行:主要让自己变成ntp的服务端使得从服务器能获取主服务器的时间

server 127.127.1.0 fudge 127.127.1.0 stratum 8

启动NTP服务:

[root@localhost ~]# systemctl start ntpd
[root@localhost ~]# systemctl enable ntpd

 配置主配置文件开启二进制日志:

[root@localhost ~]# vim /etc/my.cnf

[mysqld]

server-id=1 #server的id号 log-bin=mysql-binlog #前缀 log-slave-updates=true #允许从对log_bin进行更新

重启服务器:

[root@localhost ~]# systemctl restart mariadb
[root@localhost ~]# netstat -anpt | grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN

 创建Replication用户:

[root@localhost ~]# mysql -uroot -p123123

MariaDB [(none)]> grant replication slave on *.* to 'myslave'@'192.168.200.%' identified by '123123';             #允许192.168.200.网段的myslave连接数据库连接密码为123123
Query OK, 0 rows affected (0.00 sec)                     

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

ERROR: No query specified

MariaDB [(none)]> show master status;                       #获取masterDB的相关信息
+---------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+---------------------+----------+--------------+------------------+
| mysql-binlog.000001 | 479 | | |
+---------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

考虑问题有没有之前留下的老数据:备份master原有所有数据到两台slave上

[root@localhost ~]# mysqldump -uroot -p123123 --all-databases > /root/alldbbackup.sql
[root@localhost ~]# scp /root/alldbbackup.sql root@192.168.200.124:/root/
[root@localhost ~]# scp /root/alldbbackup.sql root@192.168.200.111:/root/

主服务器创建一个新的数据库用以给从数据库测试同步情况:

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

MariaDB [mysql]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| liuxiang |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)

 

 

 

Slave1和Slave2操作:两台从服务器操作一致

关闭防火墙:

[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# iptables -F
[root@localhost ~]# setenforce 0

安装mariadbb:

[root@localhost ~]# yum install mariadb mariadb-server -y

安装ntpdate:

[root@localhost ~]# yum install ntpdate -y

在两个salve节点上配置与Master进行时间同步:

[root@localhost ~]# ntpdate 192.168.200.125
15 Oct 14:15:10 ntpdate[11932]: adjust time server 192.168.200.125 offset -0.018919 sec

做周期计划:

[root@localhost ~]# crontab -l
* */5 * * * /usr/sbin/ntpdate pool.ntp.org > /dev/null 2>&1                 #pool.ntp.org是互联网上的时间同步器

导入master原数据脚本到mysql库中:

[root@localhost ~]# mysql -uroot -p123123 < /root/alldbbackup.sql 

测试连接主数据库运行测试:连接成功配置正确

[root@localhost ~]# mysql -u myslave -p123123 -h 192.168.200.125
修改配置文件开启中继日志:

[root@localhost ~]# vim /etc/my.cnf

[mysqld]

server-id=2                               #slave2从服务器此处改为3
relay-log=relay-log-bin                   #前缀
relay-log-index=slave-relay-bin.index     #索引文件

重启mariadb服务:

[root@localhost ~]# systemctl restart mariadb

启动从库,运行从库数据同步:

[root@localhost ~]# mysql -uroot -p12

MariaDB [(none)]> stop slave;                                                         #停掉自己从的数据库角色
Query OK, 0 rows affected, 1 warning (0.09 sec)

MariaDB [(none)]> CHANGE MASTER TO
-> MASTER_HOST='192.168.200.125',
-> MASTER_USER='myslave',
-> MASTER_PASSWORD='123123',
-> MASTER_LOG_FILE='mysql-binlog.000001',                          #masterDB的相关信息
-> MASTER_LOG_POS=721;                                                         #masterDB的相关信息
Query OK, 0 rows affected (0.10 sec)

MariaDB [(none)]> start slave;                                                        #开启自己从的数据库角色
Query OK, 0 rows affected (0.00 sec)

 

MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.200.125
Master_User: myslave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-binlog.000001
Read_Master_Log_Pos: 721
Relay_Log_File: relay-log-bin.000002
Relay_Log_Pos: 532
Relay_Master_Log_File: mysql-binlog.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 721
Relay_Log_Space: 824
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0                                             #报错编号
Last_IO_Error:                                                 #报错位置
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
1 row in set (0.00 sec)

ERROR: No query specified

从测试与主数据库同步情况:

MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| auth |
| client |
| liuxiang |
| mydb |
| mysql |
| performance_schema |
| shuifei |
| test |
| var |
| yg |
+--------------------+
11 rows in set (0.00 sec)

posted @ 2019-10-15 12:29  Room、C  阅读(4373)  评论(0编辑  收藏  举报