mariadb数据库集群

db1  192.168.7.10

db2  192.168.7.11

db3  192.168.7.12

一、修改db1、db2、db3数据库名称

[root@db1 ~]# sestatus
SELinux status:                 disabled
[root@db1 ~]# systemctl status firewalld
○ firewalld.service - firewalld - dynamic firewall daemon
     Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; preset: enabled)
     Active: inactive (dead)
       Docs: man:firewalld(1)
[root@db2 ~]# sestatus
SELinux status:                 disabled
[root@db2 ~]# systemctl status firewalld
○ firewalld.service - firewalld - dynamic firewall daemon
     Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; preset: enabled)
     Active: inactive (dead)
       Docs: man:firewalld(1)
[root@db3 ~]# sestatus
SELinux status:                 disabled
[root@db3 ~]# systemctl status firewalld
○ firewalld.service - firewalld - dynamic firewall daemon
     Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; preset: enabled)
     Active: inactive (dead)
       Docs: man:firewalld(1)

二、编辑db1、db2、db3时间同步配置文件

[root@db1 ~]# vim /etc/chrony.conf
#pool 2.rocky.pool.ntp.org iburst
pool ntp.aliyun.com iburst#同步阿里云的时间
[root@db2 ~]# vim /etc/chrony.conf

#pool 2.rocky.pool.ntp.org iburst
pool ntp.aliyun.com iburst#同步阿里云的时间
[root@db3 ~]# vim /etc/chrony.conf

#pool 2.rocky.pool.ntp.org iburst
pool ntp.aliyun.com iburst#同步阿里云的时间

三、设置时间同步

[root@db1 ~]# systemctl enable --now chronyd
[root@db1 ~]# chronyc -v
chronyc (chrony) version 4.5 (+READLINE +SECHASH +IPV6 +DEBUG)
[root@db1 ~]# 
[root@db1 ~]# chronyc sources -v

  .-- Source mode  '^' = server, '=' = peer, '#' = local clock.
 / .- Source state '*' = current best, '+' = combined, '-' = not combined,
| /             'x' = may be in error, '~' = too variable, '?' = unusable.
||                                                 .- xxxx [ yyyy ] +/- zzzz
||      Reachability register (octal) -.           |  xxxx = adjusted offset,
||      Log2(Polling interval) --.      |          |  yyyy = measured offset,
||                                \     |          |  zzzz = estimated error.
||                                 |    |           \
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^* stratum2-1.ntp.mow01.ru.>     2   6   357     9  -3766us[-4836us] +/-   61ms
^+ ntp8.flashdance.cx            2   6   377    11    +14ms[  +13ms] +/-  113ms
^+ time.cloudflare.com           3   6   377    12   +285us[ -784us] +/-  115ms
^+ time.cloudflare.com           3   6   377    10   +699us[ -370us] +/-  116ms
[root@db1 ~]# date
Wed Mar 12 03:03:14 AM EDT 2025
[root@db1 ~]# 
[root@db1 ~]# 
[root@db1 ~]# clock -w
[root@db1 ~]# dnf -y install mariadb-server-galera			安装数据库集群

[root@db2 ~]# systemctl enable --now chronyd
[root@db2 ~]# 
[root@db2 ~]# 
[root@db2 ~]# chronyc sources -v

  .-- Source mode  '^' = server, '=' = peer, '#' = local clock.
 / .- Source state '*' = current best, '+' = combined, '-' = not combined,
| /             'x' = may be in error, '~' = too variable, '?' = unusable.
||                                                 .- xxxx [ yyyy ] +/- zzzz
||      Reachability register (octal) -.           |  xxxx = adjusted offset,
||      Log2(Polling interval) --.      |          |  yyyy = measured offset,
||                                \     |          |  zzzz = estimated error.
||                                 |    |           \
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^+ time.cloudflare.com           3   6   377   113  -2995us[-2940us] +/-  114ms
^+ ntp1.flashdance.cx            2   6   377    50   -337us[ -171us] +/-  100ms
^* ntp8.flashdance.cx            2   6   377    49    +15ms[  +15ms] +/-  114ms
^+ ntp.ams1.nl.leaseweb.net      2   6   377    50    +17ms[  +17ms] +/-  165ms
[root@db2 ~]# 
[root@db2 ~]# 
[root@db2 ~]# date
Wed Mar 12 03:05:28 PM CST 2025
[root@db2 ~]# 
[root@db2 ~]# clock -w
[root@db2 ~]# 
[root@db2 ~]# dnf -y install mariadb-server-galera	安装数据库集群
[root@db3 ~]# systemctl enable --now chronyd
[root@db3 ~]# 
[root@db3 ~]# 
[root@db3 ~]# chronyc sources -v

  .-- Source mode  '^' = server, '=' = peer, '#' = local clock.
 / .- Source state '*' = current best, '+' = combined, '-' = not combined,
