Redhat 6.7 下基于Galera的MariaDB集群搭建

版权声明:未经博主允许不允许转载 https://blog.csdn.net/qianglei6077/article/details/90137198

1 MariaDB介绍

MariaDB是开源社区维护的一个MySQL分支,由MySQL的创始人Michael Widenius主导开发,采用GPL授权许可证。

MariaDB的目的是完全兼容MySQL,包括API和命令行,使之能轻松成为MySQL的代替品。

详细介绍请参考链接:
http://mariadb.org/(官网)
http://baike.baidu.com/link?url=dFJ-My-I52YFc1mx26K804LPwZrcEWCwB4IqfA4-soYx6295BZLIe7bEFgOtt3CWZ8AYpkp1P342L4S-R4x4CK

2 Galera Cluster介绍

Galera Cluster是基于MySQL/innodb二次开发而成的一个支持“多主同步”的数据库主从集群,具有高可用,易于扩展等特点。

详细介绍请参考链接:
http://galeracluster.com/(官网)
http://www.gpfeng.com/?p=603&utm_source=tuicool&utm_medium=referral

3 搭建过程

注意:下面的所有操作默认情况下在所有节点都要执行。

3.1 添加yum源

# cat /etc/yum.repos.d/RedHat-MariaDB.repo
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/5.5/rhel6-amd64
gpgkey = https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck = 1

3.2 安装Maridb galera包

# yum install MariaDB-Galera-server MariaDB-client galera

如果提示缺少包:libboost_program_options.so.5(),安装boost-program-options-1.41.0-28.el6.x86_64.rpm即可。

下载链接:https://pan.baidu.com/s/1kVj9YKZ 密码:iyub

3.3 关闭防火墙和Selinux

[root@MariDB-Master ~]# service iptables stop

[root@MariDB-Master ~]# chkconfig iptables off

[root@MariDB-Master ~]# cat /etc/selinux/config

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these two values:
#     targeted - Targeted processes are protected,
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

3.4 启动Maridb

# service mysql start

3.5 初始化DB

初始化数据库服务,只在其中一个节点执行即可。(先设置root账户密码,再一直“y”下去即可)

# 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]

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

 ... skipping.


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

 ... skipping.


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!

3.6 创建节点同步账户

MariaDB [(none)]> grant usage on *.* to galeradba@'%' identified by 'cndba';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]>  flush privileges;
Query OK, 0 rows affected (0.00 sec)

3.7 修改集群节点配置

# cp /usr/share/mysql/wsrep.cnf /etc/my.cnf.d/
# vi /etc/my.cnf.d/wsrep.cnf

修改如下几行:

wsrep_provider=/usr/lib64/galera/libgalera_smm.so
wsrep_cluster_address="gcomm://"    #集群节点N的地址(注意把前面的"#"删掉!)
wsrep_sst_auth=galeradba:cndba       #节点N的数据库账户和密码
  • 参数说明
    • “gcomm://” 是特殊的地址,仅仅是galera cluster初始化启动时候使用。
    • 如果集群启动以后,我们关闭了第一个节点,那么再次启动的时候必须先修改"gcomm://"为其他节点的集群地址,例如wsrep_cluster_address=“gcomm://192.168.0.152”。

3.8 在/etc/my.cnf中加入如下一行

!includedir /etc/my.cnf.d/

3.9 重启Maridb服务

# service mysql restart

3.10 查看集群状态–现在其他节点还没加入进来

