Linux学习之MySQL主从复制

MySQL配置一主一从

环境准备:

两台服务器: Master:192.168.88.53,Slave:192.168.88.54

在两台服务器上安装mysql-server

sql 复制代码
# 配置主服务器192.168.88.53
# 启用binlog日志
[root@mysql53 ~]# yum -y  install mysql-server  mysql
[root@mysql53 ~]# vim /etc/my.cnf.d/mysql-server.cnf
[mysqld]
server-id=53
log-bin=mysql53
[root@mysql53 ~]# systemctl  start mysqld
# 用户授权
[root@mysql53 ~]# mysql
mysql> create user repluser@"%" identified by "123456";
Query OK, 0 rows affected (0.11 sec)
mysql> grant replication slave on *.*  to repluser@"%";
Query OK, 0 rows affected (0.09 sec)
# 查看日志信息
mysql> show master status;
+----------------+----------+--------------+------------------+-------------------+
| File           | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+----------------+----------+--------------+------------------+-------------------+
| mysql53.000002 |      667 |              |                  |                   |
+----------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

将Slave192.168.88.54配置为从数据库服务

sql 复制代码
# 指定server-id并启动数据库服务
[root@mysql54 ~]# yum -y  install mysql-server  mysql
[root@mysql54 ~]# vim /etc/my.cnf.d/mysql-server.cnf
[mysqld]
server-id=54
[root@mysql54 ~]# systemctl  start mysqld
# 登陆服务指定主服务器信息
mysql> change master to master_host="192.168.88.53",master_user="repluser",master_password="123456",master_log_file="mysql53.000002",master_log_pos=667;
Query OK, 0 rows affected, 8 warnings (0.85 sec)
# 启动slave 进程
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.10 sec)
# 查看信息状态
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for source to send event
                  Master_Host: 192.168.88.53
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql53.000002
          Read_Master_Log_Pos: 667
               Relay_Log_File: mysql54-relay-bin.000002
                Relay_Log_Pos: 322
        Relay_Master_Log_File: mysql53.000002
             Slave_IO_Running: Yes           # IO线程(YES表示正常)
            Slave_SQL_Running: Yes           # SQL线程(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: 667
              Relay_Log_Space: 533
              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: 53
                  Master_UUID: bb19b901-52ca-11ee-86a6-525400605619
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
       Master_public_key_path: 
        Get_master_public_key: 0
            Network_Namespace: 
1 row in set, 1 warning (0.00 sec)
# 在主服务器添加用户,给客户端连接使用
mysql> create user plj@'%' identified by "123456";
Query OK, 0 rows affected (0.10 sec)
mysql> grant all privileges on gamedb.* to plj@'%';
Query OK, 0 rows affected (0.15 sec)
# 客户端连接主服务器存储数据
[root@mysql53 ~]# mysql -h192.168.88.53 -uplj -p123456
mysql> create database gamedb;
Query OK, 1 row affected (0.12 sec)
mysql> create table gamedb.user(name char(10),class char(3));
Query OK, 0 rows affected (1.45 sec)
mysql> insert into gamedb.user values("yaya","nsd");
Query OK, 1 row affected (0.08 sec)
mysql> select * from gamedb.user;
+------+-------+
| name | class |
+------+-------+
| yaya | nsd   |
+------+-------+
1 row in set (0.00 sec)

# 客户端连接从服务器查看数据
# -e 命令行下执行数据库命令
[root@mysql50 ~]# mysql -h192.168.88.53 -uplj -p123456 -e "select * from gamedb.user"
mysql: [Warning] Using a password on the command line interface can be insecure.
+------+-------+
| name | class |
+------+-------+
| yaya | nsd   |
+------+-------+

MySQL配置一主多从

环境:再准备一台服务器192.168.88.55

配置192.168.88.55为192.168.88.53主机的从服务器

