MariaDB集群搭建
一、環境簡介
序列 | 主機名 | IP | OS版本 |
1 | db136 | 192.168.142.136 | CentOS7.6 |
2 | db137 | 192.168.142.137 | CentOS7.6 |
3 | db138 | 192.168.142.138 | CentOS7.6 |
Galera版本:galera-4-26.4.3-1.rhel7.el7.centos.x86_64.rpm
MariaDB版本:mariadb-10.5.0-linux-systemd-x86_64.tar.gz (注意,該版本是當前最新測試版本,請勿用於生產環境)
二、系統依賴包安裝
由於筆者所使用的是vmware的虛擬機,故使用了最小化安裝方式,所以需要安裝大量的依賴包。
Centos最小化安裝后續操作設置可參考:https://www.cnblogs.com/bjx2020/p/12125386.html
yum install -y git scons gcc gcc-c++ openssl check cmake bison boost-devel asio-devel libaio-devel ncurses-devel readline-devel pam-devel socat

yum install -y net-tools yum install -y wget # 切換aliyun的yum源 cd /etc/yum.repos.d/ mv CentOS-Base.repo CentOS-Base.repo.bak wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo # 重建源數據緩存 yum clean all yum makecache yum -y install vim-enhanced wget net-tools telnet vim lrzsz ntp yum -y install lshw pciutils gdisk system-storage-manager yum -y install bash-completion zip unzip bzip2 tree tmpwatch pinfo man-pages yum -y install nano vim-enhanced tmux screen yum -y install net-tools psmisclsof sysstat yum -y install yum-plugin-security yum-utils createrepo yum -y install get wget curl eliks lynx lftp mailx mutt reync yum -y install libaio make cmake gcc-c++ gcc zib zlib-devel open openssl-devel pcre pcre-devel
三、設置時間同步
yum -t install ntp ntpdate asia.pool.ntp.org # systemctl stop ntpd.service cat >>/var/spool/cron/root<<"EOF" */10 * * * * /usr/sbin/ntpdate asia.pool.ntp.org >/dev/null EOF
四、防火牆設置

# 關閉CentOS7自帶的防火牆 firewall 啟用 IPtable systemctl stop firewalld systemctl disable firewalld.service #安裝IPtables防火牆 yum install -y iptables-services #開放443端口(HTTPS) iptables -A INPUT -p tcp --dport 443 -j ACCEPT #保存上述規則 service iptables save #開啟服務 systemctl restart iptables.service systemctl enable iptables.service
# 修改iptables配置文件,開放以下端口 (默認開啟了22端口, # 以便putty等軟件的連接,實例開啟80端口和3306端口, # 以便后期lamp環境使用,注:80 為Apache默認端口,3306為MySQL的默認端口) iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT service iptables save service iptables reload
# 自帶的firwalld關閉方式
systemctl stop firewalld
systemctl disable firewalld
五、關閉selinux
# 關閉selinux cp /etc/selinux/config /etc/selinux/config.bak sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config sed -i 's/SELINUXTYPE=targeted/# SELINUXTYPE=targeted/' /etc/selinux/config setenforce 0
六、文件描述符修改

sed -i 's/4096/unlimited/' /etc/security/limits.d/20-nproc.conf cat >>/etc/sysctl.conf <<"EOF" ################################################################ net.ipv4.tcp_keepalive_time =600 net.ipv4.tcp_syncookies = 1 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_timestamps=1 net.ipv4.tcp_tw_recycle=1 net.ipv4.tcp_fin_timeout = 30 net.ipv4.ip_local_port_range = 32768 60999 net.ipv4.tcp_max_syn_backlog = 1024 net.core.somaxconn = 1024 net.ipv4.tcp_max_tw_buckets = 5000 net.ipv4.tcp_syn_retries = 1 net.ipv4.tcp_synack_retries = 1 net.core.netdev_max_backlog = 1000 net.ipv4.tcp_max_orphans = 2000 net.nf_conntrack_max = 25000000 net.netfilter.nf_conntrack_max = 25000000 net.netfilter.nf_conntrack_tcp_timeout_established = 180 net.netfilter.nf_conntrack_tcp_timeout_time_wait = 120 net.netfilter.nf_conntrack_tcp_timeout_close_wait = 60 net.netfilter.nf_conntrack_tcp_timeout_fin_wait = 120 # 結合DDOS和TIME_WAIT過多,建議增加如下參數設置: # Use TCP syncookies when needed net.ipv4.tcp_syncookies = 1 net.ipv4.tcp_synack_retries=3 net.ipv4.tcp_syn_retries=3 net.ipv4.tcp_max_syn_backlog=2048 # Enable TCP window scaling # net.ipv4.tcp_window_scaling: = 1 # Increase TCP max buffer size net.core.rmem_max = 16777216 net.core.wmem_max = 16777216 # Increase Linux autotuning TCP buffer limits net.ipv4.tcp_rmem = 4096 87380 16777216 net.ipv4.tcp_wmem = 4096 65536 16777216 # Increase number of ports available net.ipv4.tcp_fin_timeout = 30 net.ipv4.tcp_keepalive_time = 300 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_tw_recycle = 1 net.ipv4.ip_local_port_range = 5000 65000 ################################################################ EOF sysctl -p cat >>/etc/bashrc<<"EOF" ulimit -u 65536 EOF source /etc/bashrc cat >>/etc/security/limits.conf <<"EOF" * hard nofile 1000000 * soft nofile 1000000 EOF
七、配置IP主機配置關系
# 配置主機對應關系 cat >>/etc/hosts<<"EOF" 192.168.142.136 db136 192.168.142.137 db137 192.168.142.138 db138 EOF
八、配置公鑰認證(可選)
# 配置公鑰認證(可選) ssh-keygen # 一路回車即可(三台機器都需要操作) ssh-copy-id -i ~/.ssh/id_rsa.pub db137 (只需要在第一台操作) ssh-copy-id -i ~/.ssh/id_rsa.pub db138 (只需要在第一台操作) # 驗證登錄 ssh root@db137 ssh root@db138
九、創建mysql用戶
# 創建用戶 useradd -M -r -s /bin/nologin mysql echo "mysql"|passwd --stdin mysql
十、Galera參數概覽
# node1

