mysql主从复制操作详细步骤

MySQL主从复制、读写分离

主从复制	MySQL Replication   


实现过程:

Master服务器上进行写操作时,会把写操作记录到本地的二进制日志文件中,本地会启动sql线程,该线程会通过网络把事件传递给slave从服务器
Slave服务器会启动I/O thread,负责接收二进制日志事件,并写入本地的relay log中继日志
Slave服务器启动SQL thread,负责读取中继日志中的事件,在从服务器本地执行,从而保证主、从服务器数据同步

作用:
高可用
实现冷备份
读写分离

示例1:搭建主从复制架构

master.linux.com	 10.0.0.68    主服务器
slave.linux.com	     10.0.0.69    从服务器 

两台机器都需要配置
1、关闭 SELinux, firewalld, 时间同步, yum源仓库

关闭selinux
[root@master ~]# cat /etc/selinux/config

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

重启生效

安装epel源

[root@master ~]# yum install -y epel-release

关闭防火墙:

[root@master ~]# systemctl stop firewalld.service
[root@master ~]# systemctl disable firewalld.service     #开机自动关闭防火墙
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

配置时间同步:

[root@master ~]# yum install -y ntp
[root@master ~]# ntpdate ntp1.aliyun.com
13 Nov 14:53:11 ntpdate[12167]: adjust time server 120.25.115.20 offset 0.010383 sec

2、在两台服务器安装数据库

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

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

3、 在master服务器上启用二进制日志

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

[mysqld]
server_id=101
log_bin=master-bin

启动mysql设置开机自启

[root@master ~]# systemctl start mariadb
[root@master ~]# systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service

[root@master ~]# netstat -antp | grep mysql
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      12789/mysqld

4、导入jiaowu数据库,模拟数据量

jiaowu.sql下载地址

[root@master ~]# mysql -u root -p < jiaowu.sql
Enter password:

5、在slave服务器配置server_id, 启动服务

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

[mysqld]
server_id=102
read_only=1

启动服务并设置开机自启

[root@slave ~]#  systemctl start mariadb
[root@slave ~]#  systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.

6、 在master上备份数据,在slave上恢复数据

[root@master ~]# mysqldump -u root -p --lock-all-tables --master-data=2 --all-databases > /tmp/all.sql
[root@master ~]# scp /tmp/all.sql root@10.0.0.69:/root
The authenticity of host '10.0.0.69 (10.0.0.69)' can't be established.
ECDSA key fingerprint is SHA256:AjENfpwMzubqhEfB8JMYHWbHl+5e7x5NMBPvDNYHmYc.
ECDSA key fingerprint is MD5:be:69:ad:1d:a4:54:49:22:9f:98:56:df:8a:00:7c:52.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.0.69' (ECDSA) to the list of known hosts.
root@10.0.0.69's password:
all.sql                                      100%  507KB  40.3MB/s   00:00


root@slave ~]# mysql -u root -p < /root/all.sql 

7、 在master服务器上创建一个具有复制权限的远程用户

MariaDB [(none)]> grant replication slave on *.* to 'spuser'@'10.0.0.69' identified by 'redhat';
Query OK, 0 rows affected (0.00 sec)

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

8、在slave上连接主服务器

master查看自己的logfile:

MariaDB [(none)]> show master status;
+-------------------+----------+--------------+------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000003 |     5242 |              |                  |
+-------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

在slave连接主服务器:

MariaDB [(none)]> change master to
-> master_host="10.0.0.68",
-> master_user="spuser",
-> master_password="redhat",
-> master_log_file="master-bin.000004",
-> master_log_pos=5242;

MariaDB [(none)]> start slave;        #启动复制线程  
MariaDB [(none)]> show slave status\G;        #查看从服务器上线程的状态 
*************************** 1. row ***************************
           Slave_IO_State: Waiting for master to send event
              Master_Host: 10.0.0.68
              Master_User: spuser
              Master_Port: 3306
            Connect_Retry: 60
          Master_Log_File: master-bin.000004
      Read_Master_Log_Pos: 5469
           Relay_Log_File: mariadb-relay-bin.000002
            Relay_Log_Pos: 757
    Relay_Master_Log_File: master-bin.000004
         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: 5469
          Relay_Log_Space: 1053
          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: 101
1 row in set (0.00 sec)

ERROR: No query specified

验证主从复制成功
停止从服务器,在主服务上进行修改操作,再次启动从服务器,从服务器会自动继续复制数据
因为从服务器启动后,默认会在其数据目录下,生成一个master.info,该文件中会自动记录主服务器的连接信息

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值