MariaDB [(none)]>  show status like 'wsrep%';
+------------------------------+--------------------------------------+
| Variable_name                | Value                                |
+------------------------------+--------------------------------------+
| wsrep_local_state_uuid       | fe9eb4a7-dd83-11e7-b9ae-4e0c603d8ee5 |
| wsrep_protocol_version       | 7                                    |
| wsrep_last_committed         | 0                                    |
| wsrep_replicated             | 0                                    |
| wsrep_replicated_bytes       | 0                                    |
| wsrep_repl_keys              | 0                                    |
| wsrep_repl_keys_bytes        | 0                                    |
| wsrep_repl_data_bytes        | 0                                    |
| wsrep_repl_other_bytes       | 0                                    |
| wsrep_received               | 27                                   |
| wsrep_received_bytes         | 2214                                 |
| wsrep_local_commits          | 0                                    |
| wsrep_local_cert_failures    | 0                                    |
| wsrep_local_replays          | 0                                    |
| wsrep_local_send_queue       | 0                                    |
| wsrep_local_send_queue_max   | 1                                    |
| wsrep_local_send_queue_min   | 0                                    |
| wsrep_local_send_queue_avg   | 0.000000                             |
| wsrep_local_recv_queue       | 0                                    |
| wsrep_local_recv_queue_max   | 2                                    |
| wsrep_local_recv_queue_min   | 0                                    |
| wsrep_local_recv_queue_avg   | 0.111111                             |
| wsrep_local_cached_downto    | 18446744073709551615                 |
| wsrep_flow_control_paused_ns | 0                                    |
| wsrep_flow_control_paused    | 0.000000                             |
| wsrep_flow_control_sent      | 0                                    |
| wsrep_flow_control_recv      | 0                                    |
| wsrep_cert_deps_distance     | 0.000000                             |
| wsrep_apply_oooe             | 0.000000                             |
| wsrep_apply_oool             | 0.000000                             |
| wsrep_apply_window           | 0.000000                             |
| wsrep_commit_oooe            | 0.000000                             |
| wsrep_commit_oool            | 0.000000                             |
| wsrep_commit_window          | 0.000000                             |
| wsrep_local_state            | 4                                    |
| wsrep_local_state_comment    | Synced                               |
| wsrep_cert_index_size        | 0                                    |
| wsrep_causal_reads           | 0                                    |
| wsrep_cert_interval          | 0.000000                             |
| wsrep_incoming_addresses | 192.168.1.77:3306 其他节点还没加入进来 |  
| wsrep_desync_count           | 0                                    |
| wsrep_evs_delayed            |                                      |
| wsrep_evs_evict_list         |                                      |
| wsrep_evs_repl_latency       | 0/0/0/0/0                            |
| wsrep_evs_state              | OPERATIONAL                          |
| wsrep_gcomm_uuid             | fe9e62e4-dd83-11e7-874d-5aba526a0212 |
| wsrep_cluster_conf_id        | 11                                   |
| wsrep_cluster_size           | 1                                    |
| wsrep_cluster_state_uuid     | fe9eb4a7-dd83-11e7-b9ae-4e0c603d8ee5 |
| wsrep_cluster_status         | Primary                              |
| wsrep_connected | ON                                  |
| wsrep_local_bf_aborts        | 0                                    |
| wsrep_local_index            | 0                                    |
| wsrep_provider_name          | Galera                               |
| wsrep_provider_vendor        | Codership Oy     |
| wsrep_provider_version       | 25.3.20(r3703)                       |
| wsrep_ready | ON                       |
| wsrep_thread_count           | 2                                    |
+------------------------------+--------------------------------------+

58 rows in set (0.00 sec)

3.11 配置其他节点

除了修改一下3.7小节中提到的wsrep_cluster_address="gcomm://"外,其他都保持一致。具体修改方法,参考3.7小节。

如:192.168.1.78中配置的就是第一节点的ip地址wsrep_cluster_address=“gcomm://192.168.1.77”

3.12 依次重启其他两个节点

MariaDB [(none)]>  show status like 'wsrep%';

