centos7下mysql主从

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了centos7下mysql主从相关的知识,希望对你有一定的参考价值。

1.环境说明

centos7下最小安装环境,IP分配如下,默认yum安装mysql也会安装mariadb。

IP
master 192.168.0.151
slave 192.168.0.152

2.yum直接安装mariadb

[[email protected] ~]# yum -y install mariadb mariadb-server #主从一样安装

3.配置master

[[email protected] ~]# vim /etc/my.cnf
[mysql] 下添加如下
server-id=1  #master id,不可以和slave重复
log_bin=master-bin
[[email protected] ~]# systemctl restart mariadb
[[email protected] ~]# systemctl enable mariadb
[[email protected] ~]# mysqladmin -u root password 123456  #默认没有密码,这里给root设置一个密码
[[email protected] ~]# mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

MariaDB [(none)]> GRANT REPLICATION SLAVE ON *.* to ‘mysqlback‘@‘192.168.0.152‘ identified by ‘123456‘;   #给slave授权
Query OK, 0 rows affected (0.00 sec)

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

MariaDB [(none)]> show master status\G   #查看master状态,记录下 FILE 及 Position 的值
*************************** 1. row ***************************
            File: master-bin.000003
        Position: 617
    Binlog_Do_DB: 
Binlog_Ignore_DB: 
1 row in set (0.00 sec)

MariaDB [(none)]> 

4.slave配置

[[email protected] ~]# vim /etc/my.cnf

[mysqld]下添加如下
server-id=2   #slave id,不可以和master重复
relay-log-index=slave-relay-bin.index
relay-log=slave-relay-bin
relay_log_recovery=1
[[email protected] ~]# systemctl restart mariadb
[[email protected] ~]# systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
[[email protected] ~]# mysqladmin -u root password 123456
[[email protected] ~]# mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

MariaDB [(none)]> slave stop;
Query OK, 0 rows affected, 1 warning (0.00 sec)

MariaDB [(none)]> change master to master_host=‘192.168.0.151‘,master_user=‘mysqlback‘,master_password=‘123456‘,master_log_file=‘master-bin.000003‘,master_log_pos=617;
Query OK, 0 rows affected (0.01 sec)

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

MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.0.151
                  Master_User: mysqlback
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000003
          Read_Master_Log_Pos: 617
               Relay_Log_File: slave-relay-bin.000002
                Relay_Log_Pos: 530
        Relay_Master_Log_File: master-bin.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: 617
              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)
通常看slave状态下IO和SQL是否为yes来判定主从关系是否OK
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

到此,mariadb主从基本配置完成。

5.验证

我们在master上新建一个数据库crm
技术图片
在slave查看
技术图片

以上是关于centos7下mysql主从的主要内容,如果未能解决你的问题,请参考以下文章

centos7下mysql5.6的主从复制

centos7下配置mysql5.7.24主从复制

Centos7 下Mysql5.7.18的安装和简单主从

centos7下mysql5.6.30配置单机多实例主从半同步复制

在Centos7.6上配置mysql主从

在Centos7.6上配置mysql主从