如何安装和配置MariaDB Galera CentOS-7集群

192.168.3.130 clustera galera-db01
192.168.3.131 clusterb galera-db02
192.168.3.129 clusterc galera-db03

以下1-3步三台都需要执行
1:卸载mysql(因为mysql与mariaDB与mysql不兼容)
yum erase mysql-server mysql mysql-devel mysql-libs -y
rm -rf /var/lib/mysql

2:创建MariaDB Galera存储库,mariadb 官方Yum 源配置:
vim /etc/yum.repos.d/mariadb.repo
vim 没有的话安装 yum install -y vim*

[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.0/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

3:setenforce 0 关闭selinux
安装防火墙并启动设置允许端口
yum install firewalld firewall-config
systemctl start firewalld
firewall-cmd --add-rich-rule 'rule family=ipv4 source address=192.168.3.129 port port=3306 protocol=tcp accept' --permanent
firewall-cmd --add-rich-rule 'rule family=ipv4 source address=192.168.3.130 port port=3306 protocol=tcp accept' --permanent
firewall-cmd --add-rich-rule 'rule family=ipv4 source address=192.168.3.131 port port=3306 protocol=tcp accept' --permanent
firewall-cmd --add-rich-rule 'rule family=ipv4 source address=192.168.3.129 port port=4567 protocol=tcp accept' --permanent
firewall-cmd --add-rich-rule 'rule family=ipv4 source address=192.168.3.130 port port=4567 protocol=tcp accept' --permanent
firewall-cmd --add-rich-rule 'rule family=ipv4 source address=192.168.3.131 port port=4567 protocol=tcp accept' --permanent

以下4-6步在集群mariadb节点服务器上面操作
4:安装MariaDB Galera服务器和客户端,rsync和xinetd
yum install MariaDB-Galera-server MariaDB-client rsync galera -y

如果系统中存在mysql会出如下错误

如果要自定义MariaDB的文件目录就如下操作(以自定义/data/目录为例)
rm -rf /var/lib/mysql
mkdir -p /data/{mariadb,tmp,logs} && chown -R mysql:mysql /data/{mariadb,tmp,logs} 建目录与权限
ln -s /data/mariadb /var/lib/mysql
编辑/etc/my.cnf 文件及设置优化参数,可以参照centos6配置mariadb集群
mysql_install_db --defaults-file=/etc/my.cnf --user=mysql --datadir=/data/mariadb 初始化

5:启动mariaDB并添加开机启动和加固
service mysql start 启动mariaDB
chkconfig --add mysql && chkconfig mysql on
/usr/bin/mysql_secure_installation 加固Mariadb 安全:

[root@localhost ~]# /usr/bin/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):
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] n 这里设置root密码,正式环境设置选择y
... skipping.

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] n
... 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

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

首先是设置密码,会提示先输入密码
Enter current password for root (enter for none):<–初次运行直接回车
设置密码
Set root password? [Y/n] <– 是否设置root用户密码,输入y并回车或直接回车
New password: <– 设置root用户的密码
Re-enter new password: <– 再输入一次你设置的密码
其他配置
Remove anonymous users? [Y/n] <– 是否删除匿名用户,回车
Disallow root login remotely? [Y/n] <–是否禁止root远程登录,回车,
Remove test database and access to it? [Y/n] <– 是否删除test数据库,回车
Reload privilege tables now? [Y/n] <– 是否重新加载权限表,回车

6:创建用户并授权
mysql -uroot 登录数据库,正式设置密码使用 mysql -uroot -p密码
创建用户并授权一般选用第一条命令即可,第二条命令后面三个参数是授权创建的用户拥有创建他本身的权限,实验时候执行的第三条命令

GRANT ALL PRIVILEGES ON . TO 'cluster-user'@'%' IDENTIFIED BY 'clusterpass' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON . TO 'cluster-user'@'localhost' IDENTIFIED BY 'clusterpass' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON . TO 'cluster-user'@'192.168.3.130' IDENTIFIED BY 'clusterpass' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON . TO 'cluster-user'@'192.168.3.131' IDENTIFIED BY 'clusterpass' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON . TO 'cluster-user'@'192.168.3.129' IDENTIFIED BY 'clusterpass' WITH GRANT OPTION;

flush privileges;
exit
service mysql stop

7:节点配置 在mariadb1上面配置
vim /etc/my.cnf.d/server.cnf [mariadb-10.0] 下添加

query_cache_size=0
binlog_format=ROW
default_storage_engine=innodb
innodb_autoinc_lock_mode=2
wsrep_provider=/usr/lib64/galera/libgalera_smm.so
wsrep_cluster_address="gcomm://192.168.3.130,192.168.3.131,192.168.3.129"
wsrep_cluster_name="cluster1"
wsrep_node_address="192.168.3.130"
wsrep_node_name="galera-db01"
wsrep_sst_method=rsync
wsrep_sst_auth=cluster-user:clusterpass

