centos7配置mariadb一主多从(yum安装版)

centos安装maraidb一主多从

        • 一、安装ntp服务进行时间同步
        • 二、安装mariadb(即mysql服务)
          • master、slave1、slave2
        • 三、更改主从相关配置文件
          • master
          • slave1
          • slave2
        • 四、配置主从
          • master
          • slave1
          • slave2
        • 五、检测主从是否同步

操作系统版本 mysq版本 身份 ip地址
centos7.4-1 mariadb-5.5.56 master 192.168.19.201
centos7.4-2 mariadb-5.5.56 slave1 192.168.19.202
centos7.4-3 mariadb-5.5.56 slave2 192.168.19.203

一、安装ntp服务进行时间同步

master、slave1、slave2

[root@localhost ~]# yum -y install ntpdate
总下载量:87 k
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
ntpdate-4.2.6p5-29.el7.centos.2.x86_64.rpm             |  87 kB   00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在更新    : ntpdate-4.2.6p5-29.el7.centos.2.x86_64                  1/2 
  清理        : ntpdate-4.2.6p5-25.el7.centos.2.x86_64                  2/2 
  验证中      : ntpdate-4.2.6p5-29.el7.centos.2.x86_64                  1/2 
  验证中      : ntpdate-4.2.6p5-25.el7.centos.2.x86_64                  2/2 

更新完毕:
  ntpdate.x86_64 0:4.2.6p5-29.el7.centos.2                                  

完毕!
[root@localhost ~]# ntpdate time1.aliyun.com
28 Jul 13:09:35 ntpdate[15685]: step time server 203.107.6.88 offset -28507.029087 sec

三台服务器保持时间同步即可

二、安装mariadb(即mysql服务)

master、slave1、slave2
[root@localhost ~]# yum -y install mariadb mariadb-server
[root@localhost ~]# systemctl start mariadb
[root@localhost ~]# netstat -anptu | grep 3306
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      2549/mysqld
[root@localhost ~]# mysqladmin -uroot -p password 123123;
Enter password:						回车即可
[root@localhost ~]# mysql -uroot -p123123;
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.68-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)]>
MariaDB [(none)]>exit

三、更改主从相关配置文件

master

添加配置文件内容:
server-id=1
log-bin=master-log
skip_name_reslove=ON
innodb_file_per_table=ON

[root@localhost ~]# vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
server-id=1
log-bin=master-log
skip_name_resolve=ON
innodb_file_per_table=ON
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
[root@localhost ~]# systemctl restart mariadb
[root@localhost ~]# systemctl status mariadb
 mariadb.service - MariaDB database server
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled)
   Active: active (running) since  2022-07-28 13:39:36 CST; 40min ago
  Process: 2340 ExecStartPost=/usr/libexec/mariadb-wait-ready $MAINPID (code=exited, status=0/SUCCESS)
  Process: 2308 ExecStartPre=/usr/libexec/mariadb-prepare-db-dir %n (code=exited, status=0/SUCCESS)
 Main PID: 2339 (mysqld_safe)
   CGroup: /system.slice/mariadb.service
           ├─2339 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
           └─2549 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/m...

7 28 13:39:34 localhost.localdomain systemd[1]: Starting MariaDB datab...
7 28 13:39:34 localhost.localdomain mariadb-prepare-db-dir[2308]: Data...
7 28 13:39:34 localhost.localdomain mariadb-prepare-db-dir[2308]: If t...
7 28 13:39:34 localhost.localdomain mysqld_safe[2339]: 220728 13:39:34...
7 28 13:39:34 localhost.localdomain mysqld_safe[2339]: 220728 13:39:34...
7 28 13:39:36 localhost.localdomain systemd[1]: Started MariaDB databa...
Hint: Some lines were ellipsized, use -l to show in full.
slave1

添加配置文件内容:
server-id=2
relay-log=slave1-log
skip_name_resolve=ON
innodb_file_per_table=ON

[root@localhost ~]# vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
server-id=2
relay-log=slave1-log
skip_name_resolve=ON
innodb_file_per_table=ON
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
[root@localhost ~]# systemctl restart mariadb
[root@localhost ~]# systemctl status mariadb
 mariadb.service - MariaDB database server
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; disabled; vendor preset: disabled)
   Active: active (running) since  2022-07-28 13:42:30 CST; 52min ago
  Process: 16155 ExecStartPost=/usr/libexec/mariadb-wait-ready $MAINPID (code=exited, status=0/SUCCESS)
  Process: 16119 ExecStartPre=/usr/libexec/mariadb-prepare-db-dir %n (code=exited, status=0/SUCCESS)
 Main PID: 16154 (mysqld_safe)
   CGroup: /system.slice/mariadb.service
           ├─16154 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
           └─16368 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/...

