centos7下mysql主从

1.环境说明

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

IP
master 192.168.0.151
slave 192.168.0.152

2.yum直接安装mariadb

[root@localhost ~]# yum -y install mariadb mariadb-server #主从一样安装

3.配置master

[root@localhost ~]# vim /etc/my.cnf
[mysql] 下添加如下
server-id=1  #master id,不可以和slave重复
log_bin=master-bin
[root@localhost ~]# systemctl restart mariadb
[root@localhost ~]# systemctl enable mariadb
[root@localhost ~]# mysqladmin -u root password 123456  #默认没有密码,这里给root设置一个密码
[root@localhost ~]# 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配置

[root@localhost ~]# 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
[root@localhost ~]# systemctl restart mariadb
[root@localhost ~]# systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
[root@localhost ~]# mysqladmin -u root password 123456
[root@localhost ~]# 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
centos7下mysql主从
在slave查看
centos7下mysql主从

猜你喜欢

转载自blog.51cto.com/11573159/2410128