+------------------------------+-------------------------------------------------------+
| Variable_name                | Value                                                 |
+------------------------------+-------------------------------------------------------+
| wsrep_local_state_uuid       | fe9eb4a7-dd83-11e7-b9ae-4e0c603d8ee5                  |
| wsrep_protocol_version       | 7                                                     |
| wsrep_last_committed         | 0                                                     |
| wsrep_replicated             | 0                                                     |
| wsrep_replicated_bytes       | 0                                                     |
| wsrep_repl_keys              | 0                                                     |
| wsrep_repl_keys_bytes        | 0                                                     |
| wsrep_repl_data_bytes        | 0                                                     |
| wsrep_repl_other_bytes       | 0                                                     |
| wsrep_received               | 17                                                    |
| wsrep_received_bytes         | 1550                                                  |
| wsrep_local_commits          | 0                                                     |
| wsrep_local_cert_failures    | 0                                                     |
| wsrep_local_replays          | 0                                                     |
| wsrep_local_send_queue       | 0                                                     |
| wsrep_local_send_queue_max   | 1                                                     |
| wsrep_local_send_queue_min   | 0                                                     |
| wsrep_local_send_queue_avg   | 0.000000                                              |
| wsrep_local_recv_queue       | 0                                                     |
| wsrep_local_recv_queue_max   | 2                                                     |
| wsrep_local_recv_queue_min   | 0                                                     |
| wsrep_local_recv_queue_avg   | 0.117647                                              |
| wsrep_local_cached_downto    | 18446744073709551615                                  |
| wsrep_flow_control_paused_ns | 0                                                     |
| wsrep_flow_control_paused    | 0.000000                                              |
| wsrep_flow_control_sent      | 0                                                     |
| wsrep_flow_control_recv      | 0                                                     |
| wsrep_cert_deps_distance     | 0.000000                                              |
| wsrep_apply_oooe             | 0.000000                                              |
| wsrep_apply_oool             | 0.000000                                              |
| wsrep_apply_window           | 0.000000                                              |
| wsrep_commit_oooe            | 0.000000                                              |
| wsrep_commit_oool            | 0.000000                                              |
| wsrep_commit_window          | 0.000000                                              |
| wsrep_local_state            | 4                                                     |
| wsrep_local_state_comment    | Synced                                                |
| wsrep_cert_index_size        | 0                                                     |
| wsrep_causal_reads           | 0                                                     |
| wsrep_cert_interval          | 0.000000                                              |
| wsrep_incoming_addresses | 192.168.1.78:3306,192.168.1.79:3306,192.168.1.77:3306 |
| wsrep_desync_count           | 0                                                     |
| wsrep_evs_delayed            |                                                       |
| wsrep_evs_evict_list         |                                                       |
| wsrep_evs_repl_latency       | 0.000647584/0.000718767/0.000789951/7.11835e-05/2     |
| wsrep_evs_state              | OPERATIONAL                                           |
| wsrep_gcomm_uuid             | 6ff1fcb5-dd8c-11e7-8aac-c73a72475351                  |
| wsrep_cluster_conf_id        | 7                                                     |
| wsrep_cluster_size           | 3                                                     |
| wsrep_cluster_state_uuid     | fe9eb4a7-dd83-11e7-b9ae-4e0c603d8ee5                  |
| wsrep_cluster_status         | Primary                                               |
| wsrep_connected              | ON                                                    |
| wsrep_local_bf_aborts        | 0                                                     |
| wsrep_local_index            | 2                                                     |
| wsrep_provider_name          | Galera                                                |
| wsrep_provider_vendor        | Codership Oy                      |
| wsrep_provider_version       | 25.3.20(r3703)                                        |
| wsrep_ready                  | ON                                                    |
| wsrep_thread_count           | 2                                                     |
+------------------------------+-------------------------------------------------------+

58 rows in set (0.00 sec)

集群状态正常

3.13 测试数据是否正常同步

#节点1上新建一个数据库

MariaDB [(none)]> show databases;

+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)

MariaDB [(none)]> create database cndba; Query OK, 1 row affected (0.04 sec)

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

#其他节点查看是否同步

MariaDB [(none)]> show databases;

+--------------------+
| Database           |
+--------------------+
| information_schema |
| cndba              |
| mysql              |
| performance_schema |
+--------------------+

4 rows in set (0.00 sec)

可以看到同步正常。

至此Galera集群就搭建完成了。

猜你喜欢

转载自blog.csdn.net/qianglei6077/article/details/90137198
6.7