在centos7上利用galera配置mariadb高可用 2019-05-12

实验环境:两台centos7、使用centos自带的yum源
node1 IP:10.10.49.120
node2 IP:10.10.49.123

首先关闭防火墙等服务

[root@node1~]# systemctl stop firewalld
[root@node1~]# iptables -F
[root@node1~]# iptables -X
[root@node1~]# iptables -Z
[root@node1~]# /usr/sbin/iptables-save
[root@node1~]# setenforce 0
setenforce: SELinux is disabled

配置主机名映射:

[root@node1 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.10.49.120 node1
10.10.49.123 node2

安装mariadb和galera

[root@node1 ~]# yum -y install mariadb-server
[root@node1 ~]# yum -y install galera

启动数据库

[root@node1 ~]# systemctl start mariadb

设置数据库密码:

[root@node1 ~]# mysql_secure_installation

修改配置文件:
node1:

[root@node1 ~]# cat /etc/my.cnf
[mysqld]
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

max_connect_errors=1000(添加一行)


[root@node1 ~]# cat /etc/my.cnf.d/mariadb-server.cnf
[galera]
# Mandatory settings
wsrep_on=ON
wsrep_provider=/usr/lib64/galera/libgalera_smm.so
wsrep_cluster_address=gcomm://
binlog_format=row
default_storage_engine=InnoDB
innodb_autoinc_lock_mode=2
#
# Allow server to accept connections on all interfaces.
#
bind-address=0.0.0.0

wsrep_cluster_name="cluster"
wsrep_node_address="10.10.49.120"
wsrep_sst_method=rsyn
(添加这三行)

node2:

[root@node2 ~]# cat /etc/my.cnf
[mysqld]
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

max_connect_errors=1000(添加一行)

[root@node2 ~]# cat /etc/my.cnf.d/mariadb-server.cnf
[galera]
# Mandatory settings
wsrep_on=ON
wsrep_provider=/usr/lib64/galera/libgalera_smm.so
wsrep_cluster_address="gcomm://10.10.49.120,10.10.49.123"
binlog_format=row
default_storage_engine=InnoDB
innodb_autoinc_lock_mode=2
#
# Allow server to accept connections on all interfaces.
#
bind-address=0.0.0.0

wsrep_cluster_name="cluster"
wsrep_node_address="10.10.49.123"
wsrep_sst_method=rsync

重启服务:

[root@node1 ~]# systemctl restart mariadb

验证:

MariaDB [(none)]> show status like 'wsrep_%';
| wsrep_local_state_comment    | Synced    | 这行状态为Synced同步的

在node1创建数据库synced:

[root@node1 ~]# mysql -uroot -p000000
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 5
Server version: 10.1.20-MariaDB MariaDB Server

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

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> create database synced;
Query OK, 1 row affected (0.00 sec)

在node2查询数据库列表:

[root@node2 ~]# mysql -uroot -p000000
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 10.1.20-MariaDB MariaDB Server

Copyright (c) 2000, 2016, 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 |
| qwe                |
| synced(同步成功)             |
| test               |
+--------------------+
6 rows in set (0.00 sec)(重启了一下mariadb才看到synced库的>_<)
    原文作者:黄昏水自流
    原文地址: https://www.jianshu.com/p/4a328c4f1135
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