如何在CentOS 7系统上配置高可用的MariaDB Galera Cluster多主群集

MariaDB Galera群集是多主同步群集。它只能在Linux系统上使用,并且只支持XtraDB/InnoDB存储引擎。
我们在本篇文章将配置一个HA(高可用性)多主数据库群集。

VMware® Workstation 12 Pro

CentOS 7最小化的系统镜像
CentOS-7-x86_64-Minimal-1503-01.iso


hostname ip address node name node address cluster name
contoso21.com 192.168.10.21 db1 192.168.10.21 galera1_cluster
contoso22.com 192.168.10.22 db2 192.168.10.22 galera1_cluster

contoso23.com 192.168.10.23 db3 192.168.10.23 galera1_cluster

第1步: 建立主机IP地址与主机名称的映射关系[root@contoso21 ~]# cat > /etc/hosts127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 contoso21.com::1 localhost localhost.localdomain localhost6 localhost6.localdomain6192.168.10.21 contoso21.com[root@contoso22 ~]# cat > /etc/hosts127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 contoso22.com::1 localhost localhost.localdomain localhost6 localhost6.localdomain6192.168.10.22 contoso22.com[root@contoso23 ~]# cat > /etc/hosts127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 contoso23.com::1 localhost localhost.localdomain localhost6 localhost6.localdomain6192.168.10.23 contoso23.com第2步: 添加MariaDB 软件包仓库在[root@contoso21 ~]# 和 [root@contoso22 ~]# 还有 [root@contoso23 ~]#上分别执行如下终端命令:tee /etc/yum.repos.d/MariaDB.repo <<-'EOF'[mariadb]name = MariaDBbaseurl = http://yum.mariadb.org/10.1/centos7-amd64gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDBgpgcheck=1EOF第3步: – 设置selinux的disabled模式 setenforce 0,并且关闭防火墙服务在[root@contoso21 ~]# 和 [root@contoso22 ~]# 还有 [root@contoso23 ~]#上分别执行如下终端命令: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 three two values:# targeted - Targeted processes are protected,# minimum - Modification of targeted policy. Only selected processes are protected. # mls - Multi Level Security protection.SELINUXTYPE=targeted在[root@contoso21 ~]# 和 [root@contoso22 ~]# 还有 [root@contoso23 ~]#上分别执行如下终端命令:systemctl stop firewalld && systemctl disable firewalldinit 6 ## reboot第4步: – 安装MariaDB Galera群集10.1版软件包在[root@contoso21 ~]# 和 [root@contoso22 ~]# 还有 [root@contoso23 ~]#上分别执行如下终端命令:yum -y install socat deltarpm && yum install -y MariaDB-server MariaDB-client在[root@contoso21 ~]# 和 [root@contoso22 ~]# 还有 [root@contoso23 ~]#上分别执行如下终端命令:mkdir -p /var/log/mariadbtouch /var/log/mariadb/mariadb-slow.logtouch /var/log/mariadb/queries.logchmod 666 /var/log/mariadb/mariadb-slow.logchmod 666 /var/log/mariadb/queries.log第5步: 配置MariaDB数据库的安全性 systemctl status mysql -l 命令可以查看服务的完整运行状态在[root@contoso21 ~]# 和 [root@contoso22 ~]# 还有 [root@contoso23 ~]#上分别执行如下终端命令:/usr/bin/mysql_secure_installationNOTE: 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 currentpassword for the root user. If you've just installed MariaDB,andyou 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): 刚刚安装的MariaDB没有密码,此处直接回车便可成功连接数据库.OK,successfully used password,moving on...Setting the root password ensures that nobody can log into the MariaDBroot user without the proper authorisation.Set root password? [Y/n] yNew password: 123456Re-enter new password: 123456Password updated successfully!Reloading privilege tables..... Success!By default,a MariaDB installation has an anonymous user,allowing anyoneto log into MariaDB without having to have a user account created forthem. This is intended only for testing,and to make the installationgo a bit smoother. You should remove them before moving into aproduction environment.Remove anonymous users? [Y/n] y... Success!Normally,root should only be allowed to connect from 'localhost'. Thisensures that someone cannot guess at the root password from the network.Disallow root login remotely? [Y/n] n... Success!By default,MariaDB comes with a database named 'test' that anyone canaccess. This is also intended only for testing,and should be removedbefore 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 farwill 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 MariaDBinstallation should now be secure.Thanks for using MariaDB!第6步: 创建MariaDB Galera群集需要用到的多个用户Now,we have to createsome users that must be able to access the database. The‘sst_user’is the user which a database node will use for authenticating to another database node in the State Transfer Snapshot (SST) phase. Runthe following command on all nodes:在[root@contoso21 ~]# 和 [root@contoso22 ~]# 还有 [root@contoso23 ~]#上分别执行如下终端命令:mysql -uroot -p123456 -h127.0.0.1在db1 MariaDB [(none)]> 和 db2 MariaDB [(none)]> 还有 db2 MariaDB [(none)]>上分别执行如下SQL命令:USE mysql;SHOW DATABASES;SELECT host,user FROM user;DELETE FROM mysql.user WHERE user='';GRANT ALL ON *.* TO 'root'@'%' IDENTIFIED BY '123456';GRANT USAGE ON *.* to sst_user@'%' IDENTIFIED BY '123456';GRANT ALL PRIVILEGES on *.* to sst_user@'%';FLUSH PRIVILEGES;SELECT host,user FROM user;SHOW VARIABLES LIKE 'character%';SHOW VARIABLES LIKE '%collation%';QUIT建议你使用能够访问数据库的主机名称或者IP地址替代‘%’,因为‘%’ 的意思是允许用户root或者sst_user从任何主机上访问数据库,因此安全性较差.第7步: 修改MariaDB Galera群集所有节点的配置文件,设置MariaDB数据库的默认编码格式和表名称的小写格式,配置查询日志和慢查询日志,关闭所有节点的mysql服务在[root@contoso21 ~]# 和 [root@contoso22 ~]# 还有 [root@contoso23 ~]#上分别执行如下终端命令:service mysql stop[root@contoso21 ~]# cat > /etc/my.cnf.d/server.cnf## These groups are read by MariaDB server.# Use it for options that only the server (but not clients) should see## See the examples of server my.cnf files in /usr/share/mysql/## this is read by the standalone daemon and embedded servers[server]# this is only for the mysqld standalone daemon[mysqld]character-set-server=utf8lower_case_table_names=1general-log=ONgeneral-log-file=/var/log/mariadb/queries.loglog-output=fileslow-query-log=ONslow-query-log-file=/var/log/mariadb/mariadb-slow.loglong_query_time=1## * Galera-related settings#[galera]# Mandatory settingswsrep_on=ONwsrep_provider=/usr/lib64/galera/libgalera_smm.sowsrep_cluster_address='gcomm://'wsrep_cluster_name='galera1_cluster'wsrep_node_address='192.168.10.21'wsrep_node_name='db1'wsrep_sst_method=rsyncbinlog_format=rowdefault_storage_engine=InnoDBinnodb_autoinc_lock_mode=2bind-address=0.0.0.0#wsrep_on=ON#wsrep_provider=#wsrep_cluster_address=#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## Optional setting#wsrep_slave_threads=1#innodb_flush_log_at_trx_commit=0# this is only for embedded server[embedded]# This group is only read by MariaDB servers,not by MySQL.# If you use the same .cnf file for MySQL and MariaDB,# you can put MariaDB-only options here[mariadb]# This group is only read by MariaDB-10.1 servers.# If you use the same .cnf file for MariaDB of different versions,# use this group for options that older servers don't understand[mariadb-10.1][root@contoso22 ~]# cat > /etc/my.cnf.d/server.cnf## These groups are read by MariaDB server.# Use it for options that only the server (but not clients) should see## See the examples of server my.cnf files in /usr/share/mysql/## this is read by the standalone daemon and embedded servers[server]# this is only for the mysqld standalone daemon[mysqld]character-set-server=utf8lower_case_table_names=1general-log=ONgeneral-log-file=/var/log/mariadb/queries.loglog-output=fileslow-query-log=ONslow-query-log-file=/var/log/mariadb/mariadb-slow.loglong_query_time=1## * Galera-related settings#[galera]# Mandatory settingswsrep_on=ONwsrep_provider=/usr/lib64/galera/libgalera_smm.sowsrep_cluster_address='gcomm://192.168.10.21'wsrep_cluster_name='galera1_cluster'wsrep_node_address='192.168.10.22'wsrep_node_name='db2'wsrep_sst_method=rsyncbinlog_format=rowdefault_storage_engine=InnoDBinnodb_autoinc_lock_mode=2bind-address=0.0.0.0#wsrep_on=ON#wsrep_provider=#wsrep_cluster_address=#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## Optional setting#wsrep_slave_threads=1#innodb_flush_log_at_trx_commit=0# this is only for embedded server[embedded]# This group is only read by MariaDB servers,# use this group for options that older servers don't understand[mariadb-10.1][root@contoso23 ~]# cat > /etc/my.cnf.d/server.cnf## These groups are read by MariaDB server.# Use it for options that only the server (but not clients) should see## See the examples of server my.cnf files in /usr/share/mysql/## this is read by the standalone daemon and embedded servers[server]# this is only for the mysqld standalone daemon[mysqld]character-set-server=utf8lower_case_table_names=1general-log=ONgeneral-log-file=/var/log/mariadb/queries.loglog-output=fileslow-query-log=ONslow-query-log-file=/var/log/mariadb/mariadb-slow.loglong_query_time=1## * Galera-related settings#[galera]# Mandatory settingswsrep_on=ONwsrep_provider=/usr/lib64/galera/libgalera_smm.sowsrep_cluster_address='gcomm://192.168.10.21'wsrep_cluster_name='galera1_cluster'wsrep_node_address='192.168.10.23'wsrep_node_name='db3'wsrep_sst_method=rsyncbinlog_format=rowdefault_storage_engine=InnoDBinnodb_autoinc_lock_mode=2bind-address=0.0.0.0#wsrep_on=ON#wsrep_provider=#wsrep_cluster_address=#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## Optional setting#wsrep_slave_threads=1#innodb_flush_log_at_trx_commit=0# this is only for embedded server[embedded]# This group is only read by MariaDB servers,# use this group for options that older servers don't understand[mariadb-10.1]第8步: 初始化MariaDB Galera群集中第一个节点使用专门的参数项‘ --wsrep-new-cluster’来启动MariaDB Gelera群集,只在db1节点(第1个节点)上如此操作,以便群集主要的节点被初始化.[root@contoso21 ~]# /etc/init.d/mysql start --wsrep-new-cluster[root@contoso21 ~]# mysql -uroot -p123456 -e"show status like 'wsrep%'"wsrep_local_state_comment | Synced <-- cluster is syncedwsrep_incoming_addresses | 192.168.10.21:3306 <-- node db1 is a providerwsrep_cluster_size | 1 <-- cluster consists of 1 node wsrep_ready | ON <-- good :)[root@contoso21 ~]# /etc/init.d/mysql start --wsrep-new-clusterStarting mysql (via systemctl): [ OK ][root@contoso21 ~]# mysql -uroot -p123456 -e"show status like 'wsrep%'"+------------------------------+---------------------------------------------+| Variable_name | Value |+------------------------------+---------------------------------------------+| wsrep_apply_oooe | 0.000000 || wsrep_apply_oool | 0.000000 || wsrep_apply_window | 0.000000 || wsrep_causal_reads | 0 || wsrep_cert_deps_distance | 0.000000 || wsrep_cert_index_size | 0 || wsrep_cert_interval | 0.000000 || wsrep_cluster_conf_id | 1 || wsrep_cluster_size | 1 || wsrep_cluster_state_uuid | b47976ee-1ad6-11e6-b848-c6770ef480cb || wsrep_cluster_status | Primary || wsrep_commit_oooe | 0.000000 || wsrep_commit_oool | 0.000000 || wsrep_commit_window | 0.000000 || wsrep_connected | ON || wsrep_evs_delayed | || wsrep_evs_evict_list | || wsrep_evs_repl_latency | 8.91e-07/6.818e-06/1.9412e-05/6.92271e-06/5 || wsrep_evs_state | OPERATIONAL || wsrep_flow_control_paused | 0.000000 || wsrep_flow_control_paused_ns | 0 || wsrep_flow_control_recv | 0 || wsrep_flow_control_sent | 0 || wsrep_gcomm_uuid | b47891c2-1ad6-11e6-b7ef-66d1299a9d50 || wsrep_incoming_addresses | 192.168.10.21:3306 || wsrep_last_committed | 0 || wsrep_local_bf_aborts | 0 || wsrep_local_cached_downto | 18446744073709551615 || wsrep_local_cert_failures | 0 || wsrep_local_commits | 0 || wsrep_local_index | 0 || wsrep_local_recv_queue | 0 || wsrep_local_recv_queue_avg | 0.000000 || wsrep_local_recv_queue_max | 1 || wsrep_local_recv_queue_min | 0 || wsrep_local_replays | 0 || wsrep_local_send_queue | 0 || wsrep_local_send_queue_avg | 0.000000 || wsrep_local_send_queue_max | 1 || wsrep_local_send_queue_min | 0 || wsrep_local_state | 4 || wsrep_local_state_comment | Synced || wsrep_local_state_uuid | b47976ee-1ad6-11e6-b848-c6770ef480cb || wsrep_protocol_version | 7 || wsrep_provider_name | Galera || wsrep_provider_vendor | Codership Oy <info@codership.com> || wsrep_provider_version | 25.3.15(r3578) || wsrep_ready | ON || wsrep_received | 2 || wsrep_received_bytes | 140 || wsrep_repl_data_bytes | 0 || wsrep_repl_keys | 0 || wsrep_repl_keys_bytes | 0 || wsrep_repl_other_bytes | 0 || wsrep_replicated | 0 || wsrep_replicated_bytes | 0 || wsrep_thread_count | 2 |+------------------------------+---------------------------------------------+第9步: 向已经启动的MariaDB Galera群集添加另外两个节点db2和db3检查和确认db2和db3两个节点的配置文件/etc/my.cnf.d/server.cnf内部的[galera] 下面配置的所有参数是否正确,db2和db3两个节点的服务就如同像启动普通服务一样去启动MariaDB Galera节点服务.[root@contoso22 ~]# service mysql start通过在db1和db2节点上执行如下命令,观察群集状态中已经变化参数值内容:[root@contoso22 ~]# mysql -uroot -p123456 -e"show status like 'wsrep%'"至此,你会发现节点db2已被识别,群集的大小已经是2个节点,并且节点db2的IP地址已被列出:wsrep_local_state_comment | Syncedwsrep_incoming_addre sses | 192.168.10.21:3306,192.168.10.22:3306wsrep_cluster_size | 2wsrep_connected | ONwsrep_ready | ON[root@contoso22 ~]# mysql -uroot -p123456 -e"show status like 'wsrep%'"+------------------------------+--------------------------------------------------+| Variable_name | Value |+------------------------------+--------------------------------------------------+| wsrep_apply_oooe | 0.000000 || wsrep_apply_oool | 0.000000 || wsrep_apply_window | 0.000000 || wsrep_causal_reads | 0 || wsrep_cert_deps_distance | 0.000000 || wsrep_cert_index_size | 0 || wsrep_cert_interval | 0.000000 || wsrep_cluster_conf_id | 2 || wsrep_cluster_size | 2 || wsrep_cluster_state_uuid | b47976ee-1ad6-11e6-b848-c6770ef480cb || wsrep_cluster_status | Primary || wsrep_commit_oooe | 0.000000 || wsrep_commit_oool | 0.000000 || wsrep_commit_window | 0.000000 || wsrep_connected | ON || wsrep_evs_delayed | || wsrep_evs_evict_list | || wsrep_evs_repl_latency | 0.000227382/0.000654379/0.00225715/0.000659168/7 || wsrep_evs_state | OPERATIONAL || wsrep_flow_control_paused | 0.000000 || wsrep_flow_control_paused_ns | 0 || wsrep_flow_control_recv | 0 || wsrep_flow_control_sent | 0 || wsrep_gcomm_uuid | 8dc1b4b3-1ad7-11e6-8bf0-9a0cfaca8d40 || wsrep_incoming_addresses | 192.168.10.22:3306,192.168.10.21:3306 || wsrep_last_committed | 0 || wsrep_local_bf_aborts | 0 || wsrep_local_cached_downto | 18446744073709551615 || wsrep_local_cert_failures | 0 || wsrep_local_commits | 0 || wsrep_local_index | 0 || wsrep_local_recv_queue | 0 || wsrep_local_recv_queue_avg | 0.000000 || wsrep_local_recv_queue_max | 1 || wsrep_local_recv_queue_min | 0 || wsrep_local_replays | 0 || wsrep_local_send_queue | 0 || wsrep_local_send_queue_avg | 0.333333 || wsrep_local_send_queue_max | 2 || wsrep_local_send_queue_min | 0 || wsrep_local_state | 4 || wsrep_local_state_comment | Synced || wsrep_local_state_uuid | b47976ee-1ad6-11e6-b848-c6770ef480cb || wsrep_protocol_version | 7 || wsrep_provider_name | Galera || wsrep_provider_vendor | Codership Oy <info@codership.com> || wsrep_provider_version | 25.3.15(r3578) || wsrep_ready | ON || wsrep_received | 3 || wsrep_received_bytes | 216 || wsrep_repl_data_bytes | 0 || wsrep_repl_keys | 0 || wsrep_repl_keys_bytes | 0 || wsrep_repl_other_bytes | 0 || wsrep_replicated | 0 || wsrep_replicated_bytes | 0 || wsrep_thread_count | 2 |+------------------------------+--------------------------------------------------+同样地在节点db3上执行如下2个命令:[root@contoso23 ~]# service mysql start # [root@contoso23 ~]# mysql -uroot -p123456 -e"show status like 'wsrep%'"至此,你应该会发现节点db3已被识别,群集的大小已经是3个节点,并且节点db3的IP地址已被列出:wsrep_local_state_comment | Syncedwsrep_incoming_addresses | 192.168.10.22:3306,192.168.10.21:3306,192.168.10.23:3306wsrep_cluster_size | 3wsrep_connected | ONwsrep_ready | ON[root@contoso23 ~]# mysql -uroot -p123456 -e"show status like 'wsrep%'"+------------------------------+----------------------------------------------------------+| Variable_name | Value |+------------------------------+----------------------------------------------------------+| wsrep_apply_oooe | 0.000000 || wsrep_apply_oool | 0.000000 || wsrep_apply_window | 0.000000 || wsrep_causal_reads | 0 || wsrep_cert_deps_distance | 0.000000 || wsrep_cert_index_size | 0 || wsrep_cert_interval | 0.000000 || wsrep_cluster_conf_id | 3 || wsrep_cluster_size | 3 || wsrep_cluster_state_uuid | b47976ee-1ad6-11e6-b848-c6770ef480cb || wsrep_cluster_status | Primary || wsrep_commit_oooe | 0.000000 || wsrep_commit_oool | 0.000000 || wsrep_commit_window | 0.000000 || wsrep_connected | ON || wsrep_evs_delayed | || wsrep_evs_evict_list | || wsrep_evs_repl_latency | 0/0/0/0/0 || wsrep_evs_state | OPERATIONAL || wsrep_flow_control_paused | 0.000000 || wsrep_flow_control_paused_ns | 0 || wsrep_flow_control_recv | 0 || wsrep_flow_control_sent | 0 || wsrep_gcomm_uuid | 23817fc4-1ad8-11e6-8ec5-3340a442db9a || wsrep_incoming_addresses | 192.168.10.23:3306,192.168.10.22:3306,192.168.10.21:3306 || wsrep_last_committed | 0 || wsrep_local_bf_aborts | 0 || wsrep_local_cached_downto | 18446744073709551615 || wsrep_local_cert_failures | 0 || wsrep_local_commits | 0 || wsrep_local_index | 0 || wsrep_local_recv_queue | 0 || wsrep_local_recv_queue_avg | 0.000000 || wsrep_local_recv_queue_max | 1 || wsrep_local_recv_queue_min | 0 || wsrep_local_replays | 0 || wsrep_local_send_queue | 0 || wsrep_local_send_queue_avg | 0.000000 || wsrep_local_send_queue_max | 1 || wsrep_local_send_queue_min | 0 || wsrep_local_state | 4 || wsrep_local_state_comment | Synced || wsrep_local_state_uuid | b47976ee-1ad6-11e6-b848-c6770ef480cb || wsrep_protocol_version | 7 || wsrep_provider_name | Galera || wsrep_provider_vendor | Codership Oy <info@codership.com> || wsrep_provider_version | 25.3.15(r3578) || wsrep_ready | ON || wsrep_received | 3 || wsrep_received_bytes | 284 || wsrep_repl_data_bytes | 0 || wsrep_repl_keys | 0 || wsrep_repl_keys_bytes | 0 || wsrep_repl_other_bytes | 0 || wsrep_replicated | 0 || wsrep_replicated_bytes | 0 || wsrep_thread_count | 2 |+------------------------------+----------------------------------------------------------+第10步: 测试验证群集的复制功能现在群集正在运行,让我们测试一下它是否能正常工作.在db1节点(contoso21)上创建一个‘clustertest’ 数据库,通过如下命令:[root@contoso21 ~]# mysql -uroot -p123456 -e 'SELECT * FROM clustertest.mycluster;'[root@contoso21 ~]# mysql -uroot -p123456 -e 'CREATE DATABASE clustertest;'[root@contoso21 ~]# mysql -uroot -p123456 -e 'CREATE TABLE clustertest.mycluster ( id INT NOT NULL AUTO_INCREMENT,name VARCHAR(50),birth DATE,PRIMARY KEY(id));'[root@contoso21 ~]# mysql -uroot -p123456 -e 'INSERT INTO clustertest.mycluster (name,birth) VALUES ("Tim","1989-12-27");'[root@contoso21 ~]# mysql -uroot -p123456 -e 'SELECT * FROM clustertest.mycluster;'+----+------+------------+| id | name | birth |+----+------+------------+| 3 | Tim | 1989-12-27 |+----+------+------------+[root@contoso22 ~]# mysql -uroot -p123456 -e 'SELECT * FROM clustertest.mycluster;'+----+------+------------+| id | name | birth |+----+------+------------+| 3 | Tim | 1989-12-27 |+----+------+------------+[root@contoso23 ~]# mysql -uroot -p123456 -e 'SELECT * FROM clustertest.mycluster;'+----+------+------------+| id | name | birth |+----+------+------------+| 3 | Tim | 1989-12-27 |+----+------+------------+从以上输出的结果我们能断定群集是正常工作的,群集通过了节点db1实现了与所有节点之间的复制操作.当我们群集关闭群集时,按照逆向启动顺序去关闭节点先关闭db3节点,,然后关闭db2节点,最后关闭db1节点当我们再一次启动群集时,按照以下命令顺序启动群集:[root@contoso21 ~]# /etc/init.d/mysql start --wsrep-new-cluster ## 第1步,第1个节点必须如此启动[root@contoso22 ~]# service mysql start ## 第2步[root@contoso23 ~]# service mysql start ## 第3步[root@contoso21 ~]# systemctl status mysql -l[root@contoso22 ~]# systemctl status mysql -l[root@contoso23 ~]# systemctl status mysql -lKeepAlived + HaProxy的高可用性方案配置hostname ip address VIP loadbalance1.com 192.168.100.55 192.168.10.200loadbalance2.com 192.168.100.66 192.168.10.200[root@loadbalance1 ~]# ss -anop | column[root@loadbalance2 ~]# ss -anop | column使用vi命令删除全部被编辑文件的内容第1步:输入 G 光标跳到文件末尾第2步:输入 :1,.d 回车当我们分配一个IP地址192.168.10.200当作虚拟IP地址(VIP)来使用的时候.我们需要做的第一件事情是告诉操作系统的内核,我们计划将IP地址绑定到没有被定义的接口上.为了达到这个目的我们需要编辑配置文件/etc/sysctl.conf,并且在该文件末尾添加如下行:net.ipv4.ip_nonlocal_bind=1[root@loadbalance1 ~]# vi /etc/sysctl.confnet.ipv4.ip_nonlocal_bind=1[root@loadbalance2 ~]# vi /etc/sysctl.confnet.ipv4.ip_nonlocal_bind=1然后,我们需要执行如下命令在不重新启动系统的情况下能够使VIP设置生效:[root@loadbalance1 ~]# sysctl -p[root@loadbalance2 ~]# sysctl -p[root@loadbalance1 ~]# vi /etc/hosts192.168.100.55 loadbalance1.com loadbalance1127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4::1 localhost localhost.localdomain localhost6 localhost6.localdomain6[root@loadbalance2 ~]# vi /etc/hosts192.168.100.66 loadbalance2.com loadbalance2127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4::1 localhost localhost.localdomain localhost6 localhost6.localdomain6[root@loadbalance1 ~]# vi /etc/selinux/config# disabled - No SELinux policy is loaded.SELINUX=disabled[root@loadbalance2 ~]# vi /etc/selinux/config# disabled - No SELinux policy is loaded.SELINUX=disabled[root@loadbalance1 ~]# systemctl stop firewalld && systemctl disable firewalld[root@loadbalance2 ~]# systemctl stop firewalld && systemctl disable firewalld[root@loadbalance1 ~]# yum -y install keepalived haproxy[root@loadbalance2 ~]# yum -y install keepalived haproxy[root@loadbalance1 ~]# cat /etc/keepalived/keepalived.conf[root@loadbalance2~]# cat /etc/keepalived/keepalived.conf[root@loadbalance1 ~]# vi /etc/keepalived/keepalived.confglobal_defs {notification_email {14588019@qq.com}notification_email_from 14588019@qq.comsmtp_server localhostsmtp_connect_timeout 30router_id loadbalance1.com}vrrp_script haproxy {script "killall -0 haproxy"interval 2weight 2}vrrp_instance 50 {virtual_router_id 50advert_int 1priority 101state MASTERinterface eno16777736virtual_ipaddress {192.168.10.200 dev eno16777736}track_script {haproxy}}[root@loadbalance2 ~]# vi /etc/keepalived/keepalived.conf global_defs {notification_email {14588019@qq.com}notification_email_from 14588019@qq.comsmtp_server localhostsmtp_connect_timeout 30router_id loadbalance2.com}vrrp_script haproxy {script "killall -0 haproxy"interval 2weight 2}vrrp_instance 50 {virtual_router_id 50advert_int 1priority 99state SLAVEinterface eno16777736virtual_ipaddress {192.168.10.200 dev eno16777736}track_script {haproxy}}[root@loadbalance1 ~]# vi /etc/haproxy/haproxy.cfgglobal chroot /var/lib/haproxy daemon group haproxy log 192.168.100.55 local2 maxconn 4000 pidfile /var/run/haproxy.pid stats socket /var/lib/haproxy/stats user haproxy defaults log global retries 3 timeout queue 1m timeout connect 10s timeout client 1m timeout server 1m listen mariadb-cluster bind 192.168.10.200:3306 mode tcp balance roundrobin server mariadb1 192.168.10.21:3306 server mariadb2 192.168.10.22:3306 server mariadb3 192.168.10.23:3306[root@loadbalance2 ~]# vi /etc/haproxy/haproxy.cfgglobal chroot /var/lib/haproxy daemon group haproxy log 192.168.10.66 local2 maxconn 4000 pidfile /var/run/haproxy.pid stats socket /var/lib/haproxy/stats user haproxy defaults log global retries 3 timeout queue 1m timeout connect 10s timeout client 1m timeout server 1m listen mariadb-cluster bind 192.168.10.200:3306 mode tcp balance roundrobin server mariadb1 192.168.10.21:3306 server mariadb2 192.168.10.22:3306 server mariadb3 192.168.10.23:3306[root@loadbalance1 ~]# vi /etc/default/haproxyENABLED=1[root@loadbalance2 ~]# vi /etc/default/haproxyENABLED=1[root@loadbalance1 ~]# systemctl enable keepalived[root@loadbalance2 ~]# systemctl enable keepalived[root@loadbalance1 ~]# systemctl restart keepalived[root@loadbalance2 ~]# systemctl restart keepalived[root@loadbalance1 ~]# systemctl enable haproxy[root@loadbalance2 ~]# systemctl enable haproxy[root@loadbalance1 ~]# systemctl restart haproxy[root@loadbalance2 ~]# systemctl restart haproxyshow status like 'wsrep%';show variables like 'wsrep_node%';[root@loadbalance1 ~]# cat > /etc/yum.repos.d/MariaDB.repo[root@loadbalance2 ~]# cat > /etc/yum.repos.d/MariaDB.repo[mariadb]name = MariaDBbaseurl = http://yum.mariadb.org/10.1/centos7-amd64gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDBgpgcheck=1 [root@loadbalance1 ~]# yum -y install MariaDB-client[root@loadbalance2 ~]# yum -y install MariaDB-client[root@loadbalance1 ~]# mysql -uroot -p123456 -h192.168.10.200 -e"show variables like 'wsrep_node%'"+-----------------------------+-----------------+| Variable_name | Value |+-----------------------------+-----------------+| wsrep_node_address | 192.168.10.21 || wsrep_node_incoming_address | AUTO || wsrep_node_name | db1 |+-----------------------------+-----------------+[root@loadbalance1 ~]# mysql -uroot -p123456 -h192.168.10.200 -e"show variables like 'wsrep_node%'"+-----------------------------+-----------------+| Variable_name | Value |+-----------------------------+-----------------+| wsrep_node_address | 192.168.10 .22 || wsrep_node_incoming_address | AUTO || wsrep_node_name | db2 |+-----------------------------+-----------------+[root@loadbalance1 ~]# mysql -uroot -p123456 -h192.168.10.200 -e"show variables like 'wsrep_node%'"+-----------------------------+-----------------+| Variable_name | Value |+-----------------------------+-----------------+| wsrep_node_address | 192.168.10.23 || wsrep_node_incoming_address | AUTO || wsrep_node_name | db3 |+-----------------------------+-----------------+[root@loadbalance1 ~]# for i in `seq 1 6`domysql -uroot -p123456 -h192.168.10.200 -e"show variables like 'wsrep_node%'"done[root@loadbalance1 ~]# for i in `seq 1 6`> do> mysql -uroot -p123456 -h192.168.10.200 -e"show variables like 'wsrep_node%'"> done+-----------------------------+-----------------+| Variable_name | Value |+-----------------------------+-----------------+| wsrep_node_address | 192.168.10.21 || wsrep_node_incoming_address | AUTO || wsrep_node_name | db1 |+-----------------------------+-----------------++-----------------------------+-----------------+| Variable_name | Value |+-----------------------------+-----------------+| wsrep_node_address | 192.168.10.22 || wsrep_node_incoming_address | AUTO || wsrep_node_name | db2 |+-----------------------------+-----------------++-----------------------------+-----------------+| Variable_name | Value |+-----------------------------+-----------------+| wsrep_node_address | 192.168.10.23 || wsrep_node_incoming_address | AUTO || wsrep_node_name | db3 |+-----------------------------+-----------------++-----------------------------+-----------------+| Variable_name | Value |+-----------------------------+-----------------+| wsrep_node_address | 192.168.10.21 || wsrep_node_incoming_address | AUTO || wsrep_node_name | db1 |+-----------------------------+-----------------++-----------------------------+-----------------+| Variable_name | Value |+-----------------------------+-----------------+| wsrep_node_address | 192.168.10.22 || wsrep_node_incoming_address | AUTO || wsrep_node_name | db2 |+-----------------------------+-----------------++-----------------------------+-----------------+| Variable_name | Value |+-----------------------------+-----------------+| wsrep_node_address | 192.168.10.23 || wsrep_node_incoming_address | AUTO || wsrep_node_name | db3 |+-----------------------------+-----------------+配置haproxy日志功能[root@loadbalance1 ~]# vi /etc/rsyslog.conf# Provides UDP syslog reception$ModLoad imudp$UDPServerRun 514local2.* /var/log/haproxy.log[root@loadbalance1 ~]# vi /etc/sysconfig/rsyslogSYSLOGD_OPTIONS="-r -m 0"[root@loadbalance1 ~]# systemctl restart rsyslog[root@loadbalance1 ~]# tail -f /var/log/haproxy.log

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