query_cache_size 查询缓存,0关闭
binlog_format=ROW 复制模式
wsrep_cluster_address 所有节点的IP
wsrep_cluster_name 集群名字
wsrep_node_address 当前服务器IP,也就是每个节点本身的IP
wsrep_node_name 当前服务器名字,也就是每个节点本身的名字
wsrep_provider=/usr/lib64/galera/libgalera_smm.so
32位是/usr/lib/galera/libgalera_smm.so
64位是/usr/lib64/galera/libgalera_smm.so
不填写正确启动以后始终发现不要了集群

8:在服务器mariadb1上,我们使用以下命令引导集群:
/etc/init.d/mysql bootstrap

mysql -u root -e "show status like 'wsrep%'" 查看状态

8:节点配置 在mariadb2上面配置
vim /etc/my.cnf.d/server.cnf [mariadb-10.0] 下添加

query_cache_size=0
binlog_format=ROW
default_storage_engine=innodb
innodb_autoinc_lock_mode=2
wsrep_provider=/usr/lib64/galera/libgalera_smm.so
wsrep_cluster_address="gcomm://192.168.3.130,192.168.3.131,192.168.3.129"
wsrep_cluster_name="cluster1"
wsrep_node_address="192.168.3.131"
wsrep_node_name="galera-db02"
wsrep_sst_method=rsync
wsrep_sst_auth=cluster-user:clusterpass

/etc/init.d/mysql start

mysql -u root -e "show status like 'wsrep%'" 查看状态

9:节点配置 在mariadb3上面配置
vim /etc/my.cnf.d/server.cnf [mariadb-10.0] 下添加

query_cache_size=0
binlog_format=ROW
default_storage_engine=innodb
innodb_autoinc_lock_mode=2
wsrep_provider=/usr/lib64/galera/libgalera_smm.so
wsrep_cluster_address="gcomm://192.168.3.130,192.168.3.131,192.168.3.129"
wsrep_cluster_name="cluster1"
wsrep_node_address="192.168.3.131"
wsrep_node_name="galera-db03"
wsrep_sst_method=rsync
wsrep_sst_auth=cluster-user:clusterpass

/etc/init.d/mysql start

mysql -u root -e "show status like 'wsrep%'" 查看状态

验证集群
1.任意一台,CREATE DATABASES clustertest 创建库
在另外两台台可以查看到也创建了clustertest库
2.在任意一台删除clustertest库,另外两台也可以查看到已经删除
3.dump掉MariaDB1的网卡 ifdown 网卡名,再尝试创建test表,在表中写数据,create table test (id int(4), name char(40)); 再启动MariaDB1的网卡,ifup 网卡名,再到MAriaDB1查看写的数据 select * from test;

10.使用haporxy代理的话,在代理服务器上操作(以下命令在集群只有两台作为仲裁使用)
yum erase mysql-server mysql mysql-devel mysql-libs -y
rm -rf /var/lib/mysql
yum install -y galera
which garbd
garbd -a gcomm://192.168.1.130:4567 -g cluster1 -l /tmp/1.out -d
mariaDB1上面执行
mysql -u root -e "show status like 'wsrep%'" 再查看状态
wsrep_cluster_sixe 数值已更改

11.安装配置haproxy
yum install -y haproxy

/etc/haproxy/haproxy.cfg
vim !$
global
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
stats socket /var/lib/haproxy/stats
defaults
mode tcp
#mode http
option httplog
log global
option dontlognull
option redispatch
option tcpka
retries 3
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout check 10s
timeout http-keep-alive 10s
maxconn 10000

frontend main *:3306
bind 192.168.3.135:3306
default_backend app

backend app
         balance roundrobin
         server app1 192.168.3.130:3306  maxconn 151 check
         server  app2 192.168.3.131:3306  maxconn 151 check
         server  app3  192.168.3.129:3306  maxconn 151 check

haproxy加入开机启动并启动
chkconfig haproxy on
service haproxy start

验证代理
mysql -u cluster-use -p clusterpass -h 192.168.3.135 -p 3306 -e "select @@hostname;"

如果有需要,可以配置MariaDB的字符集
vi /etc/my.cnf
在[mysqld]标签下添加
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
vi /etc/my.cnf.d/client.cnf
在[client]中添加
default-character-set=utf8

vi /etc/my.cnf.d/mysql-clients.cnf
在[mysql]中添加
default-character-set=utf8
全部配置完成,重启mariadb
systemctl restart mariadb
之后进入MariaDB查看字符集
mysql> show variables like "%character%";show variables like "%collation%";

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章