MariaDB数据库集群部署笔记

一、修改服务器主机名

hostnamectl set-hostname db1
hostnamectl set-hostname db2

二、检查 selinux 是否关闭

sestatus

关闭方法 - 编辑配置文件 /etc/selinux/config,修改 22 行内容如下:

SELINUX=disabled

重启服务器生效。

三、关闭防火墙与开机自启

systemctl stop firewalld
systemctl disable firewalld

四、配置服务器间时间同步

/etc/chrony.conf 配置文件:注释第 3 行,新增第 4 行

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

启动时间同步服务并设置开机自启

systemctl enable --now chronyd

查看同步状态

chronyc sources -v

查看时间是否一致

date

将系统时间写入硬件时钟

clock -w

五、安装 MariaDB

dnf -y install mariadb-server-galera

六、配置防火墙策略

启动防火墙

systemctl enable --now firewalld

防火墙中添加 MySQL 服务

firewall-cmd --add-service=mysql

添加集群所需的端口

firewall-cmd --add-port={3306/tcp,4567/tcp,4568/tcp,4444/tcp}

将当前运行的防火墙规则保存为永久配置

firewall-cmd --runtime-to-permanent

七、编辑集群配置文件 [db1]

vi /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.241.134" #集群的本机地址 192.168.241.134

八、创建数据库集群 [db1]

创建数据库集群

galera_new_cluster

查看文件是否生成

[root@db1 ~]# ll /var/lib/mysql/
总用量 254028
-rw-rw---- 1 mysql mysql 24576 3月 13 16:03 aria_log.00000001
-rw-rw---- 1 mysql mysql 52 3月 13 16:03 aria_log_control
-rw-rw---- 1 mysql mysql 134219032 3月 13 16:03 galera.cache
-rw-rw---- 1 mysql mysql 113 3月 13 16:03 grastate.dat
-rw-rw---- 1 mysql mysql 170 3月 13 16:03 gvwstate.dat
-rw-rw---- 1 mysql mysql 972 3月 13 16:03 ib_buffer_pool
-rw-rw---- 1 mysql mysql 12582912 3月 13 16:03 ibdata1
-rw-rw---- 1 mysql mysql 100663296 3月 13 16:03 ib_logfile0
-rw-rw---- 1 mysql mysql 12582912 3月 13 16:03 ibtmp1
-rw-rw---- 1 mysql mysql 0 3月 13 16:03 multi-master.info
drwx------ 2 mysql mysql 4096 3月 13 16:03 mysql
srwxrwxrwx 1 mysql mysql 0 3月 13 16:03 mysql.sock
-rw-rw---- 1 mysql mysql 16 3月 13 16:03 mysql_upgrade_info
drwx------ 2 mysql mysql 20 3月 13 16:03 performance_schema
-rw-rw---- 1 mysql mysql 24576 3月 13 16:03 tc.log

设置开机自启

systemctl enable mariadb

九、重新编辑集群配置文件 [db1]

vi /etc/my.cnf.d/galera.cnf
46 wsrep_cluster_address="gcomm://192.168.241.134,192.168.241.135" #添加集群所有节点的IP

十、数据库的安全设置 [db1]

[root@db1 ~]# 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
... 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]

vi /etc/my.cnf.d/galera.cnf
34 wsrep_on=1 #开启集群
43 wsrep_cluster_name="Galera_cluster" #定义数据库集群的名称
46 wsrep_cluster_address="gcomm://192.168.241.134,192.168.241.135"
54 wsrep_node_address="192.168.241.135"

十二、启动数据库并设置开机自启 [db2]

systemctl enable --now mariadb

十三、数据库的安全设置 [db2] 同 db1 一样

[root@db2 ~]# 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
... 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!

十四、登录数据库查看集群状态

MariaDB [(none)]> show status like 'wsrep_%';
……
| wsrep_incoming_addresses | 192.168.241.135:0,192.168.241.134:0 |
| wsrep_cluster_weight | 2 |
……

十五、登录数据库创建测试数据 [db2]

MariaDB [(none)]> create database class;
Query OK, 1 row affected (0.027 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);
Query OK, 0 rows affected (0.031 sec)
MariaDB [class]> insert into student (学生编号,姓名,身份证号,所在班级,成绩,备注) VALUES (112,'西毒','223456789123456789',7,95,'爱好蛤蟆功');
Query OK, 1 row affected (0.007 sec)
MariaDB [class]> insert into student (学生编号,姓名,身份证号,所在班级,成绩,备注) VALUES (113,'南帝','323456789123456789',2,99,'一阳指');
Query OK, 1 row affected (0.015 sec)
MariaDB [class]> insert into student (学生编号,姓名,身份证号,所在班级,成绩,备注) VALUES (115,'老顽童','523456789123456789',1,99,'爱好各种武功');
Query OK, 1 row affected (0.009 sec)
MariaDB [class]> insert into student (学生编号,姓名,身份证号,所在班级,成绩,备注) VALUES (116,'黄蓉','623456789123456789',3,88,'打狗棒');
Query OK, 1 row affected (0.012 sec)
MariaDB [class]> insert into student (学生编号,姓名,身份证号,所在班级,成绩,备注) VALUES (117,'郭靖','723456789123456789',1,89,'降龙十八掌');
Query OK, 1 row affected (0.007 sec)
MariaDB [class]> select * from student;
+--------------+-----------+--------------------+--------------+--------+--------------------+
| 学生编号 | 姓名 | 身份证号 | 所在班级 | 成绩 | 备注 |
+--------------+-----------+--------------------+--------------+--------+--------------------+
| 112 | 西毒 | 223456789123456789 | 7 | 95 | 爱好蛤蟆功 |
| 113 | 南帝 | 323456789123456789 | 2 | 99 | 一阳指 |
| 115 | 老顽童 | 523456789123456789 | 1 | 99 | 爱好各种武功 |
| 116 | 黄蓉 | 623456789123456789 | 3 | 88 | 打狗棒 |
| 117 | 郭靖 | 723456789123456789 | 1 | 89 | 降龙十八掌 |
+--------------+-----------+--------------------+--------------+--------+--------------------+
5 rows in set (0.001 sec)
MariaDB [class]>

十六、登录数据库查看数据是否同步 [db1]

MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| class |
| information_schema |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.002 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;
+--------------+-----------+--------------------+--------------+--------+--------------------+
| 学生编号 | 姓名 | 身份证号 | 所在班级 | 成绩 | 备注 |
+--------------+-----------+--------------------+--------------+--------+--------------------+
| 112 | 西毒 | 223456789123456789 | 7 | 95 | 爱好蛤蟆功 |
| 113 | 南帝 | 323456789123456789 | 2 | 99 | 一阳指 |
| 115 | 老顽童 | 523456789123456789 | 1 | 99 | 爱好各种武功 |
| 116 | 黄蓉 | 623456789123456789 | 3 | 88 | 打狗棒 |
| 117 | 郭靖 | 723456789123456789 | 1 | 89 | 降龙十八掌 |
+--------------+-----------+--------------------+--------------+--------+--------------------+
5 rows in set (0.001 sec)
MariaDB [class]>

数据同步成功,集群部署完成。

暂无评论

发送评论 编辑评论


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