# node1 vim /etc/my.cnf 在 [galera]下面加入以下內容 wsrep_on=ON wsrep_provider=/usr/lib64/galera-4/libgalera_smm.so wsrep_cluster_address="gcomm://192.168.142.136,192.168.142.137,192.168.142.138" wsrep_node_name=db136 binlog_format=row default_storage_engine=InnoDB innodb_autoinc_lock_mode=2 bind-address=0.0.0.0 wsrep_cluster_name="MariaDB_Cluster" wsrep_node_address=192.168.142.136 wsrep_sst_method=rsync wsrep_slave_threads=1 innodb_flush_log_at_trx_commit=2 innodb_buffer_pool_size=10240M wsrep_sst_auth=bakuser:Mqh7pFCTLqaV
# node2

# node2 vim /etc/my.cnf 在 [galera]下面加入以下內容 wsrep_on=ON wsrep_provider=/usr/lib64/galera-4/libgalera_smm.so wsrep_cluster_address="gcomm://192.168.142.136,192.168.142.137,192.168.142.138" wsrep_node_name=db137 binlog_format=row default_storage_engine=InnoDB innodb_autoinc_lock_mode=2 bind-address=0.0.0.0 wsrep_cluster_name="MariaDB_Cluster" wsrep_node_address=192.168.142.137 wsrep_sst_method=rsync wsrep_slave_threads=1 innodb_flush_log_at_trx_commit=2 innodb_buffer_pool_size=10240M wsrep_sst_auth=bakuser:Mqh7pFCTLqaV
# node3

# node3 vim /etc/my.cnf 在 [galera]下面加入以下內容 wsrep_on=ON wsrep_provider=/usr/lib64/galera-4/libgalera_smm.so wsrep_cluster_address="gcomm://192.168.142.136,192.168.142.137,192.168.142.138" wsrep_node_name=db138 binlog_format=row default_storage_engine=InnoDB innodb_autoinc_lock_mode=2 bind-address=0.0.0.0 wsrep_cluster_name="MariaDB_Cluster" wsrep_node_address=192.168.142.138 wsrep_sst_method=rsync wsrep_slave_threads=1 innodb_flush_log_at_trx_commit=2 innodb_buffer_pool_size=10240M wsrep_sst_auth=bakuser:Mqh7pFCTLqaV
# 注意要把 wsrep_node_name 和 wsrep_node_address 改成相應節點的 hostname 和 ip。
十一、下載安裝
11-1、第一個節點:db136
cd /opt/
wget http://yum.mariadb.org/10.5/centos74-amd64/rpms/galera-4-26.4.3-1.rhel7.el7.centos.x86_64.rpm
rpm -ivh galera-4-26.4.3-1.rhel7.el7.centos.x86_64.rpm
wget http://mariadb.mirror.nucleus.be//mariadb-10.5.0/bintar-linux-systemd-x86_64/mariadb-10.5.0-linux-systemd-x86_64.tar.gz tar -zxvf mariadb-10.5.0-linux-systemd-x86_64.tar.gz -C /usr/local/ cd /usr/local/ ln -s mariadb-10.5.0-linux-systemd-x86_64/ mysql echo "PATH=/usr/local/mysql/bin:$PATH" > /etc/profile.d/mysql.sh
# 設置第一個節點的my.cnf

