CentOS8部署MariaDB分布式主从数据库服务

目录

一、安装环境

二、初始化数据库

三、配置主节点masterdb 

四、配置从节点slavedb

五、验证数据库主从服务


一、安装环境准备

1、节点规划

准备两台虚拟主机,分别扮演主数据库节点(masterdb)和从数据库节点(slavedb)

IP地址主机名称节点
192.168.18.8masterdb主数据库节点
192.168.18.88slavedb从数据库节点

2、主数据库节点环境与设置

[root@red3212 ~]# hostnamectl set-hostname masterdb
[root@red3212 ~]# exit
[root@master ~]# hostnamectl 
   Static hostname: masterdb
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 651d4c1ca62b4b45aa3f2adb5bfd2230
           Boot ID: 75e152eb882c4428b12fe310c4bf91ae
    Virtualization: vmware
  Operating System: CentOS Linux 8
       CPE OS Name: cpe:/o:centos:centos:8
            Kernel: Linux 4.18.0-240.el8.x86_64
      Architecture: x86-64

[root@masterdb ~]# ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.18.8  netmask 255.255.255.0  broadcast 192.168.18.255
        inet6 fe80::5fa9:b95a:e382:5831  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:82:a2:fe  txqueuelen 1000  (Ethernet)
        RX packets 34325  bytes 47142373 (44.9 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 14183  bytes 1298293 (1.2 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

3、从数据库节点环境与设置

[root@red3212 ~]# hostnamectl set-hostname slavedb
[root@red3212 ~]# exit
[root@slavedb ~]# hostnamectl 
   Static hostname: slavedb
         Icon name: computer-vm
           Chassis: vm
        Machine ID: d8c2c33a40804fb4abf233975a7e17e5
           Boot ID: f72a0bf6ede743f9a8fc1f8606bbeef1
    Virtualization: vmware
  Operating System: CentOS Linux 8
       CPE OS Name: cpe:/o:centos:centos:8
            Kernel: Linux 4.18.0-240.el8.x86_64
      Architecture: x86-64

[root@slavedb ~]# ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.18.88  netmask 255.255.255.0  broadcast 192.168.18.255
        inet6 fe80::444d:4556:783:d78f  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:60:ad:70  txqueuelen 1000  (Ethernet)
        RX packets 91  bytes 12341 (12.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 147  bytes 19041 (18.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

4、配置防火墙和SELinux服务

虽然可以直接关闭防火墙(systemctl stop firewalld)和SELinux服务(setenforce 0),但实际应用不建议这样操作。

建议在主从两个节点中均执行以下命令配置防火墙。

[root@masterdb ~]# firewall-cmd --permanent --zone=public --add-service=mysql 
success
[root@masterdb ~]# firewall-cmd --reload 
success

5、配置hosts文件

两个节点均作以下配置

[root@masterdb ~]# vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.18.8  masterdb
192.168.18.88 slavedb

6、配置本地软件仓库

(1)确保虚拟机的光驱保持已连接,并使用CentOS8的ISO映像文件,如下图所示

(2)创建并编辑本地软件仓库,两个节点均执行以下操作

[root@masterdb ~]# vim /etc/yum.repos.d/local.repo 
[centos-appstream]
name=centos8-appstream
baseurl=file:///media/cdrom/AppStream
gpgcheck=0
enabled=1
[centos-baseos]
name=centos8-baseos
baseurl=file:///media/cdrom/BaseOS
gpgcheck=0
enabled=1

(3)挂载本地软件仓库,两个节点均执行以下操作

[root@masterdb ~]# mkdir  -p  /media/cdrom
[root@masterdb ~]# mount  /dev/cdrom  /media/cdrom
mount: /media/cdrom: WARNING: device write-protected, mounted read-only.

7、安装MariaDB数据库服务程序,两个节点均执行以下操作

[root@masterdb ~]# dnf -y install mariadb mariadb-server

8、启动并设置开机自启动mariadb服务,两个节点均执行以下操作

[root@masterdb ~]# systemctl start mariadb
[root@masterdb ~]# systemctl enable mariadb
Created symlink /etc/systemd/system/mysql.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/mysqld.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service → /usr/lib/systemd/system/mariadb.service.

二、初始化数据库

初始化数据库,两个节点均执行以下操作

[root@masterdb ~]# mysql_secure_installation 

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):         #默认直接按Enter键
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y            #输入y,设置数据库管理员root的密码
New password:                         #输入密码,比如666666
Re-enter new password:                 #再次输入密码,比如666666
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y        #输入y,删除数据库匿名账号
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] n        #输入n,允许root远登登录
 ... skipping.

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y        #输入y,删除测试数据库
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y        #输入y,重新加载授权表
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

三、配置主节点masterdb 

1、修改数据库配置文件/etc/my.cnf.d/mariadb-server.cnf ,添加如下最后三行内容。

[root@masterdb ~]# vim /etc/my.cnf.d/mariadb-server.cnf 
……此处省略部分信息……
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mariadb/mariadb.log
pid-file=/run/mariadb/mariadb.pid 
log_bin = mysql-bin            #记录操作日志
binlog_ignore_db = mysql        #不同步mysql系统数据库
server_id=8                    #数据库集群中的每个节点ID都不相同
                #server_id一般取值为该服务器IP地址的最后字段的值,如192.168.18.8就取值8
……此处省略部分信息……

2、重启数据库服务,并进入数据库

[root@masterdb ~]# systemctl restart mariadb.service 
[root@masterdb ~]# mysql -u root -p666666
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 10.3.28-MariaDB-log 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)]> 