| /             'x' = may be in error, '~' = too variable, '?' = unusable.
||                                                 .- xxxx [ yyyy ] +/- zzzz
||      Reachability register (octal) -.           |  xxxx = adjusted offset,
||      Log2(Polling interval) --.      |          |  yyyy = measured offset,
||                                \     |          |  zzzz = estimated error.
||                                 |    |           \
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^- time.cloudflare.com           3   7   377    48  +3053us[+3053us] +/-  120ms
^+ ntp1.flashdance.cx            2   6   377    49  +4893us[+4893us] +/-  125ms
^* ntp8.flashdance.cx            2   7   377    49   -203us[-1070us] +/-  116ms
^- ntp.ams1.nl.leaseweb.net      2   6   377    49    +25ms[  +25ms] +/-  163ms
[root@db3 ~]# 
[root@db3 ~]# date
Wed Mar 12 03:07:51 AM EDT 2025
[root@db3 ~]# 
[root@db3 ~]# 
[root@db3 ~]# clock -w
[root@db3 ~]# dnf -y install mariadb-server-galera		安装数据库集群

四、启动防火墙并在防火墙里添加端口号

4.1db1数据库

[root@db1 ~]# systemctl enable --now firewalld
Created symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service → /usr/lib/systemd/system/firewalld.service.
Created symlink /etc/systemd/system/multi-user.target.wants/firewalld.service → /usr/lib/systemd/system/firewalld.service.
[root@db1 ~]# firewall-cmd --add-service=mysql			在防火墙里添加Mysql服务
success
[root@db1 ~]# firewall-cmd --add-port={3306/tcp,4567/tcp,4568/tcp,4444/tcp}		在防火墙里添加3306/tcp mysql端口号,4567/tcp,4568/tcp,4444/tcp端口  后三个是数据库集群的端口号

success
[root@db1 ~]# firewall-cmd --runtime-to-permanent			在防火墙里添加永久运行时	
success

4.2db2数据库

[root@db2 ~]# systemctl enable --now firewalld
Created symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service → /usr/lib/systemd/system/firewalld.service.
Created symlink /etc/systemd/system/multi-user.target.wants/firewalld.service → /usr/lib/systemd/system/firewalld.service.
[root@db2 ~]# firewall-cmd --add-service=mysql   在防火墙里添加Mysql服务
success
[root@db2 ~]# firewall-cmd --add-port={3306/tcp,4567/tcp,4568/tcp,4444/tcp}			在防火墙里添加3306/tcp mysql端口号,4567/tcp,4568/tcp,4444/tcp端口  后三个是数据库集群的端口号

success
[root@db2 ~]# firewall-cmd --runtime-to-permanent		在防火墙里添加永久运行时
success

4.3 db3数据库

[root@db3 ~]# systemctl enable --now firewalld
Created symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service → /usr/lib/systemd/system/firewalld.service.
Created symlink /etc/systemd/system/multi-user.target.wants/firewalld.service → /usr/lib/systemd/system/firewalld.service.
[root@db3 ~]# firewall-cmd --add-service=mysql
success
[root@db3 ~]# firewall-cmd --add-port={3306/tcp,4567/tcp,4568/tcp,4444/tcp}		在防火墙里添加3306/tcp mysql端口号,4567/tcp,4568/tcp,4444/tcp端口  后三个是数据库集群的端口号

success
[root@db3 ~]# firewall-cmd --runtime-to-permanent		在防火墙里添加永久运行时	

五、编辑galera数据库集群的配置文件