# db136 cat >/etc/my.cnf<<"EOF" [client] port = 3306 socket = /data/mysql/data/mysql.sock default-character-set=utf8mb4 [mysql] prompt="\u@MariaDB \R:\m:\s [\d]> " no-auto-rehash default-character-set=utf8mb4 [mysqld] user = mysql port = 3306 extra_port=13306 socket = /data/mysql/data/mysql.sock basedir= /usr/local/mysql datadir= /data/mysql/data log-error=/data/mysql/logs/mysql-error.log pid-file=/data/mysql/data/mariadb.pid character-set-server = utf8mb4 skip_name_resolve = 1 open_files_limit = 65535 back_log = 1024 max_connections = 512 max_connect_errors = 1000000 table_open_cache = 1024 table_definition_cache = 1024 thread_stack = 512K external-locking = FALSE max_allowed_packet = 32M sort_buffer_size = 4M join_buffer_size = 4M thread_cache_size = 768 query_cache_size = 0 query_cache_type = 0 interactive_timeout = 600 wait_timeout = 600 tmp_table_size = 32M max_heap_table_size = 32M slow_query_log = 1 slow_query_log_file = /data/mysql/logs/slow.log long_query_time = 0.1 log_queries_not_using_indexes =1 min_examined_row_limit = 100 log_slow_admin_statements = 1 log_slow_slave_statements = 1 server-id = 3306 log-bin = /data/mysql/logs/mybinlog sync_binlog = 1 binlog_cache_size = 4M max_binlog_cache_size = 2G max_binlog_size = 1G expire_logs_days = 30 log_slave_updates binlog_format = row binlog_checksum = 1 relay_log_recovery = 1 relay-log-purge = 1 key_buffer_size = 32M read_buffer_size = 8M read_rnd_buffer_size = 4M bulk_insert_buffer_size = 64M myisam_sort_buffer_size = 128M myisam_max_sort_file_size = 10G myisam_repair_threads = 1 lock_wait_timeout = 3600 innodb_thread_concurrency = 0 innodb_sync_spin_loops = 100 innodb_spin_wait_delay = 30 transaction_isolation = REPEATABLE-READ #innodb_additional_mem_pool_size = 16M innodb_buffer_pool_size = 5734M innodb_buffer_pool_instances = 8 innodb_data_file_path = ibdata1:1G:autoextend innodb_flush_log_at_trx_commit = 1 innodb_log_buffer_size = 32M innodb_log_file_size = 2G innodb_log_files_in_group = 2 # 根據您的服務器IOPS能力適當調整 # 一般配普通SSD盤的話,可以調整到 10000 - 20000 # 配置高端PCIe SSD卡的話,則可以調整的更高,比如 50000 - 80000 innodb_io_capacity = 8000 innodb_write_io_threads = 8 innodb_read_io_threads = 8 innodb_purge_threads = 1 innodb_open_files = 65535 innodb_max_dirty_pages_pct = 50 innodb_flush_method = O_DIRECT #innodb_checksums = 1 #innodb_file_format = Barracuda #innodb_file_format_max = Barracuda innodb_lock_wait_timeout = 10 innodb_rollback_on_timeout = 1 innodb_print_all_deadlocks = 1 innodb_file_per_table = 1 innodb_stats_on_metadata = 0 symbolic-links=0 innodb_status_file = 1 #performance_schema performance_schema = 1 # 字符集設定utf8mb4 character-set-server=utf8mb4 collation-server=utf8mb4_unicode_ci init-connect='SET NAMES utf8mb4' # 優化 optimizer_switch = "mrr=on,mrr_cost_based=on,mrr_sort_keys=on" deadlock_search_depth_short = 3 deadlock_search_depth_long = 10 deadlock_timeout_long = 10000000 deadlock_timeout_short = 5000 slave-parallel-threads=8 # gtid gtid_strict_mode=1 wsrep_gtid_mode=1 [galera] wsrep_on=ON wsrep_provider=/usr/lib64/galera-4/libgalera_smm.so wsrep_cluster_address="gcomm://192.168.142.136,192.168.142.137,192.168.142.138" wsrep_node_name=db136 binlog_format=row default_storage_engine=InnoDB innodb_autoinc_lock_mode=2 bind-address=0.0.0.0 wsrep_cluster_name="MariaDB_Cluster" wsrep_node_address=192.168.142.136 wsrep_sst_method=rsync wsrep_slave_threads=1 innodb_flush_log_at_trx_commit=2 innodb_buffer_pool_size=10240M wsrep_sst_auth=bakuser:Mqh7pFCTLqaV [mysqld_safe] nice=-19 open-files-limit=65535 [mysqldump] quick max_allowed_packet = 64M EOF SERVIER_ID=`date +%S` sed -i "s/server-id = 3306/server-id = 3306"${SERVIER_ID}"/" /etc/my.cnf mkdir -p /data/mysql/{data,logs} chown mysql.mysql -R /data/mysql chown mysql.mysql -R /usr/local/mariadb-10.5.0-linux-systemd-x86_64 chown mysql.mysql -R /usr/local/mysql
# 如果是拷貝my.cnf,只需要修改相應的參數值即可。
scp /etc/my.cnf root@db137:/etc/my.cnf scp /etc/my.cnf root@db138:/etc/my.cnf sed -i 's/wsrep_node_address=192.168.142.136/wsrep_node_address=192.168.142.137/' /etc/my.cnf sed -i 's/wsrep_node_name=db136/wsrep_node_name=db137/' /etc/my.cnf sed -i 's/wsrep_node_address=192.168.142.136/wsrep_node_address=192.168.142.138/' /etc/my.cnf sed -i 's/wsrep_node_name=db136/wsrep_node_name=db138/' /etc/my.cnf
# 特別注意: 初始化第一台(另外兩台不需要初始化)
# 初始化數據
cd /usr/local/mysql ./scripts/mysql_install_db --defaults-file=/etc/my.cnf --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/data
# 第一個節點第一次啟動: /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf --wsrep-new-cluster & # 第一個節點第一次啟動后再次啟動的命令: /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf &
# 啟動之后登陸第一個實例后設置用戶名和密碼(僅在第一個實例) mysql # 直接回車 delete from mysql.user where user=''; drop database test; # 創建管理員 grant all privileges on *.* to 'root'@'127.0.0.1' identified by 'rootpwd' with grant option; alter user 'root'@'localhost' identified by 'rootpwd'; alter user 'mysql'@'localhost' identified by 'mysqlpwd'; -- grant all privileges on *.* to 'root'@'%' identified by 'rootpwd' with grant option; GRANT RELOAD, LOCK TABLES, PROCESS, REPLICATION CLIENT ON *.* TO 'bakuser'@'%' identified by 'Mqh7pFCTLqaV'; GRANT RELOAD, LOCK TABLES, PROCESS, REPLICATION CLIENT ON *.* TO 'bakuser'@'localhost' identified by 'Mqh7pFCTLqaV'; flush privileges; exit;
# 登錄超級管理端口 mysql -h 127.0.0.1 -uroot -p'rootpwd' -P13306 # 查看服務 [root@db136 mysql]# netstat -anltp|grep mysql tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 7440/mysqld tcp 0 0 0.0.0.0:4567 0.0.0.0:* LISTEN 7440/mysqld tcp 0 0 0.0.0.0:13306 0.0.0.0:* LISTEN 7440/mysqld [root@db136 mysql]# # 啟動第二和第三個實例,讓后面兩個實例加入到集群 /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf & /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf &
11-2、第二個節點:db137
# 下載安裝
cd /opt/ wget http://yum.mariadb.org/10.5/centos74-amd64/rpms/galera-4-26.4.3-1.rhel7.el7.centos.x86_64.rpm rpm -ivh galera-4-26.4.3-1.rhel7.el7.centos.x86_64.rpm wget http://mariadb.mirror.nucleus.be//mariadb-10.5.0/bintar-linux-systemd-x86_64/mariadb-10.5.0-linux-systemd-x86_64.tar.gz tar -zxvf mariadb-10.5.0-linux-systemd-x86_64.tar.gz -C /usr/local/ cd /usr/local/ ln -s mariadb-10.5.0-linux-systemd-x86_64/ mysql echo "PATH=/usr/local/mysql/bin:$PATH" > /etc/profile.d/mysql.sh
# my.cnf配置

