+----------------------+ +------------+ +------------+
+-- | keepalived - HaProxy | -+ +-> | MyCat | -+ +-> | PXC集群 |
+------------+ | +----------------------+ | | +------------+ | | +------------+
| VIP | <--| |--| |--|
+------------+ | +----------------------+ | | +------------+ | | +------------+
+-- | keepalived - HaProxy | -+ +-> | MyCat | -+ +-> | PXC集群 |
+----------------------+ +------------+ +------------+
一、升级 OpenSSL
-
查看当前OpenSSL版本
> openssl version -a # 如果版本不为1.0.1u,需要升级。
-
创建两个文件夹
> mkdir openssl_new # 用于需要更新的版本 > mkdir openssl_old # 用于备份老的OpenSSL
-
下载OpenSSL
> wget https://www.openssl.org/source/old/1.0.1/openssl-1.0.1u.tar.gz # 或者下载到本地上传 > scp openssl-1.0.1u.tar.gz root@192.168.43.103:/root/openssl_new
-
备份旧版本
> mv /usr/bin/openssl /root/openssl_old # 备份旧版本 > mv /etc/ssl/ /root/openssl_old # 备份旧版本配置文件
-
卸载旧版本
# 删除旧版本SSL > rpm -qa |grep openssl # 查看安装信息 > rpm -q openssl # 查看版本信息 > rpm -e --nodeps **** # 不要删除lib开头的库,可能会导致系统问题
-
安装OpenSSL - 方法一
> cd /root/openssl_new > tar -xvf openssl-1.0.1u.tar.gz # 解压 > cd openssl-1.0.1u > ./config --prefix=/usr/local/openssl --shared # 注意是否报错 > make > make test > make install > echo /usr/local/openssl/lib >> /etc/ld.so.conf # 配置库文件搜索路径 > ldconfig # 加载新模块 > /sbin/ldconfig -v # 查看新加载模块 #设置软链接 > ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl > ln -s /usr/local/openssl/include/openssl /usr/include/openssl > ln -s /usr/local/openssl/lib/libssl.so.1.0.0 /usr/lib64/libssl.so > ln -s /usr/local/openssl/lib/libssl.so.1.0.0 /usr/lib64/libssl.so.10 > ln -s /usr/local/openssl/lib/libcrypto.so.1.0.0 /usr/lib64/libcrypto.so.10 > openssl version # 查看是否完成升级
-
安装OpenSSL - 方法二
> cd /root/openssl_new > tar -xvf openssl-1.0.1u.tar.gz # 解压 > cd openssl-1.0.1u # 安装在/usr目录下,会覆盖旧版本数据 > ./config --prefix=/usr --shared # 注意是否报错 > make > make test > make install #设置软链接 > ln -s /usr/lib64/libssl.so.1.0.0 /usr/lib64/libssl.so.10 > ln -s /usr/lib64/libcrypto.so.1.0.0 /usr/lib64/libcrypto.so.10 > openssl version # 查看是否完成升级
二、 安装依赖库
-
安装 ncurses
# 下载地址 > wget http://ftp.gnu.org/pub/gnu/ncurses/ncurses-5.6.tar.gz > tar -xzvf ncurses-5.6.tar.gz -C /usr/local/ > cd /usr/local/ncurses-5.6 > ./configure --with-share --prefix=/usr > make > make install
-
安装 libreadline 6
# 下载地址 > wget http://ftp.gnu.org/pub/gnu/readline/readline-6.0.tar.gz > tar -xzvf readline-6.0.tar.gz -C /usr/local/ > cd /usr/local/readline-6.0 > ./configure --prefix=/usr --with-ncurses > vi shlib/Makefile # 将"SHLIB_LIBS = "改为 "SHLIB_LIBS = -lncurses" > make > make install > ln -s /usr/lib/libreadline.so.6.0 /usr/lib64/libreadline.so.6
-
安装 socat
# 下载地址 > wget http://www.dest-unreach.org/socat/download/socat-1.7.3.2.tar.gz > tar -xzvf socat-1.7.3.2.tar.gz -C /usr/local/ > cd /usr/local/socat-1.7.3.2 > ./configure > make > make install
三、安装Percona XtraDB Cluster 5.7
-
下载 Percona XtraDB Cluster 5.7
> wget https://www.percona.com/downloads/Percona-XtraDB-Cluster-LATEST/Percona-XtraDB-Cluster-5.7.19-29.22-3/binary/tarball/Percona-XtraDB-Cluster-5.7.19-rel17-29.22.3.Linux.x86_64.ssl101.tar.gz # 或者下载到本地上传 > scp Percona-XtraDB-Cluster-5.7.19-rel17-29.22.3.Linux.x86_64.ssl101.tar.gz root@192.168.43.103:/pxc
-
配置PXC路径
> tar -xvf Percona-XtraDB-Cluster-5.7.19-rel17-29.22.3.Linux.x86_64.ssl101.tar.gz # 解压 > ln -s /pxc/Percona-XtraDB-Cluster-5.7.19-rel17-29.22.3.Linux.x86_64.ssl101 /usr/local/mysql # 创建软连接 > echo "PATH=$PATH:/usr/local/mysql/bin"|cat >>/etc/profile > source /etc/profile
-
添加用户和用户组
> groupadd mysql > useradd -g mysql mysql > mkdir -p /data/mysql/{data,logs,tmp,var} > chown mysql:mysql /data -R # 修改所有者 > su mysql > chmod -R 755 /data # 修改读写权限
-
创建配置文件
> sudo vi /etc/my.cnf # 默认地址
[client] socket=/data/mysql/tmp/mysql.sock [mysqld] server-id=1 basedir=/usr/local/mysql datadir=/data/mysql/data log-error=/data/mysql/logs/error.log pid-file=/data/mysql/var/mysql.pid tmpdir=/data/mysql/tmp
-
初始化MySQL
# mysqld --initialize --basedir=/usr/local/mysql --datadir=/data/mysql/data --log-error=/data/mysql/logs/error.log # mysqld --defaults-file=/etc/my.cnf --initialize > mysqld --initialize
-
查看MySQL初始密码
> cat /data/mysql/logs/error.log | grep "A temporary password"
-
修改初始密码
# 启动mysl > mysqld & > ln -s /tmp/mysql.sock /data/mysql/tmp/mysql.sock # 登录mysql > mysql -u root -p > set password='Abc_123456' > flush privileges
-
创建管理用户
> CREATE USER 'admin'@'%' IDENTIFIED BY 'Abc_123456'; > GRANT all privileges ON *.* TO 'admin'@'%'; > FLUSH PRIVILEGES;
-
关闭MySQL
> mysqladmin shutdown -u root -p
四、安装percona-xtrabackup
-
下载percona-xtrabackup
> wget https://www.percona.com/downloads/XtraBackup/Percona-XtraBackup-2.4.11/binary/tarball/percona-xtrabackup-2.4.11-Linux-x86_64.libgcrypt11.tar.gz
-
安装
> tar -xvf percona-xtrabackup-2.4.11-Linux-x86_64.libgcrypt11.tar.gz > ln -s /pxc/percona-xtrabackup-2.4.11-Linux-x86_64 /usr/local/xtrabackup > echo "PATH=$PATH:/usr/local/xtrabackup/bin"|cat >>/etc/profile > source /etc/profile
五、配置PXC集群
-
修改/etc/my.cnf
> vi /etc/my.cnf
[client] socket=/data/mysql/tmp/mysql.sock [mysqld] server-id=1 basedir=/usr/local/mysql datadir=/data/mysql/data log-error=/data/mysql/logs/error.log pid-file=/data/mysql/var/mysql.pid tmpdir=/data/mysql/tmp log-bin character_set_server = utf8 bind-address = 0.0.0.0 #跳过DNS解析 skip-name-resolve # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 wsrep_provider=/usr/local/mysql/lib/libgalera_smm.so wsrep_cluster_name=pxc-cluster #PXC集群的名称 wsrep_cluster_address=gcomm://192.168.43.31,192.168.43.190,192.168.43.103 wsrep_node_name=pxc3_1 #当前节点的名称 wsrep_node_address=192.168.43.31 #当前节点的IP wsrep_sst_method=xtrabackup-v2 #同步方法(mysqldump、rsync、xtrabackup) wsrep_sst_auth= admin:Abc_123456 #同步使用的帐户 pxc_strict_mode=ENFORCING #同步严厉模式 binlog_format=ROW #基于ROW复制(安全可靠) default_storage_engine=InnoDB #默认引擎 innodb_autoinc_lock_mode=2 #主键自增长不锁表
-
启动PXC节点
# 启动主节点 > mysqld --wsrep-new-cluster& # 启动两个子节点 > mysqld &
-
PXC测试集群搭建
# 依照上面配置搭建两个集群 # 集群-1 : 192.168.43.31 192.168.43.190 192.168.43.103 # 集群-2 : 192.168.43.249 192.168.43.127 192.168.43.62
# 登录MySQL,创建测试库,测试表 CREATE DATABASE test; CREATE TABLE `t_user` ( `id` bigint(20) NOT NULL COMMENT 'ID', `account` varchar(255) NOT NULL DEFAULT '' COMMENT '账号', `password` varchar(255) NOT NULL DEFAULT '' COMMENT '密码', `m_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间', `c_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户表';
六、安装MyCat
-
安装JDK
# oracle 官网下载1.8版本, > vi /etc/profile
export JAVA_HOME=/usr/local/java/jdk1.8.0_181 export PATH=$PATH:$JAVA_HOME/bin export CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
> source /etc/profile
-
下载MyCat
> wget http://dl.mycat.io/1.6.5/Mycat-server-1.6.5-release-20180122220033-linux.tar.gz
-
安装MyCat
> tar -xvf Mycat-server-1.6.5-release-20180122220033-linux.tar.gz -C /usr/local/ # 配置环境变量 > vi /etc/profile
export MYCAT_HOME=/usr/local/mycat export PATH=$PATH:$MYCAT_HOME/bin
> source /etc/profile # 创建MyCat用户 > groupadd mycat > useradd -g mycat mycat > chown mycat:mycat /usr/local/mycat/ -R
-
启动MyCat
# 按照机器配置修改配置文件 > sudo vi /usr/local/mycat/conf/wrapper.conf
wrapper.java.additional.5=-XX:MaxDirectMemorySize=512M wrapper.java.additional.10=-Xmx512M wrapper.java.additional.11=-Xms256M
> mycat start # 启动MyCat > mysql -u root -p123456 -h192.168.43.231 -P8066 # 测试连接是否成功,切换到安装了MySQL客户端的机器
-
修改 server.xml
> vi /usr/local/mycat/conf/server.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mycat:server SYSTEM "server.dtd"> <mycat:server xmlns:mycat="http://io.mycat/"> <system> <property name="nonePasswordLogin">0</property> <property name="useHandshakeV10">1</property> <property name="useSqlStat">0</property> <property name="useGlobleTableCheck">0</property> <property name="sequnceHandlerType">2</property> <property name="subqueryRelationshipCheck">false</property> <property name="processorBufferPoolType">0</property> <property name="handleDistributedTransactions">0</property> <property name="useOffHeapForMerge">1</property> <property name="memoryPageSize">64k</property> <property name="spillsFileBufferSize">1k</property> <property name="useStreamOutput">0</property> <property name="systemReserveMemorySize">384m</property> <property name="useZKSwitch">false</property> </system> <!--这里是设置的admin用户和虚拟逻辑库--> <user name="admin" defaultAccount="true"> <property name="password">Abc_123456</property> <property name="schemas">test</property> </user> </mycat:server>
-
修改 schema.xml
> vi /usr/local/mycat/conf/schema.xml
<?xml version="1.0"?> <!DOCTYPE mycat:schema SYSTEM "schema.dtd"> <mycat:schema xmlns:mycat="http://io.mycat/"> <!--配置数据表--> <schema name="test" checkSQLschema="false" sqlMaxLimit="100"> <table name="t_user" dataNode="dn1,dn2" rule="mod-long" /> </schema> <!--配置分片关系--> <dataNode name="dn1" dataHost="cluster1" database="test" /> <dataNode name="dn2" dataHost="cluster2" database="test" /> <!--配置连接信息--> <dataHost name="cluster1" maxCon="1000" minCon="10" balance="2" writeType="1" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100"> <heartbeat>select user()</heartbeat> <writeHost host="W1" url="192.168.43.31:3306" user="admin" password="Abc_123456"> <readHost host="W1R1" url="192.168.43.190:3306" user="admin" password="Abc_123456" /> <readHost host="W1R2" url="192.168.43.103:3306" user="admin" password="Abc_123456" /> </writeHost> </dataHost> <dataHost name="cluster2" maxCon="1000" minCon="10" balance="2" writeType="1" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100"> <heartbeat>select user()</heartbeat> <writeHost host="W1" url="192.168.43.249:3306" user="admin" password="Abc_123456"> <readHost host="W1R1" url="192.168.43.127:3306" user="admin" password="Abc_123456" /> <readHost host="W1R2" url="192.168.43.62:3306" user="admin" password="Abc_123456" /> </writeHost> </dataHost> </mycat:schema>
-
修改 rule.xml
> vi /usr/local/mycat/conf/rule.xml
<function name="mod-long" class="io.mycat.route.function.PartitionByMod"> <property name="count">2</property> </function>
-
重启MyCat
> mycat restart
六、安装HaProxy
-
安装HaProxy
# 下载 > https://www.haproxy.org/download/1.7/src/haproxy-1.7.9.tar.gz # 解压 > tar -zxvf haproxy-1.6.9.tar.gz > cd haproxy-1.6.9 # 安装 > make TARGET=linux2628 ARCH=x86_64 PREFIX=/usr/local/haproxy > make install PREFIX=/usr/local/haproxy
-
创建配置文件
> vi /etc/haproxy/haproxy.cfg
global log 127.0.0.1 local2 pidfile /usr/local/haproxy/haproxy.pid maxconn 4000 user haproxy group haproxy daemon # turn on stats unix socket stats socket /usr/local/haproxy/stats defaults mode http log global option httplog option dontlognull option http-server-close option forwardfor except 127.0.0.0/8 option redispatch retries 3 timeout http-request 10s timeout queue 1m timeout connect 10s timeout client 1m timeout server 1m timeout http-keep-alive 10s timeout check 10s maxconn 3000 listen admin_stats bind 0.0.0.0:4001 mode http stats uri /dbs stats realm Global\ statistics stats auth admin:abc123456 listen proxy-mysql bind 0.0.0.0:5001 mode tcp balance roundrobin option tcplog #日志格式 server mycat_1 192.168.43.254:8066 check port 8066 maxconn 2000 server mycat_2 192.168.43.231:8066 check port 8066 maxconn 2000
-
启动HaProxy
> /usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
七、安装Keepalived
-
安装Keepalived
# 首先安装OpenSSL > wget http://www.keepalived.org/software/keepalived-1.2.23.tar.gz > ./configure --prefix=/usr/local/keepalived/ > make > make install
-
开启防火墙
> iptables -A INPUT -p vrrp -j ACCEPT
-
新增配置文件
> vi /etc/keepalived/keepalived.conf
vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 123456 } virtual_ipaddress { 192.168.43.199 } }
-
启动keepalived
> cp /usr/local/keepalived/sbin/keepalived /usr/local/sbin/ > keepalived -f /etc/keepalived/keepalived.conf