上篇文章介绍了openstack组件rabbitmq高可用,现在介绍其另一个重要组件mysql高可用。
我是本次选择mysql的版本是mariadb,集群方法是galera cluster多主集群。
其实也有很多其他方案,如pxc、mha等等,选择galera的原因是安装方便,使用与维护也方便,多主模式任何一个节点挂了都可以在另外节点查看数据,同时openstack各组件也支持配置集群方式的配置。
简介
MariaDB Galera Cluster 是一套在mysql innodb存储引擎上面实现multi-master及数据实时同步的系统架构,业务层面无需做读写分离工作,数据库读写压力都能按照既定的规则分发到 各个节点上去。在数据方面完全兼容 MariaDB 和 MySQL。
特性
(1).同步复制 Synchronous replication (2).Active-active multi-master 拓扑逻辑 (3).可对集群中任一节点进行数据读写 (4).自动成员控制,故障节点自动从集群中移除 (5).自动节点加入 (6).真正并行的复制,基于行级 (7).直接客户端连接,原生的 MySQL 接口 (8).每个节点都包含完整的数据副本 (9).多台数据库中数据同步由 wsrep 接口实现
不过局限性也有很多,比如复制仅支持innode,节点必须是3个等等,具体看官网介绍。
下面是安装
环境
系统centos 7.1
/etc/hosts是
node1 192.168.1.18 node2 192.168.1.20 node3 192.168.1.19
一、安装基础库(所有节点运行)
yum -y install make cmake bc gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5-devel libidn libidn-devel openssl openssl-devel nss_ldap openldap openldap-devel openldap-clients openldap-servers libxslt-devel libevent-devel ntp libtool-ltdl bison libtool vim-enhanced tar wget readline-devel libyaml-devel patch telnet lrzsz sysstat screen parted rsync libselinux-python dmidecode ntpdate sar openssh-clients man
二、配置mariadb源(所有节点运行)
cat >/etc/yum.repos.d/mariadb.repo <
三、安装(所有节点运行)
yum install MariaDB-server MariaDB-client -y
四、配置
1、在node1里运行
主要是修改bind-address、wsrep_cluster_name、wsrep_node_address、wsrep_node_name
[root@mysql-cluster-1 my.cnf.d]# cat /etc/my.cnf.d/server.cnf |grep -v '^#'|sed '/^$/d' [server] [mysqld] collation-server = utf8_general_ci init-connect = 'SET NAMES utf8' character-set-server = utf8 skip-name-resolve skip-host-cache open_files_limit = 65535 max_connections = 5000 bind-address=192.168.1.18 binlog_format=row default_storage_engine=InnoDB innodb_autoinc_lock_mode=2 innodb_file_per_table character-set-server = utf8 [galera] wsrep_on=ON wsrep_provider=/usr/lib64/galera/libgalera_smm.so wsrep_provider_options="pc.recovery=TRUE;gcache.size=300M" wsrep_cluster_address='gcomm://' wsrep_cluster_name='ck-galera' wsrep_node_address='192.168.1.18' wsrep_node_name='m-1' wsrep_sst_method=rsync [embedded] [mariadb] [mariadb-10.1]
启动
systemctl enable mariadb systemctl start mariadb
初始化
[root@mysql-cluster-1 log]# 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] 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] 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!
查询集群数量
[root@mysql-cluster-1 log]# mysql -uroot -p -e "show status where Variable_name like 'wsrep_cluster_size'" Enter password: +--------------------+-------+ | Variable_name | Value | +--------------------+-------+ | wsrep_cluster_size | 1 | +--------------------+-------+
可以看到只有一个
查看当前集群ip
[root@mysql-cluster-1 log]# mysql -uroot -p -e "show status where Variable_name like 'wsrep_incoming_addresses'" Enter password: +--------------------------+-------------------+ | Variable_name | Value | +--------------------------+-------------------+ | wsrep_incoming_addresses | 192.168.1.18:3306 | +--------------------------+-------------------+
2、在node里配置
同样修改bind-address、wsrep_cluster_name、wsrep_node_address、wsrep_node_name
[root@mysql-cluster-2 log]# grep -v '^#' /etc/my.cnf.d/server.cnf |sed '/^$/d' [server] [mysqld] collation-server = utf8_general_ci init-connect = 'SET NAMES utf8' character-set-server = utf8 skip-name-resolve skip-host-cache open_files_limit = 65535 max_connections = 5000 bind-address=192.168.1.20 binlog_format=row default_storage_engine=InnoDB innodb_autoinc_lock_mode=2 innodb_file_per_table character-set-server = utf8 [galera] wsrep_on=ON wsrep_provider=/usr/lib64/galera/libgalera_smm.so wsrep_provider_options="pc.recovery=TRUE;gcache.size=300M" wsrep_cluster_address='gcomm://192.168.1.18' wsrep_cluster_name='ck-galera' wsrep_node_address='192.168.1.20' wsrep_node_name='m-2' wsrep_sst_method=rsync [embedded] [mariadb] [mariadb-10.1]
启动
systemctl enable mariadb systemctl start mariadb
查看集群数量
[root@mysql-cluster-2 log]# mysql -uroot -p -e "show status where Variable_name like 'wsrep_cluster_size'" Enter password: +--------------------+-------+ | Variable_name | Value | +--------------------+-------+ | wsrep_cluster_size | 2 | +--------------------+-------+
可以看到已经有2个节点了
查看集群ip
[root@mysql-cluster-2 log]# mysql -uroot -p -e "show status where Variable_name like 'wsrep_incoming_addresses'" Enter password: +--------------------------+-------------------------------------+ | Variable_name | Value | +--------------------------+-------------------------------------+ | wsrep_incoming_addresses | 192.168.1.20:3306,192.168.1.18:3306 | +--------------------------+-------------------------------------+
可以看到node1与node2都在里面
3、在node3里配置
修改跟node1与node2一样
[root@mysql-cluster-3 ~]# grep -v '^#' /etc/my.cnf.d/server.cnf |sed '/^$/d' [server] [mysqld] collation-server = utf8_general_ci init-connect = 'SET NAMES utf8' character-set-server = utf8 skip-name-resolve skip-host-cache open_files_limit = 65535 max_connections = 5000 bind-address=192.168.1.19 binlog_format=row default_storage_engine=InnoDB innodb_autoinc_lock_mode=2 innodb_file_per_table character-set-server = utf8 [galera] wsrep_on=ON wsrep_provider=/usr/lib64/galera/libgalera_smm.so wsrep_provider_options="pc.recovery=TRUE;gcache.size=300M" wsrep_cluster_address='gcomm://192.168.1.18,192.168.1.20' wsrep_cluster_name='ck-galera' wsrep_node_address='192.168.1.19' wsrep_node_name='m-3' wsrep_sst_method=rsync [embedded] [mariadb] [mariadb-10.1]
启动
systemctl enable mariadb systemctl start mariadb
查看集群数量
[root@mysql-cluster-3 ~]# mysql -uroot -p -e "show status where Variable_name like 'wsrep_cluster_size'" Enter password: +--------------------+-------+ | Variable_name | Value | +--------------------+-------+ | wsrep_cluster_size | 3 | +--------------------+-------+
查看集群IP
[root@mysql-cluster-3 ~]# mysql -uroot -p -e "show status where Variable_name like 'wsrep_incoming_addresses'" Enter password: +--------------------------+-------------------------------------------------------+ | Variable_name | Value | +--------------------------+-------------------------------------------------------+ | wsrep_incoming_addresses | 192.168.1.19:3306,192.168.1.20:3306,192.168.1.18:3306 | +--------------------------+-------------------------------------------------------+
可以看到3个节点都在里面了。
下一步大家如果想测试,可以在任意一个节点里创建数据库与表,插入数据后,在另外节点里查看是否有对应信息,我这里就不列举了。
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/55634.html