| hostnamectl set-hostname db1 |
| hostnamectl set-hostname db2 |
关闭方法 - 编辑配置文件 /etc/selinux/config
,修改 22 行内容如下:
重启服务器生效。
| 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 |
查看同步状态
查看时间是否一致
将系统时间写入硬件时钟
| dnf -y install mariadb-server-galera |
启动防火墙
| systemctl enable --now firewalld |
防火墙中添加 MySQL 服务
添加集群所需的端口
| firewall-cmd --add-port={3306/tcp,4567/tcp,4568/tcp,4444/tcp} |
将当前运行的防火墙规则保存为永久配置
| vi /etc/my.cnf.d/galera.cnf |
| 34 wsrep_on=1 |
| 43 wsrep_cluster_name="Galera_cluster" |
| 46 wsrep_cluster_address="gcomm://" |
| 54 wsrep_node_address="192.168.241.134" |
创建数据库集群
查看文件是否生成
| [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 |
设置开机自启
| vi /etc/my.cnf.d/galera.cnf |
| 46 wsrep_cluster_address="gcomm://192.168.241.134,192.168.241.135" |
| [root@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 |
| ... 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! |
| 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" |
| systemctl enable --now mariadb |
| [root@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 |
| ... 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 | |
| …… |
| 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]> |
| 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]> |
数据同步成功,集群部署完成。