# 配置文件 # db137 cat >/etc/my.cnf<<"EOF" [client] port = 3306 socket = /data/mysql/data/mysql.sock default-character-set=utf8mb4 [mysql] prompt="\u@MariaDB \R:\m:\s [\d]> " no-auto-rehash default-character-set=utf8mb4 [mysqld] user = mysql port = 3306 extra_port=13306 socket = /data/mysql/data/mysql.sock basedir= /usr/local/mysql datadir= /data/mysql/data log-error=/data/mysql/logs/mysql-error.log pid-file=/data/mysql/data/mariadb.pid character-set-server = utf8mb4 skip_name_resolve = 1 open_files_limit = 65535 back_log = 1024 max_connections = 512 max_connect_errors = 1000000 table_open_cache = 1024 table_definition_cache = 1024 thread_stack = 512K external-locking = FALSE max_allowed_packet = 32M sort_buffer_size = 4M join_buffer_size = 4M thread_cache_size = 768 query_cache_size = 0 query_cache_type = 0 interactive_timeout = 600 wait_timeout = 600 tmp_table_size = 32M max_heap_table_size = 32M slow_query_log = 1 slow_query_log_file = /data/mysql/logs/slow.log long_query_time = 0.1 log_queries_not_using_indexes =1 min_examined_row_limit = 100 log_slow_admin_statements = 1 log_slow_slave_statements = 1 server-id = 3306 log-bin = /data/mysql/logs/mybinlog sync_binlog = 1 binlog_cache_size = 4M max_binlog_cache_size = 2G max_binlog_size = 1G expire_logs_days = 30 log_slave_updates binlog_format = row binlog_checksum = 1 relay_log_recovery = 1 relay-log-purge = 1 key_buffer_size = 32M read_buffer_size = 8M read_rnd_buffer_size = 4M bulk_insert_buffer_size = 64M myisam_sort_buffer_size = 128M myisam_max_sort_file_size = 10G myisam_repair_threads = 1 lock_wait_timeout = 3600 innodb_thread_concurrency = 0 innodb_sync_spin_loops = 100 innodb_spin_wait_delay = 30 transaction_isolation = REPEATABLE-READ #innodb_additional_mem_pool_size = 16M innodb_buffer_pool_size = 5734M innodb_buffer_pool_instances = 8 innodb_data_file_path = ibdata1:1G:autoextend innodb_flush_log_at_trx_commit = 1 innodb_log_buffer_size = 32M innodb_log_file_size = 2G innodb_log_files_in_group = 2 # 根據您的服務器IOPS能力適當調整 # 一般配普通SSD盤的話,可以調整到 10000 - 20000 # 配置高端PCIe SSD卡的話,則可以調整的更高,比如 50000 - 80000 innodb_io_capacity = 8000 innodb_write_io_threads = 8 innodb_read_io_threads = 8 innodb_purge_threads = 1 innodb_open_files = 65535 innodb_max_dirty_pages_pct = 50 innodb_flush_method = O_DIRECT #innodb_checksums = 1 #innodb_file_format = Barracuda #innodb_file_format_max = Barracuda innodb_lock_wait_timeout = 10 innodb_rollback_on_timeout = 1 innodb_print_all_deadlocks = 1 innodb_file_per_table = 1 innodb_stats_on_metadata = 0 symbolic-links=0 innodb_status_file = 1 #performance_schema performance_schema = 1 # 字符集設定utf8mb4 character-set-server=utf8mb4 collation-server=utf8mb4_unicode_ci init-connect='SET NAMES utf8mb4' # 優化 optimizer_switch = "mrr=on,mrr_cost_based=on,mrr_sort_keys=on" deadlock_search_depth_short = 3 deadlock_search_depth_long = 10 deadlock_timeout_long = 10000000 deadlock_timeout_short = 5000 slave-parallel-threads=8 # gtid gtid_strict_mode=1 wsrep_gtid_mode=1 [galera] wsrep_on=ON wsrep_provider=/usr/lib64/galera-4/libgalera_smm.so wsrep_cluster_address="gcomm://192.168.142.136,192.168.142.137,192.168.142.138" wsrep_node_name=db137 binlog_format=row default_storage_engine=InnoDB innodb_autoinc_lock_mode=2 bind-address=0.0.0.0 wsrep_cluster_name="MariaDB_Cluster" wsrep_node_address=192.168.142.137 wsrep_sst_method=rsync wsrep_slave_threads=1 innodb_flush_log_at_trx_commit=2 innodb_buffer_pool_size=10240M wsrep_sst_auth=bakuser:Mqh7pFCTLqaV [mysqld_safe] nice=-19 open-files-limit=65535 [mysqldump] quick max_allowed_packet = 64M EOF SERVIER_ID=`date +%S` sed -i "s/server-id = 3306/server-id = 3306"${SERVIER_ID}"/" /etc/my.cnf mkdir -p /data/mysql/{data,logs} chown mysql.mysql -R /data/mysql chown mysql.mysql -R /usr/local/mariadb-10.5.0-linux-systemd-x86_64 chown mysql.mysql -R /usr/local/mysql
# 特別注意:第二節點、第三節點不需要初始化
# 啟動該節點
/usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf &
11-3、第三個節點:db138
# 下載安裝
cd /opt/ wget http://yum.mariadb.org/10.5/centos74-amd64/rpms/galera-4-26.4.3-1.rhel7.el7.centos.x86_64.rpm rpm -ivh galera-4-26.4.3-1.rhel7.el7.centos.x86_64.rpm wget http://mariadb.mirror.nucleus.be//mariadb-10.5.0/bintar-linux-systemd-x86_64/mariadb-10.5.0-linux-systemd-x86_64.tar.gz tar -zxvf mariadb-10.5.0-linux-systemd-x86_64.tar.gz -C /usr/local/ cd /usr/local/ ln -s mariadb-10.5.0-linux-systemd-x86_64/ mysql echo "PATH=/usr/local/mysql/bin:$PATH" > /etc/profile.d/mysql.sh
# my.cnf配置