7 28 13:42:28 localhost.localdomain systemd[1]: Starting MariaDB datab...
7 28 13:42:28 localhost.localdomain mariadb-prepare-db-dir[16119]: Dat...
7 28 13:42:28 localhost.localdomain mysqld_safe[16154]: 220728 13:42:2...
7 28 13:42:29 localhost.localdomain mysqld_safe[16154]: 220728 13:42:2...
7 28 13:42:30 localhost.localdomain systemd[1]: Started MariaDB databa...
Hint: Some lines were ellipsized, use -l to show in full.
slave2

添加配置文件内容:
server-id=3
relay-log=slave2-log
skip_name_resolve=ON
innodb_file_per_table=ON

[root@localhost ~]# vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
server-id=3
relay-log=slave2-log
skip_name_resolve=ON
innodb_file_per_table=ON
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
[root@localhost ~]# systemctl restart mariadb
[root@localhost ~]# systemctl status mariadb
 mariadb.service - MariaDB database server
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; disabled; vendor preset: disabled)
   Active: active (running) since  2022-07-28 13:47:28 CST; 46min ago
  Process: 16373 ExecStartPost=/usr/libexec/mariadb-wait-ready $MAINPID (code=exited, status=0/SUCCESS)
  Process: 16338 ExecStartPre=/usr/libexec/mariadb-prepare-db-dir %n (code=exited, status=0/SUCCESS)
 Main PID: 16372 (mysqld_safe)
   CGroup: /system.slice/mariadb.service
           ├─16372 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
           └─16586 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/...

7 28 13:47:26 localhost.localdomain systemd[1]: Starting MariaDB datab...
7 28 13:47:26 localhost.localdomain mariadb-prepare-db-dir[16338]: Dat...
7 28 13:47:26 localhost.localdomain mysqld_safe[16372]: 220728 13:47:2...
7 28 13:47:26 localhost.localdomain mysqld_safe[16372]: 220728 13:47:2...
7 28 13:47:28 localhost.localdomain systemd[1]: Started MariaDB databa...
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost ~]# mysql -uroot -p123123

四、配置主从

1.创建并授权主从同步用户
2.查看master状态并记住bin-log文件名称和POS号
3.配置两台从中指定主的ip地址以及登录用户和bin-log文件
4.开启主从模式并查看状态

master
[root@localhost ~]# mysql -uroot -p123123;
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.56-MariaDB MariaDB Server

Copyright (c) 2000, 2017, 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,replication client on *.* to 'slave'@'192.168.19.%' identified by "123123";
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;
*************************** 1. row ***************************
            File: master-log.000001
        Position: 493
    Binlog_Do_DB: 
Binlog_Ignore_DB: 
1 row in set (0.00 sec)

ERROR: No query specified
slave1
[root@localhost ~]# mysql -uroot -p123123
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.56-MariaDB MariaDB Server

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

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

MariaDB [(none)]> change master to
    -> master_host="192.168.19.201",
    -> master_user="slave",
    -> master_password="123123",
    -> master_log_file="master-log.000001",
    -> master_log_pos=493;
Query OK, 0 rows affected (0.00 sec)

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

MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.19.201
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-log.000001
          Read_Master_Log_Pos: 493
               Relay_Log_File: slave1-log.000002
                Relay_Log_Pos: 530
        Relay_Master_Log_File: master-log.000001
             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: 493
              Relay_Log_Space: 819
              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)

ERROR: No query specified 
slave2
[root@localhost ~]# mysql -uroot -p123123
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.56-MariaDB MariaDB Server

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

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

MariaDB [(none)]> change master to
   ->master_host="192.168.19.201",
   ->master_user="slave",
   ->master_passowrd="123123",
   ->master_passos=493;
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> start slave;
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.19.201
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-log.000001
          Read_Master_Log_Pos: 493
               Relay_Log_File: slave2-log.000002
                Relay_Log_Pos: 530
        Relay_Master_Log_File: master-log.000001
             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: 493
              Relay_Log_Space: 819
              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)

ERROR: No query specified

五、检测主从是否同步

master上操作,创建数据库

MariaDB [(none)]> create database school;
Query OK, 1 row affected (0.00 sec)

slave1查看是否同步

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

slave2查看是否同步

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

以上就是关于mariadb主从的相关配置与操作了,感谢大家的支持!

你可能感兴趣的:(mariadb,linux,服务器)