linux下开机自启: 在/etc/init.d目录下新建文件elasticsearch 并敲入shell脚本: 注意, 前两行必须填写,且要注释掉。 第一行为shell前行代码,目的告诉系统使用shell。 第二行分别代表运行级别、启动优先权、关闭优先权,且后面添加开机服务会用到。 shell脚本
1、因为在centos7中/etc/rc.d/rc.local的权限被降低了,所以需要赋予其可执行权 chmod +x /etc/rc.d/rc.local 2、赋予脚本可执行权限假设/usr/local/script/autostart.sh是你的脚本路径,给予执行权限 chmod +x /usr
最简单的查看方法可以使用ls -ll、ls-lh命令进行查看,当使用ls -ll,会显示成字节大小,而ls- lh会以KB、MB等为单位进行显示,这样比较直观一些。 通过命令du -h –max-depth=1 *,可以查看当前目录下各文件、文件夹的大小,这个比较实用。 查询当前目录总大小可以使用d
ASP.NET Core应用程序发布linux在shell中运行是正常的。可一但shell关闭网站也就关闭了,所以要配置守护进程, 用的是Supervisor,本文主要记录配置的过程和过程遇到的问题 安装Supervisor&#160;1 yum install python-setuptools
设置时区(CentOS 7) 先执行命令timedatectl status|grep &#39;Time zone&#39;查看当前时区,如果不是时区(Asia/Shanghai),则需要先设置为中国时区,否则时区不同会存在时差。 #已经是Asia/Shanghai,则无需设置 [root@xia
vim&#160;/etc/sysconfig/network-scripts/ifcfg-eth0 BOOTPROTO=&quot;static&quot; ONBOOT=yes IPADDR=192.168.8.106 NETMASK=255.255.252.0 GATEWAY=192.168.
一、安装gcc依赖 由于 redis 是用 C 语言开发,安装之前必先确认是否安装 gcc 环境(gcc -v),如果没有安装,执行以下命令进行安装 [root@localhost local]# yum install -y gcc 二、下载并解压安装包 [root@localhost local
第一步 On CentOS/RHEL 6.*: $ sudo rpm -Uvh http://li.nux.ro/download/nux/dextop/el6/x86_64/nux-dextop-release-0-2.el6.nux.noarch.rpm On CentOS/RHEL 7: $
/// &lt;summary&gt; /// 取小写文件名后缀 /// &lt;/summary&gt; /// &lt;param name=&quot;name&quot;&gt;文件名&lt;/param&gt; /// &lt;returns&gt;返回小写后缀,不带“.”&lt;/ret
which nohup .bash_profile中并source加载 如果没有就安装吧 yum provides */nohup nohup npm run start &amp; nohup ./kibana &amp;
1.1 MySQL安装 1.1.1 下载wget命令 yum -y install wget 1.1.2 在线下载mysql安装包 wget https://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm 1.1.3 安装My
重启 reboot shutdown -r now init 6 关闭 init 0 shutdown -h now shutdown -h 20:25 #8点25关机查看内存 free CPU利用率 top 日期 date 设置时间 date 033017002015 #月日时间年 日历 cal
1、firewalld的基本使用 启动: systemctl start firewalld 关闭: systemctl stop firewalld 查看状态: systemctl status firewalld 开机禁用 : systemctl disable firewalld 开机启用 :
1 下载并安装MySQL官方的&#160;Yum Repository wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm 使用上面的命令就直接下载了安装用的Yum Repository,大概
CentOS6.x CentOS6中转用Upstrat代替以前的init.d/rcX.d的线性启动方式。 一、相关命令 通过initctl help可以查看相关命令 [root@localhost ~]# initctl help Job commands: start Start job. sto
1、使用命令:df -lk 找到已满磁盘 2、使用命令:du --max-depth=1 -h 查找大文件,删除
ifconfig:查看网卡信息 网卡配置文件位置: /etc/sysconfig/network-scripts/文件夹 nmtui:配置网卡 netstat -tlunp:查看端口信息 端口信息存储位置: /etc/services文件 route:查看路由信息 wget:下载网路文件,例如 wg
ps -ef:查看所有进程,&#160;ps -ef |grap firewalld 查看与firewalld相关的进程 which :查看进程:which firewalld kill 进程id:杀掉进程 kill 640,强制杀:kill -9 640 man:查看帮助,例如 man ps 查看
useradd:添加用户 useradd abc,默认添加一个abc组 vipw:查看系统中用户 groupadd:添加组groupadd ccna vigr:查看系统中的组 gpasswd:将用户abc添加到ccna组 gpasswd -a abc ccna groups abc:查看用户abc属