# 配置文件 # db138 cat >/etc/my.cnf<<"EOF" [client] port = 3306 socket = /data/mysql/data/mysql.sock default-character-set=utf8mb4 [mysql] prompt="\u@MariaDB \R:\m:\s [\d]> " no-auto-rehash default-character-set=utf8mb4 [mysqld] user = mysql port = 3306 extra_port=13306 socket = /data/mysql/data/mysql.sock basedir= /usr/local/mysql datadir= /data/mysql/data log-error=/data/mysql/logs/mysql-error.log pid-file=/data/mysql/data/mariadb.pid character-set-server = utf8mb4 skip_name_resolve = 1 open_files_limit = 65535 back_log = 1024 max_connections = 512 max_connect_errors = 1000000 table_open_cache = 1024 table_definition_cache = 1024 thread_stack = 512K external-locking = FALSE max_allowed_packet = 32M sort_buffer_size = 4M join_buffer_size = 4M thread_cache_size = 768 query_cache_size = 0 query_cache_type = 0 interactive_timeout = 600 wait_timeout = 600 tmp_table_size = 32M max_heap_table_size = 32M slow_query_log = 1 slow_query_log_file = /data/mysql/logs/slow.log long_query_time = 0.1 log_queries_not_using_indexes =1 min_examined_row_limit = 100 log_slow_admin_statements = 1 log_slow_slave_statements = 1 server-id = 3306 log-bin = /data/mysql/logs/mybinlog sync_binlog = 1 binlog_cache_size = 4M max_binlog_cache_size = 2G max_binlog_size = 1G expire_logs_days = 30 log_slave_updates binlog_format = row binlog_checksum = 1 relay_log_recovery = 1 relay-log-purge = 1 key_buffer_size = 32M read_buffer_size = 8M read_rnd_buffer_size = 4M bulk_insert_buffer_size = 64M myisam_sort_buffer_size = 128M myisam_max_sort_file_size = 10G myisam_repair_threads = 1 lock_wait_timeout = 3600 innodb_thread_concurrency = 0 innodb_sync_spin_loops = 100 innodb_spin_wait_delay = 30 transaction_isolation = REPEATABLE-READ #innodb_additional_mem_pool_size = 16M innodb_buffer_pool_size = 5734M innodb_buffer_pool_instances = 8 innodb_data_file_path = ibdata1:1G:autoextend innodb_flush_log_at_trx_commit = 1 innodb_log_buffer_size = 32M innodb_log_file_size = 2G innodb_log_files_in_group = 2 # 根據您的服務器IOPS能力適當調整 # 一般配普通SSD盤的話,可以調整到 10000 - 20000 # 配置高端PCIe SSD卡的話,則可以調整的更高,比如 50000 - 80000 innodb_io_capacity = 8000 innodb_write_io_threads = 8 innodb_read_io_threads = 8 innodb_purge_threads = 1 innodb_open_files = 65535 innodb_max_dirty_pages_pct = 50 innodb_flush_method = O_DIRECT #innodb_checksums = 1 #innodb_file_format = Barracuda #innodb_file_format_max = Barracuda innodb_lock_wait_timeout = 10 innodb_rollback_on_timeout = 1 innodb_print_all_deadlocks = 1 innodb_file_per_table = 1 innodb_stats_on_metadata = 0 symbolic-links=0 innodb_status_file = 1 #performance_schema performance_schema = 1 # 字符集設定utf8mb4 character-set-server=utf8mb4 collation-server=utf8mb4_unicode_ci init-connect='SET NAMES utf8mb4' # 優化 optimizer_switch = "mrr=on,mrr_cost_based=on,mrr_sort_keys=on" deadlock_search_depth_short = 3 deadlock_search_depth_long = 10 deadlock_timeout_long = 10000000 deadlock_timeout_short = 5000 slave-parallel-threads=8 # gtid gtid_strict_mode=1 wsrep_gtid_mode=1 [galera] wsrep_on=ON wsrep_provider=/usr/lib64/galera-4/libgalera_smm.so wsrep_cluster_address="gcomm://192.168.142.136,192.168.142.137,192.168.142.138" wsrep_node_name=db138 binlog_format=row default_storage_engine=InnoDB innodb_autoinc_lock_mode=2 bind-address=0.0.0.0 wsrep_cluster_name="MariaDB_Cluster" wsrep_node_address=192.168.142.138 wsrep_sst_method=rsync wsrep_slave_threads=1 innodb_flush_log_at_trx_commit=2 innodb_buffer_pool_size=10240M wsrep_sst_auth=bakuser:Mqh7pFCTLqaV [mysqld_safe] nice=-19 open-files-limit=65535 [mysqldump] quick max_allowed_packet = 64M EOF SERVIER_ID=`date +%S` sed -i "s/server-id = 3306/server-id = 3306"${SERVIER_ID}"/" /etc/my.cnf mkdir -p /data/mysql/{data,logs} chown mysql.mysql -R /data/mysql chown mysql.mysql -R /usr/local/mariadb-10.5.0-linux-systemd-x86_64 chown mysql.mysql -R /usr/local/mysql
# 第二節點、第三節點不需要初始化
# 啟動該節點
/usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf &
十二、登錄驗證同步