3、在主节点上建立帐户并授权slave

在主节点上授权允许root用户在任何客户端机器上登录数据库,同时在主节点上创建一个名为zhangsan的数据库用户,并授权该账号连接从节点,这里也可以用具体的IP地址代替主机名slavedb,同时赋予从节点同步主节点数据库的权限。

注:%表示所有主机。

MariaDB [(none)]> grant all privileges on *.* to 'root'@'%' identified by '666666';
Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]> grant replication slave on *.* to 'zhangsan'@'slavedb' identified by '666666';
Query OK, 0 rows affected (0.000 sec)
MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 |      709 |              | mysql            |
+------------------+----------+--------------+------------------+
1 row in set (0.001 sec)

MariaDB [(none)]> exit
Bye

四、配置从节点slavedb

1、与主节点一样,确保先完成前面的数据库mariadb的安装、启动、初始化和配置防火墙等操作,然后修改/etc/hosts文件。

2、接下来修改从节点slavedb的数据库配置文件/etc/my.cnf.d/mariadb-server.cnf ,添加如下所示最后三行内容。

[root@slavedb ~]# vim /etc/my.cnf.d/mariadb-server.cnf 
……此处省略部分信息……
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mariadb/mariadb.log
pid-file=/run/mariadb/mariadb.pid 
log_bin = mysql-bin            #记录操作日志
binlog_ignore_db = mysql        #不同步mysql系统数据库
server_id=88                    #数据库集群中的每个节点ID都不相同
          #server_id一般取值为该服务器IP地址的最后字段的值,从节点IP为192.168.18.88就取值88
……此处省略部分信息……

3、配置从节点连接到数据库

其中的master_host用于指定主节点的主机名,master_user用于指定在主节点中创建并授权的用户账号,master_password则是对应的账号密码。

[root@slavedb ~]# systemctl restart mariadb.service 
[root@slavedb ~]# mysql -u root -p666666
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.3.28-MariaDB-log 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)]> change master to master_host='masterdb',master_user='zhangsan',master_password='666666';
Query OK, 0 rows affected (0.004 sec)

4、开启从节点服务

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

MariaDB [(none)]> show slave status \G
*************************** 1. row ***************************
                Slave_IO_State: Waiting for master to send event
                   Master_Host: masterdb
                   Master_User: zhangsan
                   Master_Port: 3306
                 Connect_Retry: 60
               Master_Log_File: mysql-bin.000002
           Read_Master_Log_Pos: 1111
                Relay_Log_File: mariadb-relay-bin.000003
                 Relay_Log_Pos: 1410
         Relay_Master_Log_File: mysql-bin.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: 1111
               Relay_Log_Space: 2473
               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: 8
                Master_SSL_Crl: 
            Master_SSL_Crlpath: 
                    Using_Gtid: No
                   Gtid_IO_Pos: 
       Replicate_Do_Domain_Ids: 
   Replicate_Ignore_Domain_Ids: 
                 Parallel_Mode: conservative
                     SQL_Delay: 0
           SQL_Remaining_Delay: NULL
       Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
              Slave_DDL_Groups: 6
Slave_Non_Transactional_Groups: 0
    Slave_Transactional_Groups: 0
1 row in set (0.000 sec)
MariaDB [(none)]> exit
Bye

若Slave_IO_Running和Slave_SQL_Running的状态均为Yes时,就表示主从数据库集群配置成功。

五、验证数据库主从服务

1、在主节点创建数据库

先在主节点masterdb中创建数据库test,并在库test中创建表company,插入表数据,创建完成后,查看表comany的数据。

[root@masterdb ~]# mysql -u root -p666666
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 41
Server version: 10.3.28-MariaDB-log 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)]> create database test;
Query OK, 1 row affected (0.000 sec)

MariaDB [(none)]> use test;
Database changed
MariaDB [test]> create table company (id int not null primary key, name varchar(50),addr varchar(255));
Query OK, 0 rows affected (0.061 sec)

MariaDB [test]> insert into company values(1,"Baidu","BeiJing");
Query OK, 1 row affected (0.013 sec)

MariaDB [test]> select * from company;
+----+-------+---------+
| id | name  | addr    |
+----+-------+---------+
|  1 | Baidu | BeiJing |
+----+-------+---------+
1 row in set (0.001 sec)

2、在从节点上验证数据同步功能

登录从节点slavedb节点的数据库,查看数据库列表。找到test数据库,查询表和其中的数据。

[root@slavedb ~]# mysql -u root -p666666
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 15
Server version: 10.3.28-MariaDB-log 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)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.001 sec)

MariaDB [(none)]> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [test]> show tables;
+----------------+
| Tables_in_test |
+----------------+
| company        |
+----------------+
1 row in set (0.000 sec)

MariaDB [test]> select * from company;
+----+-------+---------+
| id | name  | addr    |
+----+-------+---------+
|  1 | Baidu | BeiJing |
+----+-------+---------+
1 row in set (0.000 sec)

可见,主从数据库数据可以成功同步。

到此,分布式的主从数据库成功配置完成。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值