[root@db1 ~]# vim /etc/my.cnf.d/galera.cnf
34 wsrep_on=1		#开启集群		
43 wsrep_cluster_name="Galera_cluster"			#定义集群的名称:Galera_cluster
46 wsrep_cluster_address="gcomm://"				#集群的地址:gcomm://
54 wsrep_node_address="192.168.7.10"			#集群的本机地址192.168.7.10

六、创建数据库集群

[root@db1 ~]# galera_new_cluster 		创建数据库集群
[root@db1 ~]#         
[root@db1 ~]# systemctl enable mariadb		设置数据库开机启动
Created symlink /etc/systemd/system/mysql.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/mysqld.service → 

七、编辑db1数据库集群的配置文件

[root@db1 ~]# vim /etc/my.cnf.d/galera.cnf		编辑galera数据库集群的配置文件

46 wsrep_cluster_address="gcomm://192.168.7.10,192.168.7.11,192.168.7.12"		#添加集群所有节点的IP

八、db1数据库的安全设置

[root@db1 ~]# mysql_secure_installation			db1数据库的安全设置

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
haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

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

You already have your root account protected, so you can safely answer 'n'.

Switch to unix_socket authentication [Y/n] n		交换机切换到unix接口认证吗 n
 ... skipping.

You already have your root account protected, so you can safely answer 'n'.

Change the root password? [Y/n] y			更改root密码吗?y
New password: 
Re-enter new password: 
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!Disallow root login remotely? [Y/n] y			禁止root 远程登陆吗?y
 ... Success!

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!

九、编辑db2数据库集群的配置文件及启动数据库

[root@db2 ~]# vim /etc/my.cnf.d/galera.cnf  编辑数据库集群配置文件

34 wsrep_on=1								#开启集群
43 wsrep_cluster_name="Galera_cluster"			#定义数据库集群的名称
46 wsrep_cluster_address="gcomm://192.168.7.10,192.168.7.11,192.168.7.12"
54 wsrep_node_address="192.168.7.11"
[root@db2 ~]# systemctl enable --now 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.se

十、db2数据库的安全设置

[root@db2 ~]# mysql_secure_installation			db2数据库的安全设置

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
haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

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

You already have your root account protected, so you can safely answer 'n'.

Switch to unix_socket authentication [Y/n] n		交换机切换到unix接口认证吗 n
 ... skipping.

You already have your root account protected, so you can safely answer 'n'.

Change the root password? [Y/n] y
New password: 
Re-enter new password: 
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
 ... 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] y
 ... Success!

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
 - 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
 ... 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!

十一、db2数据库集群的配置文件远程复制

[root@db2 ~]# scp /etc/my.cnf.d/galera.cnf 192.168.7.12:/etc/my.cnf.d/galera.cnf		将数据库集群的配置文件远程复制到 192.168.7.12集群配置文件目录下

Are you sure you want to continue connecting (yes/no/[fingerprint])? yes

Warning: Permanently added ‘192.168.7.12’ (ED25519) to the list of known hosts.

root@192.168.7.12’s password: 密码:123.com

十二、编辑db3数据库集群配置文件

[root@db3 ~]# vim /etc/my.cnf.d/galera.cnf		编辑db3数据库集群配置文件
54 wsrep_node_address="192.168.7.12"		修改集群节点IP	

十三、启动db3数据库并设置开机启动

[root@db3 ~]# systemctl enable --now mariadb		启动db3数据库并设置开机启动

十四、db3数据库的安全设置

[root@db3 ~]# 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
haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

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

You already have your root account protected, so you can safely answer 'n'.

Switch to unix_socket authentication [Y/n] n		交换机切换到unix接口认证吗 n
 ... skipping.

You already have your root account protected, so you can safely answer 'n'.

Change the root password? [Y/n] y
New password: 
Re-enter new password: 
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
 ... 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] y
 ... Success!

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
 - 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
 ... 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!

十五、登录db2数据库查看数据库集群的状态

MariaDB [(none)]> show status like 'wsrep_%'; #登录数据库 查看数据库集群的状态
wsrep_cert_index_size         | 3 
| wsrep_incoming_addresses      | 192.168.7.11:0,192.168.7.10:0,192.168.7.12:0                                                                                                   |登陆成功 看到三个集群节点IP

