1:部署MySQL主从异步复制(三台机器)
(1):在所有机器的操作
《1》: systemctl stop firewalld 关闭防火墙
《2》:setenforce 0
《3》:iptables -F
(2):在 mysql master 上配置NTP时间同步服务器
《1》:rpm -q ntp 查看
《2》:vim /etc/ntp.conf 编辑
《3》:写入
server 127.127.1.0
fudge 127.127.1.0 stratum 8
(3):启动ntp
《1》:systemctl enable ntpd 重启
《2》:systemctl start ntpd
(4):在2个slave 节点上配置与master 进行时间同步
《1》:yum -y install ntpdate 安装
《2》:/usr/sbin/ntpdate 192.168.100.10
(5):在所有机器上的操作
《1》:yum -y install mariadb mariadb-devel maridb-server
(6): 配置 mysql master 服务器
《1》:在 /etc/my.cnf 中修改或者如下内容
[mysqld]
server-id = 1
log-bin=mysql-binlog
log-slave-updates=true
《2》:重启服务器
systemctl restart mariadb
《3》: 创建replication 用户
(7):备份Master原有数据
如果在生产环境中Master服务器已经运行一段时间,或者Master服务器上已经存在数据,为了保证所有数据的一致性,需要先将Master目前已有的数据全部导给Slave服务器。
备份的方法有很多,可以直接备份数据文件,也可以使用mysqldump工具。全新搭建的环境不存在数据备份问题。
[root@mysql-master ~]# mysqldump -uroot --all-detabases > /root/alldbbackup.sql
[root@mysql-master ~]# scp /root/ alldbbackup.sql root@192.168.200.112:/root/
[root@mysql-master ~]# scp /root/ alldbbackup.sql root@192.168.200.113:/root/
(8): 在mysqlslave 上配置:
《1》:导入mater的备份脚本
[root@localhost ~]# mysql -uroot -p < /root/alldbbackup.sql
从库连接主库进行测试,如果连接成功说明主库配置成功
《1》:登录
[root@localhost ~]# mysql -u myslave -p123456 -h 192.168.100.10
《2》:修改MySQL配置文件
[root@localhost ~]# vim /etc/my.cnf
[mysqld]
server-id=2
relay-log=relay-log-bin
relay-log-index=slave-relay-bin.index
配置多个从服务器时依次设置server-id号
《3》:修改完后重启数据库
[root@localhost ~]# systemctl start mariadb
《4》:在Slave服务器授权,启动从库,进行主从库数据同步
[root@localhost ~]# mysql
MariaDB [(none)]> stop slave;
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='172.16.0.17',MASTER_USER='root',MASTER_PASSWORD='Manyun@888',MASTER_LOG_FILE='mysql-binlog.0000068',MASTER_LOG_POS=1712;
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.200.111
Master_User: myslave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-binlog.000003
Read_Master_Log_Pos: 477
Relay_Log_File: relay-log-bin.000002
Relay_Log_Pos: 532
Relay_Master_Log_File: mysql-binlog.000003
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: 477
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)
参数说明:
CHANGE MASTER TO
MASTER_HOST='master_host_name', #主服务器的IP地址
MASTER_USER='replication_user_name', #主服务器授权的用户
MASTER_PASSWORD='replication_password', #主服务器授权的密码
MASTER_LOG_FILE='recorded_log_file_name', #主服务器二进制日志的文件名
MASTER_LOG_POS=recorded_log_position; #日志文件的开始位置
9、测试复制是否成功
在Master服务器上创建一个数据库或者表,到Slave服务器上查看,如果配置成功就可以成功同步。
《1》:主库查看当前存在的库
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
《2》:从库查看当前存在库
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
《3》:主库服务器创建库和表:
MariaDB [(none)]> create database sampdb;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> use sampdb;
Database changed
MariaDB [sampdb]> create table new(name char(20),phone char(20));
Query OK, 0 rows affected (0.01 sec)
《4》:从库验证:
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sampdb |
| test |
+--------------------+
5 rows in set (0.00 sec)
MariaDB [(none)]> use sampdb
MariaDB [sampdb]> show tables;
+------------------+
| Tables_in_sampdb |
+------------------+
| new |
+------------------+
1 row in set (0.00 sec)
说明主从数据库创建成功。