sql 复制代码
# 1) 指定mysql55主机的server-id并重启数据库服务
[root@mysql55 ~]# yum -y  install mysql-server  mysql
[root@mysql55 ~]# vim /etc/my.cnf.d/mysql-server.cnf
[mysqld]
server-id=55
[root@mysql55 ~]# systemctl  start mysqld
# 2)确保与主服务器数据一致。
# 在mysql53执行备份命令前查看日志名和偏移量 ,mysql55 在当前查看到的位置同步数据
mysql> show master status;
+----------------+----------+--------------+------------------+-------------------+
| File           | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+----------------+----------+--------------+------------------+-------------------+
| mysql53.000002 |     1871 |              |                  |                   |
+----------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
# 在主服务器存做完全备份
[root@mysql53 ~]# mysqldump -B gamedb > /root/gamedb.sql
# 将主服务器把备份文件拷贝给从服务器mysql55
[root@mysql53 ~]# scp /root/gamedb.sql root@192.168.88.55:/root/
[root@mysql55 ~]# mysql </root/gamedb.sql
# 3)在MySQL55主机指定主服务器信息
[root@mysql55 ~]# mysql
mysql> change master to master_host="192.168.88.53",master_user="repluser",master_password="123456",master_log_file="mysql53.000002",master_log_pos=1871;
Query OK, 0 rows affected, 8 warnings (0.88 sec)

mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.05 sec)
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for source to send event
                  Master_Host: 192.168.88.53
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql53.000002
          Read_Master_Log_Pos: 1871
               Relay_Log_File: mysql55-relay-bin.000002
                Relay_Log_Pos: 322
        Relay_Master_Log_File: mysql53.000002
             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: 1871
              Relay_Log_Space: 533
              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: 53
                  Master_UUID: bb19b901-52ca-11ee-86a6-525400605619
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
       Master_public_key_path: 
        Get_master_public_key: 0
            Network_Namespace: 
1 row in set, 1 warning (0.00 sec)

客户端测试配置

sql 复制代码
# 连接主服务器存储数据
[root@mysql50 ~]# mysql -h192.168.88.53 -uplj -p123456
mysql> insert into  gamedb.user values("tt","aid");
Query OK, 1 row affected (0.14 sec)
mysql> insert into  gamedb.user values("mm","uid");
Query OK, 1 row affected (0.13 sec)
# 在client50 分别连接2个从服务器查看数据
# 连接从服务器54查看数据
[root@mysql50 ~]# mysql -h192.168.88.54 -uplj -p123456 -e 'select * from gamedb.user'
mysql: [Warning] Using a password on the command line interface can be insecure.
+------+-------+
| name | class |
+------+-------+
| yaya | nsd   |
| tt   | aid   |
| mm   | uid   |
+------+-------+
# 连接从服务器55查看数据
[root@mysql50 ~]# mysql -h192.168.88.55 -uplj -p123456 -e 'select * from gamedb.user'
mysql: [Warning] Using a password on the command line interface can be insecure.
+------+-------+
| name | class |
+------+-------+
| yaya | nsd   |
| tt   | aid   |
| mm   | uid   |
+------+-------+
相关推荐
Decay__6 小时前
05_MySQL 常用 SQL 大全 —— 诊断、管理、优化辅助 SQL 与三大系统库速查
运维·数据库·sql·mysql·架构
代码山河8 小时前
Python就业方向全解析:后端、爬虫、数据分析、AI你选哪个?
python·学习·架构·教程·面向对象·项目
HideF8 小时前
Linux
linux·服务器·网络
释厄6239 小时前
AI学习层更新与跨体转移律——人类社会同构·不缺理解缺 U
人工智能·学习
Ruiery10 小时前
Linux 6.6内核内存管理深度解析(十三):slab/slub 分配器
linux·运维·服务器
Decay__12 小时前
04_MySQL DBA 日常运维手册 —— 会话管理、锁与阻塞、事务监控、慢查询与日志巡检
运维·数据库·sql·mysql·架构
丰锋ff13 小时前
压缩命令笔记
linux
小嘉丶学长13 小时前
Day14 · 字典与集合
java·开发语言·网络·python·学习·1024程序员节
小李不想当小白13 小时前
SPI通信外设与W25Q64 (硬件SPI)(STM32标准库学习笔记)
笔记·stm32·单片机·嵌入式硬件·学习·嵌入式
GeW14 小时前
产业新优势基础软件的人才底盘,从Linux与数据库开始
linux·数据库