【终端】CentOS7下MySQL的主从配置

本文使用Xshell作为虚拟机连接的工具

简介

本文使用CentOS系统,使用命令行的方式配置MySQL的主从

软件

  • VMWare15
  • CentOS7
  • MySQL5.7

过程

主从配置需要满足以下条件:
需要两台虚拟机,并且需要安装并配置完成MySQL
两台虚拟机需要将访问端口添加到防火墙,或者直接关闭防火墙
主从数据库中都需要预先创建好相同的、需要同步的数据库
以上需要按照实际情况进行处理

一、主服务器配置

1.创建同步账号

下面的zenith和123456为同步账号的应户名和密码,具体请按照实际情况编写

(1)进入mysql

[root@localhost mysql]# mysql -u root -p

(2)创建同步账号

mysql> create user 'zenith'@'%' identified by '123456';

(3)授予replication slave权限

mysql> grant replication slave on *.* to 'zenith'@‘%’;

2.编写my.cnf配置
(1)编写my.cnf内容

[root@localhost mysql]# vi /etc/my.cnf

主服务器配置内容需要按照实际情况处理

log_bin=master-bin
log_bin_index = master-bin.index
server-id=10
expire-logs-days=30
binlog_ignore_db=mysql
binlog_ignore_db=information_schemab
binlog_ignore_db=performation_schema
binlog_ignore_db=sys
binlog_do_db=zenith_star

内容说明如下:

参数说明
log_bin启动MySQL二进制日志
server-id服务器唯一标识
expire-logs-days二进制日期的有效期
binlog_ignore_db不需要同步的数据库
binlog_do_db需要同步的数据库

(2)重启mysql

[root@localhost mysql]# systemctl restart mysqld

3.查看主服务器状态

[root@localhost mysql]# mysql -u root -p
mysql> show master status;

显示如下:

mysql> show master status;
+-------------------+----------+-----------------+--------------------------------------------------+-------------------+
| File              | Position | Binlog_Do_DB    | Binlog_Ignore_DB                                 | Executed_Gtid_Set |
+-------------------+----------+-----------------+--------------------------------------------------+-------------------+
| master-bin.000008 | 35721572 | zenith_star | mysql,information_schema,performance__schema,sys |                   |
+-------------------+----------+-----------------+--------------------------------------------------+-------------------+
1 row in set (0.00 sec)

这里需要记录File和Position内容数据,从服务器在进行配置时需要使用

二、从服务器配置

1.编写my.cnf配置
(1)编写my.cnf

[root@localhost mysql]# vi /etc/my.cnf
server-id = 2
relay-log = slave-relay-bin
relay-log-index = slave-relay-bin.index

解析如下:

参数说明
server-id服务器id
relay-log日志文件(这里将主服务器的日志读取到从服务器,然后线程读取此日志内容并应用到从服务器)
relay-log-index定义relay-log的位置和名称

2.配置slave

[root@localhost mysql]# mysql -u root -p
mysql> change master to master_host='192.168.1.1',master_post=3306,master_user='zenith',master_password='123456',master_log_file='master_bin.000001',master_log_pos=2237;

参数说明如下:

参数说明
master_host主服务器地址
master_post主服务器端口
master_user同步账号
master_password同步账号的密码
master_log_file主服务器的日志文件名称
master_log_pos主服务器的日志文件位置

3.检查slave连接状态
(1)启动slave

mysql> start slave;

(2)检查slave

mysql> show slave status\G;

显示如下:

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.153.155
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000008
          Read_Master_Log_Pos: 35721572
               Relay_Log_File: slave-relay-bin.000002
                Relay_Log_Pos: 321
        Relay_Master_Log_File: master-bin.000008
             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: 35721572
              Relay_Log_Space: 528
              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: 4
                  Master_UUID: e2a8b217-c286-11eb-bc93-000c2909a371
             Master_Info_File: /var/lib/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave 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: 
1 row in set (0.00 sec)

其中Slave_IO_Running和Slave_SQL_Running均需要显示为Yes才表示连接成功。任何一个显示Connecting,则表示未连接到主服务器,需要检查连接问题
这里需要注意同步的账号和密码、主服务器的ip和端口、主服务器的日志文件名称和位置这三部分数据,确保数据一致

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值