Mysql 主从复制操作
文章目录
- 准备工作
- 3.1.1 master服务器配置
- 3.1.2 两台SLAVE服务器配置
- 3.2、MySQL手工编译安装(这里忽略)
- 3.3、配置主从同步
- ① master服务器修改配置文件
- ② 从服务器配置
- 3.4、测试数据同步
- 2.1 MySQL的复制类型
- 2.2 MySQL主从复制的工作过程;
准备工作
整个实验的环境 以及服务器信息
- 环境部署 cetos7.6
- 虚拟机服务环境
Master服务器:192.168.10.16 mysql5.7
slave1服务器:192.168.10.14 mysql5.7
Slave2服务器:192.168.10.15 mysql5.7
Amoeba服务器:192.168.10.80 (预备机器) jdk1.6
客户端服务器:192.168.10.13 测试
3.1.1 master服务器配置
① 安装ntp、修改配置文件
[root@master ~]# yum install ntp -y
[root@master ~]# vim /etc/ntp.conf 可不做,在内网环境
------------------------------------------------
[root@master ~]# yum -y install ntpdate ntp #安装ntp软件
[root@master ~]# ntpdate ntp.aliyun.com #时间同步
[root@master ~]# vi /etc/ntp.conf
放在其他server后
server 127.127.1.0 #设置本机为时间同步源fudge 127.127.1.0 stratum 10 #编辑配置文件 放在server 127.127.1.0 后
#设置本机的时间层级为10级,0级表示时间层级为0级,是向其他服务器提供时间同步源的意思,不要设置为0级
② 开启NTP服务、关闭防火墙和增强性安全功能
[root@master ~]# systemctl start ntpd
[root@master ~]# systemctl stop firewalld.service
[root@master ~]# setenforce 0
3.1.2 两台SLAVE服务器配置
① 安装ntp、ntpdate服务
[root@localhost ~]# yum install ntp ntpdate -y
② 开启ntp服务,关闭防火墙、增强性安全功能
③ 时间同步master服务器
[root@localhost ~]# ntpdate 192.168.10.16
如果失败检查防火墙,setenforece,iptables是否关闭
④ 两台slave服务器配置相同
#master服务器同步阿里云时钟服务器
ntpdate ntp.aliyun.com
ntpdate 192.168.10.16
crontable -e
*/10 * * * * /usr/sbin/ntpdate 192.168.10.16内网ntp
外网阿里云
3.2、MySQL手工编译安装(这里忽略)
…省略部分内容
详情参考mysql编译安装
三台MySQL同时手工编译安装
3.3、配置主从同步
① master服务器修改配置文件
[root@master ~]# vi /etc/my.cnf
#在mysqld模块下修改一下内容
#开启二进制日志文件(之后生成的日志名为master-bin)
log_bin=master-bin
#开启从服务器日志同步
log_slave-updates=true
#主服务器id为1(不可重复)
server_id = 1
--------》wq重启服务
[root@master ~]# systemctl restart mysqld配置规则
[root@master ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.mysql> GRANT REPLICATION SLAVE ON *.* TO 'myslave'@'192.168.10.%' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.00 sec)
#按理说这里的*.*应该是指定数据库
#刷新权限表
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)规则解析:GRANT REPLICATION SLAVE ON *.* TO ‘myslave’@‘192.168.10.%’ IDENTIFIED BY ‘123456’;给从服务器提权,允许使用slave的身份复制master的所有数据库的所有表,并指定密码为123456查看master数据库状态
mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000001 | 412 | | | |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)cd /usr/loacal/mysql/data
#以上可见产生了master-bin.000001日志文件,定位为412
#从服务器需要定位到此处进行复制
② 从服务器配置
[root@slave1 ~]# vi /etc/my.cnf
#开启二进制日志文件
log-bin=master-bin
#设置server id为22,slave2 为23
server_id = 22
#从主服务器上同步日志文件记录到本地
relay-log=relay-log-bin
#定义relay-log的位置和名称(index索引)
relay-log-index=slave-relay-bin.index
--------》wqlog_bin=master-bin
server_id = 22
relay-log=relay-log-bin
relay-log-index=slave-relay-bin.indexrestart mysqld
ls /usr/local/mysql/data开启从服务器功能
[root@slave1 ~]# mysql -uroot -p
...............
mysql> change master to master_host='192.168.10.16',master_user='myslave',master_password='123456',master_log_file='master-bin.000001',master_log_pos=412;
Query OK, 0 rows affected, 2 warnings (0.02 sec)
#!!!这里的host是master的ip,名字是在master中设置slave的用户名密码,文件名和log代码可以使用show master status;在master中查看!!!
文件要和master的文件名和编号对应
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)查看从服务器状态
mysql> show slave status\G;
*************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: 192.168.226.129Master_User: myslaveMaster_Port: 3306Connect_Retry: 60Master_Log_File: master-bin.000001Read_Master_Log_Pos: 412Relay_Log_File: relay-log-bin.000002Relay_Log_Pos: 284Relay_Master_Log_File: master-bin.000001Slave_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: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 412Relay_Log_Space: 455Until_Condition: NoneUntil_Log_File: Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_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: NoLast_IO_Errno: 0Last_IO_Error: Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 11Master_UUID: c59043ec-5ad8-11ea-b895-000c29fe085bMaster_Info_File: /home/mysql/master.infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update itMaster_Retry_Count: 86400Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0
1 row in set (0.00 sec)
看到上面两个yes证明启动配置成功
同理、开启另一台从服务器同步
3.4、测试数据同步
在主服务器上创建一个数据库
mysql> create database work;
Query OK, 1 row affected (0.00 sec)mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| work |
+--------------------+
5 rows in set (0.00 sec)
在两台从服务器上直接查看数据库列表
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| work |
+--------------------+
5 rows in set (0.00 sec)以上,主从同步复制配置完成
2.1 MySQL的复制类型
- 基于语句的复制(STATEMENT, MySQL默认类型)
- 基于行的复制(ROW)
- 混合类型的复制(MIXED)