yum安装mariadb-galera同步

时间:2022-09-19 20:21:00

     节点             ip地址      hostname                            系统版本   程序版本

node1   10.4.90.90   mysql1    db1       centos6.8   MariaDB10.0.21
node2   10.4.90.91   mysql2    db2       centos6.8   MariaDB10.0.21
node3   10.4.90.92   mysql3    db3       centos6.8   MariaDB10.0.21

停止防火墙

service iptables stop
chkconfig --level 2345 iptables off

关闭selinux

echo -e "#SELINUX=enforcing\n#SELINUXTYPE=targeted\nSELINUX=disabled\nSETLOCALDEFS=0" > /etc/selinux/config

1、Node1 Node2 Node3节点上安装ntpdate设置定时时间同步和ssh互相
安装ntp

yum install ntpdate 

2.在crontab里面加入时钟同步

crontab -e

*/5 * * * * /usr/sbin/ntpdate pool.ntp.org >/dev/null 2>&1

/etc/init.d/crond restart

3.vim /etc/hosts

10.4.90.90    db1 mysql1
10.4.90.91    db2 mysql2
10.4.90.92    db3 mysql3

4.下面是在Node1 Node2 Node3节点上执行,让本机的公钥放到其他节点上

ssh-keygen  -t rsa -f ~/.ssh/id_rsa  -P ''
awk '{if ($0!~/'"$(hostname)"'|localhost/)print $NF}' /etc/hosts |xargs -i ssh-copy-id -i ~/.ssh/id_rsa.pub root@{}

5.在Node1 Node2 Node3节点上安装基础的编译环境

rm -rf /etc/my.cnf
rm -rf /etc/init.d/mysql*
rm -rf /etc/init.d/php*
rm -rf /etc/init.d/http*
rm -rf /etc/php.ini

yum -y remove httpd
yum -y remove http
yum -y remove php
yum -y remove mysql
yum -y remove mysqld
yum -y remove  mariadb
yum -y remove mysql-libs

yum -y install  readline* gcc* xmlto kernel-devel yum* nc nmap perl  screen vim* libaio psmisc wget lrzsz rsync perl-version pcre-devel perl-libs perl-Time-HiRes libtidy libtidy-devel perl-Pod-Simple iptraf sysstat libevent libevent-devel perl-Pod-Escapes perl-Module-Pluggable perl-DBI perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker tar rrdtool ntp* dos2unix setup* net-snmp-utils net-snmp-devel ruby ruby-devel php-snmp cmake net-snmp-perl net-snmp net-snmp-libs wget elinks make autoconf213 m4 gcc gcc-c++ automake autoconf fontconfig fontconfig-devel libjpeg libjpeg* libjpeg-devel libpng libpng-devel freetype freetype* freetype-devel libxml2 libxml2-devel libdhash libdhash-devel  libxslt* zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2* libc-client libc-client-devel libicu-devel ncurses ncurses-devel xmlrpc-c xmlrpc-c-devel curl curl-devel e2fsprogs e2fsprogs-devel  krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel openldap-clients openldap-servers gd gd-devel pam* gettext gettext-devel  keyutils  compat* mpfr cpp libgomp libstdc++-devel ppl cloog-ppl keyutils-libs-devel libcom_err-devel libsepol-devel libXpm*  php-gd ncurses* libtool*  patch unzip bison-devel bison libcom_err-devel libsepol-devel gtk* libselinux-devel  libtiff*  php-common policycoreutils telnet t1lib* nasm* git redhat-lsb-core lsof man net-tools strace sudo system-config-network-tui tcpdump zip bind-utils

6.在Node1 Node2 Node3节点上编译安装MariaDB Galera

rpm地址http://yum.mariadb.org/10.0.21/centos6-amd64/rpms/

cd /etc/yum.repos.d/
mkdir bak
mv *.repo bak
cp bak/CentOS-Base.repo .

vim db.repo
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.0.21/centos6-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
enabled=1

-----------
我用的是和原始上产一样的版本 大家可以选址10.x其他的版本

yum clean all
yum makecache

yum install socat-1.7.2.4-1.el6.rf.x86_64.rpm 

yum install MariaDB-Galera-server MariaDB-client galera perl-DBD-MySQL

7.设置mysql密码

/etc/init.d/mysql start
mysqladmin -u root password 回车输入2次新密码

8.创建用于同步数据库的SST帐号

mysql -u root -p
mysql> GRANT ALL PRIVILEGES ON *.* TO 'cluster'@'%' IDENTIFIED BY 'lookback' WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES;
mysql> quit
###验证通过之后停止MariaDB
/etc/init.d/mysql stop

9.第一次启动主节点,也就是就是node1 需要--wsrep-new-cluster参数,新节点加入有改写配置文件在启动就不需要加入这个参数了

/etc/init.d/mysql start --wsrep-new-cluster (记得node2启动加入集群后 才能修改配置文件的wsrep_cluster_address)

备注:node节点加入后用SHOW STATUS LIKE 'wsrep%'; 查询就能看到节点的存在

10.配置/etc/my.cnf.d/server.cnf

vim /etc/my.cnf.d/server.cnf

[mariadb-10.0]
wsrep_on=ON  #开启wsrep服务
binlog_format=ROW #日志格式必须为ROW
max_connections=128
default-storage-engine=innodb  #默认引擎
innodb_autoinc_lock_mode=2 #指定innodb自增长列锁模式,2为交叉锁模式,多个语句能同时执行
innodb_locks_unsafe_for_binlog=1
query_cache_size=2048M
query_cache_type=1
table_cache=1024M
innodb_additional_mem_pool_size=8M
innodb_flush_log_at_trx_commit=0
innodb_log_buffer_size=4M
innodb_thread_concurrency=16
key_buffer_size=128M
tmp_table_size=128M
read_buffer_size=4M
read_rnd_buffer_size=16M
sort_buffer_size=32M
max_connections=1024
bind-address=0.0.0.0 #绑定地址
datadir=/data/mysql #mysql的data存放地址
innodb_log_file_size=100M
innodb_file_per_table
innodb_flush_log_at_trx_commit=2

wsrep_provider=/usr/lib64/galera/libgalera_smm.so #定Galera库的路径和文件名
###这里需要注意,如果集群是第一次启动请用当前配置,等其他节点启动起来后将下面两行的注释对换重启本节点就OK
wsrep_cluster_address="gcomm://"
#wsrep_cluster_address="gcomm://10.4.90.90,10.4.90.91"   #协议地址来访问集群中的节点,默认端口 4567 mariadb-galera启动后可以查看4567是否启动
wsrep_cluster_name='cluster1' #为使用中的集群指定名称
wsrep_node_address='10.4.90.90' #同步节点的地址 每个节点配置为自己的IP
wsrep_node_name='db1' #同步节点的名字 写本机节点名字
wsrep_sst_method=rsync #指定SST方式,支持RSYNC(最快,需要表锁)一种执行初始状态传输的方法。如果不指定的话,默认值是mysqldump,推荐设置为rsync或xtrabackup。
wsrep_sst_auth=cluster:lookback #同步时候用的用户和密码