| wsrep_cluster_weight          | 3 		看到集群的数量 3

十六、在db3上创建class数据库,创建学生表

16.1创建class数据库,创建student表

[root@db3 ~]# mysql				登录数据库
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.5.27-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)]> create database class;
Query OK, 1 row affected (0.005 sec)

MariaDB [(none)]> use class;
Database changed
MariaDB [class]>  create table student(   
    ->     学生编号 int(3) not null,                   
    ->     姓名 nvarchar(50),                          
    ->     身份证号 varchar(18) primary key,           
    ->     所在班级 int(11),                           
    ->     成绩 int(11),                               
    ->     check(成绩 >=0 and 成绩 <=100),             
    ->     备注 nvarchar(2000) null                    
    ->     );

创建student数据表

16.2向student表中插入数据

MariaDB [class]> INSERT INTO student (学生编号,姓名,身份证号,所在班级,成绩,备注)
    -> VALUES (112,'西毒','223456789123456789',7,95,'爱好蛤蟆功');
Query OK, 1 row affected (0.004 sec)

MariaDB [class]>  
MariaDB [class]>  
MariaDB [class]> INSERT INTO student (学生编号,姓名,身份证号,所在班级,成绩,备注)
    -> VALUES (113,'南帝','323456789123456789',2,99,'一阳指');
Query OK, 1 row affected (0.002 sec)

MariaDB [class]>  
MariaDB [class]>  
MariaDB [class]>  
MariaDB [class]> INSERT INTO student (学生编号,姓名,身份证号,所在班级,成绩,备注)
    -> VALUES (115,'老顽童','523456789123456789',1,99,'爱好各种武功');
Query OK, 1 row affected (0.003 sec)

MariaDB [class]>  
MariaDB [class]>  
MariaDB [class]>  
MariaDB [class]> INSERT INTO student (学生编号,姓名,身份证号,所在班级,成绩,备注)
    -> VALUES (116,'黄蓉','623456789123456789',3,88,'打狗棒');
Query OK, 1 row affected (0.002 sec)

MariaDB [class]>  
MariaDB [class]>  
MariaDB [class]>  
MariaDB [class]> INSERT INTO student (学生编号,姓名,身份证号,所在班级,成绩,备注)
    -> VALUES (117,'郭靖','723456789123456789',1,89,'降龙十八掌');
Query OK, 1 row affected (0.002 sec)

十七、在db1数据库上查看是否数据已同步

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

MariaDB [(none)]> use class;
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 [class]> show tables;
+-----------------+
| Tables_in_class |
+-----------------+
| student         |
+-----------------+
1 row in set (0.000 sec)

在db2数据库上测试,数据是否同步成功

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

MariaDB [(none)]> use class;
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 [class]> show tables;
+-----------------+
| Tables_in_class |
+-----------------+
| student         |
+-----------------+
1 row in set (0.000 sec)

MariaDB [class]> select * from student;
+--------------+-----------+--------------------+--------------+--------+--------------------+
| 学生编号     | 姓名      | 身份证号           | 所在班级     | 成绩   | 备注               |
+--------------+-----------+--------------------+--------------+--------+--------------------+
|          111 | 东邪      | 123456789123456789 |            2 |     90 | NULL               |
|          120 | 丘处机    | 213456789123456789 |            5 |     87 | NULL               |
|          112 | 西毒      | 223456789123456789 |            7 |     95 | 爱好蛤蟆功         |
|          113 | 南帝      | 323456789123456789 |            2 |     99 | 一阳指             |
|          114 | 北丐      | 423456789123456789 |            3 |     96 | NULL               |
|          115 | 老顽童    | 523456789123456789 |            1 |     99 | 爱好各种武功       |
|          116 | 黄蓉      | 623456789123456789 |            3 |     88 | 打狗棒             |
|          117 | 郭靖      | 723456789123456789 |            1 |     89 | 降龙十八掌         |
|          118 | 杨康      | 823456789123456789 |            1 |     80 | 九阴白骨爪         |
|          119 | 裘千仞    | 923456789123456789 |            7 |     77 | 水上漂             |
+--------------+-----------+--------------------+--------------+--------+--------------------+
10 rows in set (0.001 sec)

数据同步成功!

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