--- title: "CentOS7 部署 Mariadb Galera 集群" date: 2019-10-30T14:01:06+08:00 lastmod: 2019-10-30T14:01:06+08:00 tags: ["mariadb", "galera"] categories: ["database"] --- # 环境 cpu | mem | hostname | public ip | cluster ip | CentOS | MariaDB ---- | ---- | ---- | ---- | ---- | ---- | ---- 双核 | 2GB | mariadb_1 | 10.0.0.231 | 10.10.10.1 | 7.5 | 10.1.33 双核 | 2GB | mariadb_2 | 10.0.0.232 | 10.10.10.2 | 7.5 | 10.1.33 双核 | 2GB | mariadb_3 | 10.0.0.233 | 10.10.10.3 | 7.5 | 10.1.33 # 安装数据库 ### 离线安装 - [下载 rpm](http://yum.mariadb.org/10.1/centos7-amd64/rpms/) - MariaDB-10.1.33-centos7-x86_64-client.rpm - MariaDB-10.1.33-centos7-x86_64-common.rpm - MariaDB-10.1.33-centos7-x86_64-server.rpm - MariaDB-10.1.33-centos7-x86_64-shared.rpm - galera-25.3.23-1.rhel7.el7.centos.x86_64.rpm - jemalloc-3.6.0-1.el7.x86_64.rpm - 安装 rpm ```bash yum erase mariadb-libs rpm -ivh *.rpm ``` - 安装后会自动执行数据库初始化脚本,如果未执行,可手动运行 ```bash mysql_install_db --user=mysql ``` ### yum 安装 - 创建 MariaDB-10.1 的 yum 源文件(就近选择一个) ```bash #官方 cat > /etc/yum.repos.d/MariaDB.repo <<-END [mariadb] name = MariaDB baseurl = http://yum.mariadb.org/10.1/centos7-amd64 gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB gpgcheck=1 END #中科大 cat > /etc/yum.repos.d/MariaDB.repo <<-END [mariadb] name = MariaDB baseurl = https://mirrors.ustc.edu.cn/mariadb/yum/10.1/centos7-amd64 gpgkey=https://mirrors.ustc.edu.cn/mariadb/yum/RPM-GPG-KEY-MariaDB gpgcheck=1 END #上海大学 cat > /etc/yum.repos.d/MariaDB.repo <<-END [mariadb] name = MariaDB baseurl = https://mirrors.shu.edu.cn/mariadb/yum/10.1/centos7-amd64 gpgkey=https://mirrors.shu.edu.cn/mariadb/yum/RPM-GPG-KEY-MariaDB gpgcheck=1 END ``` - 安装 MariaDB ```bash yum install MariaDB-client MariaDB-server # galera_4 会作为依赖自动安装 ``` # 启动server ```bash systemctl start mariadb ``` # 安全设置 - 设置root账户密码(推荐) ```bash mysqladmin -u root password 'password' ``` - 数据库安全设置(推荐) ```bash mysql_secure_installation ``` # 配置Galera Cluster - 修改/etc/my.cnf.d/server.cnf如下 ``` [server] innodb-flush-log-at-trx-commit=0 innodb-buffer-pool-size=1024M #一半内存 innodb-autoinc-lock-mode=2 default-storage-engine=InnoDB # [mysqld] # [galera] wsrep-on=ON wsrep-provider = /usr/lib64/galera/libgalera_smm.so wsrep-provider-options="gcache.dir=/var/lib/gcache;gcache.size=1G;gcache.recover=yes;pc.recovery=TRUE" wsrep-cluster-name="mariadb_galera_cluster" wsrep-cluster-address = "gcomm://10.10.10.1,10.10.10.2,10.10.10.3" wsrep-node-name = mariadb_1 #当前节点名字 wsrep-node-address = 10.10.10.1 #当前节点地址 binlog-format=ROW wsrep-slave-threads=2 wsrep-sst-method=rsync #wsrep-auto-increment-control=OFF #只通过一个节点做增删改时使用 # [embedded] # [mariadb] # [mariadb-10.1] ``` # 停止 server ```bash systemctl stop mariadb ``` # 启动集群 - 启动 galera cluster ```bash mysqld --wsrep-new-cluster --user=mysql ``` - 查看集群状态 ```sql show status like 'wsrep_%'; ``` - 在剩余两台服务器启动 server,向集群中添加节点 ```bash systemctl start mariadb ``` - 再次查看集群状态 ```sql show status like 'wsrep_%'; ``` # 注意事项 - 防火墙开放 3306、4444 和 4567 端口 - 关闭 selinux - 集群关闭时,/var/lib/mysql/grastate.dat 文件中 safe_to_bootstrap 项为 1 的节点服务器是最后关闭的数据库,数据最全,所以下次集群启动时应从这台节点服务器启动