# 測試驗證同步 [root@db136 mysql]# mysql -e "show databases" +--------------------+ | Database | +--------------------+ | db136 | | db137 | | db138 | | information_schema | | mysql | | performance_schema | +--------------------+ [root@db136 mysql]# [root@db137 mysql]# mysql -e "show databases" +--------------------+ | Database | +--------------------+ | db136 | | db137 | | db138 | | information_schema | | mysql | | performance_schema | +--------------------+ [root@db137 mysql]# [root@db138 mysql]# mysql -e "show databases" +--------------------+ | Database | +--------------------+ | db136 | | db137 | | db138 | | information_schema | | mysql | | performance_schema | +--------------------+ [root@db138 mysql]# # 刪除庫操作 [root@db138 mysql]# mysql -e "show databases" +--------------------+ | Database | +--------------------+ | db136 | | db137 | | information_schema | | mysql | | performance_schema | +--------------------+ [root@db138 mysql]# [root@db137 mysql]# mysql -e "show databases" +--------------------+ | Database | +--------------------+ | db136 | | db137 | | information_schema | | mysql | | performance_schema | +--------------------+ [root@db137 mysql]# [root@db136 mysql]# mysql -e "show databases" +--------------------+ | Database | +--------------------+ | db136 | | db137 | | information_schema | | mysql | | performance_schema | +--------------------+ [root@db136 mysql]# # 導入表測試 [root@db136 mysql]# mysql db136 < /opt/1.sql [root@db137 mysql]# mysql -e "use db136; show tables;" +-----------------+ | Tables_in_db136 | +-----------------+ | t1 | +-----------------+ [root@db137 mysql]# [root@db137 mysql]# mysql -e "use db136; select * from t1 where id=10;" +----+---------+---------+------------+---------+--------+----------+--------+ | id | pay_min | pay_max | grade_type | subject | period | discount | price | +----+---------+---------+------------+---------+--------+----------+--------+ | 10 | 21 | 59 | 2 | 0 | 0 | 90 | 205.00 | +----+---------+---------+------------+---------+--------+----------+--------+ [root@db137 mysql]# [root@db137 mysql]# mysql -e "use db136; delete from t1 where id=10;" [root@db138 mysql]# mysql -e "use db136; select * from t1 where id=10;"
十三、常用命令收錄
# 這里應該顯示集群里有3個節點 [root@db137 mysql]# mysql -e "show status like 'wsrep_cluster_size'" +--------------------+-------+ | Variable_name | Value | +--------------------+-------+ | wsrep_cluster_size | 3 | +--------------------+-------+ # 這里應該顯示ON [root@db137 mysql]# mysql -e "show status like 'wsrep_connected'" +-----------------+-------+ | Variable_name | Value | +-----------------+-------+ | wsrep_connected | ON | +-----------------+-------+ # 這里應該顯示 AUTO [root@db137 mysql]# mysql -e "show status like 'wsrep_incoming_addresses'" +--------------------------+----------------+ | Variable_name | Value | +--------------------------+----------------+ | wsrep_incoming_addresses | AUTO,AUTO,AUTO | +--------------------------+----------------+ # 這里節點的同步狀態 [root@db137 mysql]# mysql -e "show status like 'wsrep_local_state_comment'" +---------------------------+--------+ | Variable_name | Value | +---------------------------+--------+ | wsrep_local_state_comment | Synced | +---------------------------+--------+ [root@db137 mysql]#
十四、附錄:斷電異常處理

異常處理:當機房突然停電,所有galera主機都非正常關機,來電后開機,會導致galera集群服務無法正常啟動。如何處理? 第1步:開啟galera集群的群主主機的mariadb服務。 第2步:開啟galera集群的成員主機的mariadb服務。 異常處理:galera集群的群主主機和成員主機的mysql服務無法啟動,如何處理? 解決方法一: 第1步、刪除garlera群主主機的/data/mysql/data/grastate.dat狀態文件 /bin/galera_new_cluster啟動服務。啟動正常。登錄並查看wsrep狀態。 第2步:刪除galera成員主機中的/data/mysql/data/grastate.dat狀態文件 systemctl restart mariadb重啟服務。啟動正常。登錄並查看wsrep狀態。 解決方法二: 第1步、修改garlera群主主機的/data/mysql/data/grastate.dat狀態文件中的0為1 /bin/galera_new_cluster啟動服務。啟動正常。登錄並查看wsrep狀態。 第2步:修改galera成員主機中的/data/mysql/data/grastate.dat狀態文件中的0為1 重啟服務。啟動正常。登錄並查看wsrep狀態。 # find / -name grastate.dat
cat >>~/.bashrc <<"EOF" # .bashrc # Source global definitions if [ -f /etc/bashrc ]; then . /etc/bashrc fi # Uncomment the following line if you don't like systemctl's auto-paging feature: # export SYSTEMD_PAGER= # User specific aliases and functions alias mysql.galera_start="/usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf --wsrep-new-cluster &" alias mysql.start="/usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf &" EOF source ~/.bashrc 2020-01-03 10:20:37 0 [Note] WSREP: (96c2caea, 'tcp://0.0.0.0:4567') turning message relay requesting off 2020-01-03 10:20:53 0 [ERROR] WSREP: failed to open gcomm backend connection: 110: failed to reach primary view: 110 (Connection timed out) at gcomm/src/pc.cpp:connect():158 2020-01-03 10:20:53 0 [ERROR] WSREP: gcs/src/gcs_core.cpp:gcs_core_open():220: Failed to open backend connection: -110 (Connection timed out) 2020-01-03 10:20:53 0 [ERROR] WSREP: gcs/src/gcs.cpp:gcs_open():1608: Failed to open channel 'MariaDB_Cluster' at 'gcomm://192.168.142.136,192.168.142.137,192.168.142.138': -110 (Connection timed out) 2020-01-03 10:20:53 0 [ERROR] WSREP: gcs connect failed: Connection timed out 2020-01-03 10:20:53 0 [ERROR] WSREP: wsrep::connect(gcomm://192.168.142.136,192.168.142.137,192.168.142.138) failed: 7 2020-01-03 10:20:53 0 [ERROR] Aborting #虛擬機關閉后,啟動失敗 # 第一台: rm -rf /data/mysql/data/galera.cache rm -rf /data/mysql/data/grastate.dat /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf --wsrep-new-cluster & # 第二台: rm -rf /data/mysql/data/galera.cache rm -rf /data/mysql/data/grastate.dat /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf & # 第三台: rm -rf /data/mysql/data/galera.cache rm -rf /data/mysql/data/grastate.dat /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf &
十五、其他

# 模擬故障 [root@db138 mysql]# mysql Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 20 Server version: 10.5.0-MariaDB-log MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. root@MariaDB0 16:19: [(none)]> shutdown; Query OK, 0 rows affected (0.001 sec) root@MariaDB0 16:19: [(none)]> exit Bye [root@db138 mysql]# # 查看日志 [root@db137 ~]# tail -f /data/mysql/logs/mysql-error.log ...... ...... 2020-01-02 16:19:14 1 [Note] WSREP: wsrep_notify_cmd is not defined, skipping notification. 2020-01-02 16:19:20 0 [Note] WSREP: cleaning up 0a38be73 (tcp://192.168.142.138:4567) # 啟動這個節點 [root@db138 mysql]# /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf & [1] 15714 # 可看到直接恢復正常 [root@db137 ~]# tail -f /data/mysql/logs/mysql-error.log ...... ...... 2020-01-02 16:20:59 0 [Note] WSREP: 1.0 (db138): State transfer from 0.0 (db137) complete. 2020-01-02 16:20:59 0 [Note] WSREP: Member 1.0 (db138) synced with group. [root@db136 mysql]# mysql -e "show status like '%wsrep_cluster_size%';" +--------------------+-------+ | Variable_name | Value | +--------------------+-------+ | wsrep_cluster_size | 3 | +--------------------+-------+ [root@db136 mysql]# # 關機測試 [root@db138 mysql]# shutdown [root@db137 ~]# tail -f /data/mysql/logs/mysql-error.log ...... ...... 2020-01-02 16:23:01 1 [Note] WSREP: wsrep_notify_cmd is not defined, skipping notification. 2020-01-02 16:23:06 0 [Note] WSREP: cleaning up c83f63d6 (tcp://192.168.142.138:4567) [root@db136 mysql]# mysql -e "show status like '%wsrep_cluster_size%';" +--------------------+-------+ | Variable_name | Value | +--------------------+-------+ | wsrep_cluster_size | 2 | +--------------------+-------+ [root@db136 mysql]# # 啟動故障機器 [root@db138 ~]# /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf & [1] 6913 # 由於是測試環境,期間未有數據變化操作,啟動服務后,發現集群還是自我恢復了。 # 再次,非常感謝創始人,為我等提供了偉大的產品。也讓自己前行在自己喜歡的道路上。 [root@db136 mysql]# tail -f /data/mysql/logs/mysql-error.log View: id: d0943aae-2d2a-11ea-9103-4f3bbdb0b32e:29 status: primary protocol_version: 4 capabilities: MULTI-MASTER, CERTIFICATION, PARALLEL_APPLYING, REPLAY, ISOLATION, PAUSE, CAUSAL_READ, INCREMENTAL_WS, UNORDERED, PREORDERED, STREAMING, NBO final: no own_index: 2 members(3): 0: 9974d728-2d39-11ea-b3dc-fa279d9d7c6e, db138 1: c5ad2abf-2d2b-11ea-86c5-e3ff80386683, db137 2: d092d0cc-2d2a-11ea-b2ec-57ab1554759e, db136 ================================================= 2020-01-02 16:26:43 1 [Note] WSREP: wsrep_notify_cmd is not defined, skipping notification. 2020-01-02 16:26:44 0 [Note] WSREP: Member 0.0 (db138) requested state transfer from '*any*'. Selected 1.0 (db137)(SYNCED) as donor. 2020-01-02 16:26:45 0 [Note] WSREP: 1.0 (db137): State transfer to 0.0 (db138) complete. 2020-01-02 16:26:45 0 [Note] WSREP: Member 1.0 (db137) synced with group. 2020-01-02 16:26:45 0 [Note] WSREP: (d092d0cc, 'tcp://0.0.0.0:4567') turning message relay requesting off 2020-01-02 16:26:50 0 [Note] WSREP: 0.0 (db138): State transfer from 1.0 (db137) complete. 2020-01-02 16:26:50 0 [Note] WSREP: Member 0.0 (db138) synced with group. # 另外,在本文檔記錄的環境下,如果系統重新安裝或者其他嚴重異常后,需要重新加入集群中。 rm -rf /data/mysql/data/* 把文件給刪除了。 那么把這個也刪掉: rm -rf /data/mysql/logs/* 然后啟動該節點即可。