升级网站运行平台|部署缓存服务|数据迁移|部署集群
- 案例1:升级网站运行平台
- 步骤一:清除当前配置
- 步骤二:部署LNMP
- 步骤三:测试配置
- 案例2:部署内存存储服务
- 步骤一:部署redis服务器(6台都要配置)
- 步骤二:创建redis集群
- 步骤三:配置网站服务器
- 步骤四:测试配置
- 案例3:数据迁移
- 步骤一:配置从服务器(把主机192.168.4.66 配置为192.168.4.11的从服务器)
- 步骤二:配置第1台PXC服务器(192.168.4.66)
- 步骤三:配置第2台PXC服务器(192.168.4.10)
- 步骤四:配置第3台PXC服务器(192.168.4.88)
- 步骤五:公共配置(192.168.4.88、192.168.4.10、192.168.4.66)
- 案例4:部署LB集群
- 步骤一:安装软件: 在haproxy99主机上安装haproxy软件
- 步骤二:修改配置文件
- 步骤三:启动服务
- 步骤四:测试配置:在网站服务器连接haproxy99主机访问数据
- 案例5:部署HA集群
- 步骤一:准备备用调度器主机
- 步骤二:安装软件
- 步骤三:修改配置文件
- 步骤四:启动服务
- 步骤五:测试配置
升级网站运行平台
部署内存存储服务
部署pxc集群
解决数据库服务负载问题
解决调度器单点故障问题
案例1:升级网站运行平台
步骤一:清除当前配置
# 停止网站
[root@web33 ~]# /usr/local/tomcat/bin/shutdown.sh
[root@web33 ~]# vim /etc/rc.local
#/usr/local/tomcat/bin/startup.sh
# 卸载掉web服务器挂载
umount /usr/local/tomcat/webapps/ROOT/
vim /etc/fstab # 注销掉挂载
步骤二:部署LNMP
# 安装软件
[root@web33 ~]# yum -y install gcc zlib-devel pcre-devel //安装源码Nginx依赖软件
[root@web33 ~]# tar -zxvf nginx-1.12.2.tar.gz //解压
[root@web33 ~]# cd nginx-1.12.2 //进源码目录
[root@web33 nginx-1.12.2]# ./configure //配置
[root@web33 nginx-1.12.2]# make //编译
……
……
sed -e "s|%%PREFIX%%|/usr/local/nginx|" \-e "s|%%PID_PATH%%|/usr/local/nginx/logs/nginx.pid|" \-e "s|%%CONF_PATH%%|/usr/local/nginx/conf/nginx.conf|" \-e "s|%%ERROR_LOG_PATH%%|/usr/local/nginx/logs/error.log|" \< man/nginx.8 > objs/nginx.8
make[1]: 离开目录“/root/nginx-1.12.2”
[root@web33 nginx-1.12.2]#
[root@web33 nginx-1.12.2]# make install //安装
[root@web33 nginx-1.12.2]# ls /usr/local/nginx //查看安装目录
conf html logs sbin
[root@web33 ~]# yum -y install php-fpm //安装php-fpm 软件
[root@web33 ~]# yum -y install php php-mysql //安装php 及 php-mysql 软件
挂载共享存储
[root@web33 ~]# vim /etc/fstab //开机挂载
192.168.4.30:/sitedir /usr/local/nginx/html nfs defaults 0 0
:wq
[root@web33 ~]# mount -a //挂载设备
[root@web33 ~]# mount | grep "/usr/local/nginx/html" //查看挂载
启动服务
[root@web33 ~]# vim +65 /usr/local/nginx/conf/nginx.conf //修改主配置文件
location ~ \.php$ {root html;fastcgi_pass 127.0.0.1:9000;fastcgi_index index.php;#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;include fastcgi.conf;}
:wq
[root@web33 ~]# /usr/local/nginx/sbin/nginx //启动服务
[root@web33 ~]# netstat -utnlp | grep :80 //查看端口
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 26335/nginx: master
[root@web33 ~]# systemctl start php-fpm
[root@web33 ~]# netstat -utnlp | grep :9000
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 26345/php-fpm: mast
步骤三:测试配置
编写PHP脚本
[root@nfs30 ~]# vim /sitedir/linkdb2.php //在nfs30共享目录编写php脚本文件
<?php
$conn=mysql_connect("192.168.4.11","yaya99","123qqq...A");
mysql_select_db("gamedb");
$sql = 'insert into user (name) values ("PLJ")';
mysql_query($sql);
mysql_close();
echo "save data ok";
?>
:wq
访问脚本
[root@client50 ~]# curl http://192.168.4.33/linkdb2.php //访问web33服务器
save data ok
查看数据
[root@mysql11 ~]# mysql –uyaya99 -p123qqq...A -e 'select * from gamedb.user where name="PLJ"' //在数据库服务器查看数据
mysql: [Warning] Using a password on the command line interface can be insecure.
+------+
| name |
+------+
| PLJ |
+------+
案例2:部署内存存储服务
Redis 主要负责做网站加速的
具体操作如下:
- 部署redis服务器
- 创建redis集群
- 配置网站服务器
- 测试配置
步骤一:部署redis服务器(6台都要配置)
搭建redis服务器
[root@redisA ~]# rpm -q gcc || yum -y install gcc //安装编译工具
[root@redisA ~]# tar -zxvf redis-4.0.8.tar.gz //解压
[root@redisA ~]# cd redis-4.0.8/ //进源码目录
[root@redisA redis-4.0.8]# make install //安装软件
[root@redisA redis-4.0.8]# ./utils/install_server.sh //初始化配置
[root@redisA redis-4.0.8]# /etc/init.d/redis_6379 stop //停止服务
Stopping ...
Redis stopped
[root@redisA redis-4.0.8]#
[root@redisA redis-4.0.8]# vim /etc/redis/6379.conf //修改配置文件,启用集群配置
70 bind 192.168.4.51
815 cluster-enabled yes
823 cluster-config-file nodes-6379.conf
829 cluster-node-timeout 5000
:wq
[root@redisA redis-4.0.8]# /etc/init.d/redis_6379 start //启动服务
Starting Redis server...
[root@redisA redis-4.0.8]# netstat -utnlp | grep redis-server //查看端口
tcp 0 0 192.168.4.51:6379 0.0.0.0:* LISTEN 29720/redis-server //redis服务端口
tcp 0 0 192.168.4.51:16379 0.0.0.0:* LISTEN 29720/redis-server //集群端口
# 6台主机都要如此操作
步骤二:创建redis集群
配置管理主机
[root@mgm ~]# yum -y install ruby rubygems //安装依赖
[root@mgm ~]# gem install redis-3.2.1.gem //安装依赖软件gem程序
[root@mgm ~]# mkdir /root/bin
[root@mgm ~]# tar -zxvf redis-4.0.8.tar.gz
[root@mgm ~]# cp redis-4.0.8/src/redis-trib.rb /root/bin/ //拷贝脚本
[root@mgm ~]# chmod +x /root/bin/redis-trib.rb //确保脚本有执行权限
[root@mgm ~]# redis-trib.rb help //查看帮助
Usage: redis-trib <command> <options> <arguments ...>
创建集群
]# redis-trib.rb create --replicas 1 \
192.168.4.51:6379 192.168.4.52:6379 192.168.4.53:6379 \ 192.168.4.54:6379 192.168.4.55:6379 192.168.4.56:6379
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.4.51:6379
192.168.4.52:6379
192.168.4.53:6379
Adding replica 192.168.4.56:6379 to 192.168.4.51:6379
Adding replica 192.168.4.55:6379 to 192.168.4.52:6379
Adding replica 192.168.4.54:6379 to 192.168.4.53:6379
M: d9f8fe6d6d9dd391be8e7904501db1535e4d17cb 192.168.4.51:6379slots:0-5460 (5461 slots) master
M: 324e05df3f143ef97e50d09be0328a695e655986 192.168.4.52:6379slots:5461-10922 (5462 slots) master
M: 9e44139cffb8ebd7ed746aabbf4bcea9bf207645 192.168.4.53:6379slots:10923-16383 (5461 slots) master
S: d9634ba0aa5c1a07193da4a013da6051c1515922 192.168.4.54:6379replicates 9e44139cffb8ebd7ed746aabbf4bcea9bf207645
S: 2d343a9df48f6f6e207949e980ef498466a44dad 192.168.4.56:6379replicates d9f8fe6d6d9dd391be8e7904501db1535e4d17cb
S: 894dd0008053f6fb65e9e4a36b755d9351607500 192.168.4.55:6379replicates 324e05df3f143ef97e50d09be0328a695e655986
Can I set the above configuration? (type 'yes' to accept): yes //同意以上配置
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join...
>>> Performing Cluster Check (using node 192.168.4.51:6379)
M: d9f8fe6d6d9dd391be8e7904501db1535e4d17cb 192.168.4.51:6379slots:0-5460 (5461 slots) master1 additional replica(s)
S: d9634ba0aa5c1a07193da4a013da6051c1515922 192.168.4.54:6379slots: (0 slots) slavereplicates 9e44139cffb8ebd7ed746aabbf4bcea9bf207645
S: 894dd0008053f6fb65e9e4a36b755d9351607500 192.168.4.56:6379slots: (0 slots) slavereplicates 324e05df3f143ef97e50d09be0328a695e655986
M: 324e05df3f143ef97e50d09be0328a695e655986 192.168.4.52:6379slots:5461-10922 (5462 slots) master1 additional replica(s)
M: 9e44139cffb8ebd7ed746aabbf4bcea9bf207645 192.168.4.53:6379slots:10923-16383 (5461 slots) master1 additional replica(s)
S: 2d343a9df48f6f6e207949e980ef498466a44dad 192.168.4.57:6379slots: (0 slots) slavereplicates d9f8fe6d6d9dd391be8e7904501db1535e4d17cb
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered. //提示16384个槽分配完毕
查看集群信息
[root@mgm ~]# redis-trib.rb info 192.168.4.51:6379 //查看集群信息
192.168.4.51:6379 (d9f8fe6d...) -> 0 keys | 5461 slots | 1 slaves.
192.168.4.52:6379 (324e05df...) -> 0 keys | 5462 slots | 1 slaves.
192.168.4.53:6379 (9e44139c...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
keys per slot on average
[root@mgm ~]# redis-trib.rb check 192.168.4.51:6379 //检测集群
测试配置(在客户端连接集群中的任意一台服务器存取数据)
[root@client50 ~]# redis-cli -c -h 192.168.4.51 -p 6379 //连接服务器51
192.168.4.51:6379> set x 100 //存储
-> Redirected to slot [16287] located at 192.168.4.53:6379 //提示存储在53主机
OK
192.168.4.53:6379> set z 300 //存储
-> Redirected to slot [8157] located at 192.168.4.52:6379 //提示存储在52主机
192.168.4.52:6379> keys * //在52主机查看数据 只有变量z
1) "z"
192.168.4.52:6379> get x
-> Redirected to slot [16287] located at 192.168.4.53:6379 //连接53主机获取数据
"100"
步骤三:配置网站服务器
[root@web33 ~ ]# yum -y install php-devel //安装依赖
[root@web33 ~]# tar -zxvf redis-cluster-4.3.0.tgz //解压
[root@web33 ~]# cd redis-4.3.0/ //进入源码目录
[root@web33 redis-4.3.0]# phpize //创建configure命令及配置信息文件/usr/bin/php-config
[root@web33 redis-4.3.0]# ./configure --with-php-config=/usr/bin/php-config
……
……
configure: creating ./config.status
config.status: creating config.h
config.status: config.h is unchanged
config.status: executing libtool commands
[root@web33 redis-4.3.0]#
[root@web33 redis-4.3.0]# make //编译
[root@web33 redis-4.3.0]# make install //安装
Installing shared extensions: /usr/lib64/php/modules/ //提示模块安装目录
[root@web33 redis-4.3.0]#
[root@web33 redis-4.3.0]# ls /usr/lib64/php/modules/ //查看目录列表
curl.so fileinfo.so json.so mysqli.so mysql.so pdo_mysql.so pdo.so pdo_sqlite.so phar.so redis.so sqlite3.so zip.so
[root@web33 redis-4.3.0]# vim /etc/php.ini
728 extension_dir = "/usr/lib64/php/modules/" //模块目录
730 extension = "redis.so" //模块名
:wq
[root@web33 redis-4.3.0]# systemctl restart php-fpm //重启php-fpm服务
[root@web33 redis-4.3.0]# php -m | grep -i redis //查看模块
redis
步骤四:测试配置
在存储服务器共享目录下,创建连接集群PHP脚本
nfs30~ ]# vim /sitedir/set_data.php //存储数据脚本
<?php
$redis_list = ['192.168.4.51:6379','192.168.4.52:6379','192.168.4.53:6379','192.168.4.54:6379','192.168.4.55:6379','192.168.4.56:6379']; //定义redis服务器列表
$client = new RedisCluster(NUll,$redis_list); //定义连接redis服务器变量
$client->set("i","tarenaA "); //存储数据 变量名 i
$client->set("j","tarenaB "); //存储数据 变量名 j
$client->set("k","tarenaC "); //存储数据 变量名 k
?>
:wq
nfs30~ ]# vim /sitedir/get_data.php //获取数据脚本
<?php
$redis_list = ['192.168.4.51:6379','192.168.4.52:6379','192.168.4.53:6379','192.168.4.54:6379','192.168.4.55:6379','192.168.4.56:6379']; //定义redis服务器列表
$client = new RedisCluster(NUll,$redis_list); //定义连接redis服务器变量
echo $client->get("i"); //获取变量i 的数据
echo $client->get("j"); //获取变量j 的数据
echo $client->get("k"); //获取变量k 的数据
?>
:wq
nfs30~ ]# vim /sitedir/test3.php //存/取数据脚本
<?php
$redis_list = ['192.168.4.51:6379','192.168.4.52:6379','192.168.4.53:6379','192.168.4.54:6379','192.168.4.55:6379','192.168.4.56:6379'];
$client = new RedisCluster(NUll,$redis_list);
$client->set(“name“,”panglijing”); //存数据
echo $client->get(“name”); //取数据
?>
:wq
访问网站执行脚本(在任意主机访问网站服务器都可以)
]# curl http://192.168.4.33/set_data.php
]# curl http://192.168.4.33/get_data.php
]# curl http://192.168.4.33/test3.php
案例3:数据迁移
要求如下:
- 配置从服务器
- 配置第1台PXC服务器
- 配置第2台PXC服务器
- 配置第3台PXC服务器
- 公共配置
- 测试配置
步骤一:配置从服务器(把主机192.168.4.66 配置为192.168.4.11的从服务器)
在192.168.4.66主机安装数据库服务软件并启动mysqld服务
[root@pxcnode66 ~]# tar -xvf mysql-5.7.17.tar //解包
[root@pxcnode66 ~]# ls *.rpm //查看软件列表
[root@pxcnode66 ~]# yum -y install mysql-community-*.rpm //安装软件
[root@pxcnode66 ~]# systemctl start mysqld //启动服务
[root@pxcnode66 ~]# ls /var/lib/mysql //查看数据库文件列表
auto.cnf client-cert.pem ibdata1 ibtmp1 mysql.sock.lock public_key.pem sys
ca-key.pem client-key.pem ib_logfile0 mysql performance_schema server-cert.pem
ca.pem ib_buffer_pool ib_logfile1 mysql.sock private_key.pem server-key.pem
[root@pxcnode66 ~]# systemctl enable mysqld //设置服务开机运行
[root@pxcnode66 ~]# grep password /var/log/mysqld.log //查看初始密码
2019-07-05T01:56:51.895852Z 1 [Note] A temporary password is generated for root@localhost: bB0*uCmu:.Kj
[root@pxcnode66 ~]# mysql -uroot -p'bB0*uCmu:.Kj' //初始密码登录
mysql> alter user root@"localhost" identified by "123qqq...A";//修改登录密码
[root@pxcnode66 ~]# mysql -uroot -p123qqq...A //新密码登录
mysql> show databases; //查看数据库
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)[root@pxcnode66 ~]# vim /etc/my.cnf
[mysqld]
server_id=66 //指定server_id
:wq
[root@pxcnode66 ~]# systemctl restart mysqld //重启服务
确保数据一致 (pxcnode66主机 使用mysql11主机的完全备份恢复数据确保数据一致 )
[root@mysql11 ~]# rpm -ivh libev-4.15-1.el6.rf.x86_64.rpm //安装依赖软件
[root@mysql11 ~]# yum -y install percona-xtrabackup-24-2.4.7-1.el7.x86_64.rpm //安装在线热备软件
[root@mysql11 ~]# innobackupex --user root --password 123qqq...A --slave-info /allbak --no-timestamp //备份所有数据,并记录备份数据对应的binlog日志名
[root@mysql11 ~]# scp -r /allbak root@192.168.4.66:/root/ //把备份文件发送给pxcnode66主机
[root@pxcnode66 ~]# rpm -ivh libev-4.15-1.el6.rf.x86_64.rpm //安装依赖软件
[root@pxcnode66 ~]# yum -y install percona-xtrabackup-24-2.4.13-1.el7.x86_64.rpm //安装在线热备软件
[root@pxcnode66 ~]# systemctl stop mysqld //停止服务
[root@pxcnode66 ~]# rm -rf /var/lib/mysql/* //清空数据库目录
[root@pxcnode66 ~]# innobackupex --apply-log /root/allbak/ //准备恢复数据
[root@pxcnode66 ~]# innobackupex --copy-back /root/allbak/ //恢复数据
[root@pxcnode66 ~]# chown -R mysql:mysql /var/lib/mysql //修改所有者
[root@pxcnode66 ~]# systemctl start mysqld //启动服务
指定主服务器
[root@pxcnode66 ~]# cat /root/allbak/xtrabackup_info | grep master11 //查binlog日志
binlog_pos = filename 'master11.000001', position '7700'
[root@pxcnode66 ~]# mysql -uroot -p123qqq...A //管理员登录指定主服务器信息
mysql> change master to
master_host="192.168.4.11", //主服务器ip地址
master_user="repluser", //主服务器授权用户
master_password="123qqq...A", //授权密码
master_log_file="master11.000001", //binlog日志名
master_log_pos=7700; //日志偏移量
Query OK, 0 rows affected, 2 warnings (0.31 sec)
mysql> start slave ; //启动slave 程序
Query OK, 0 rows affected (0.09 sec)
mysql> exit //断开连接
Bye
[root@pxcnode66 ~]# mysql -uroot -p123qqq...A -e "show slave status\G" | grep -i 192.168.4.11 //查看主服务器地址
mysql: [Warning] Using a password on the command line interface can be insecure.Master_Host: 192.168.4.11 //主服务器ip地址
[root@pxcnode66 ~]# mysql -uroot -p123qqq...A -e "show slave status\G" | grep -i "yes" //查看状态信息
mysql: [Warning] Using a password on the command line interface can be insecure.Slave_IO_Running: Yes //IO线程正常Slave_SQL_Running: Yes //SQL线程正常
步骤二:配置第1台PXC服务器(192.168.4.66)
停止mysqld服务、卸载mysqld服务软件
[root@pxcnode66 ~]# systemctl stop mysqld //停止服务
[root@pxnode66 ~]# rpm -qa | grep -i mysql //查看安装的MySQL服务软件
[root@pxcnode66 ~]# rpm -e --nodeps mysql-community-server mysql-community-embedded-compat mysql-community-common mysql-community-client mysql-community-devel \
安装PXC软件、修改配置文件、启动mysql服务
[root@pxcnode66 ~]# cd PXC //进软件目录
[root@pxcnode66 PXC]# rpm -ivh qpress-1.1-14.11.x86_64.rpm //安装依赖
警告:qpress-1.1-14.11.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 6cb7b81f: NOKEY
[root@pxcnode66 PXC]# tar -xvf Percona-XtraDB-Cluster-5.7.25-31.35-r463-el7-x86_64-bundle.tar //解压PXC软件包
[root@pxcnode66 PXC]# yum -y install Percona-XtraDB-Cluster-*.rpm //安装软件
[root@pxcnode66 PXC]# vim /etc/percona-xtradb-cluster.conf.d/mysqld.cnf //修改数据库服务配置文件
[mysqld]
server-id=66 //指定server_id
[root@pxcnode66 PXC]# vim /etc/percona-xtradb-cluster.conf.d/wsrep.cnf //修改集群服务配置文件
wsrep_cluster_address=gcomm:// 不需要写ip地址 因为目前没有集群
wsrep_node_address=192.168.4.66 //指定本机Ip地址
wsrep_cluster_name=pxc-cluster //指定集群名称(另外2台的集群名称要于此相同)
wsrep_node_name=pxcnode66 //指定本机主机名
wsrep_sst_auth="sstuser:123qqq...A" //数据全量同步授权用户及密码
:wq
[root@pxcnode66 PXC]# systemctl start mysql //启动服务
[root@pxcnode66 PXC]# netstat -utnlp | grep :3306 //查看MySQL服务端口
tcp6 0 0 :::3306 :::* LISTEN 24482/mysqld
[root@pxcnode66 PXC]# netstat -utnlp | grep :4567 //查看集群通信端口
tcp 0 0 0.0.0.0:4567 0.0.0.0:* LISTEN 24472/mysqld
[root@pxcnode66 PXC]# systemctl enable mysql //设置服务开机运行
数据库管理员登录、用户授权、查看状态信息
[root@pxcnode66 PXC]# mysql -uroot -p123qqq...A //管理员登录
mysql> grant all on *.* to sstuser@"localhost" identified by "123qqq...A"; //用户授权
Query OK, 0 rows affected, 1 warning (0.10 sec)
mysql> show status like "%wsrep%"; //查看集群状态信息
| wsrep_incoming_addresses | 192.168.4.66:3306 |
| wsrep_cluster_weight | 1 |
| wsrep_desync_count | 0 |
| wsrep_evs_delayed | |
| wsrep_evs_evict_list | |
| wsrep_evs_repl_latency | 0/0/0/0/0 |
| wsrep_evs_state | OPERATIONAL |
| wsrep_gcomm_uuid | 73809cc5-cf00-11e9-aac3-b223959fecdf |
| wsrep_cluster_conf_id | 1 |
| wsrep_cluster_size | 1 |
| wsrep_cluster_state_uuid | 73848b1a-cf00-11e9-9058-36c1ac1e1359 |
| wsrep_cluster_status | Primary |
| wsrep_connected | ON |
| wsrep_local_bf_aborts | 0 |
| wsrep_local_index | 0 |
| wsrep_provider_name | Galera |
| wsrep_provider_vendor | Codership Oy <info@codership.com> |
| wsrep_provider_version | 3.35(rddf9876) |
| wsrep_ready | ON |
+----------------------------------+--------------------------------------+
71 rows in set (0.00 sec)
mysql> exit ;
[root@pxcnode66 ~]# mysql -uroot -p123qqq...A -e "show slave status\G" | grep -i "yes" //查看状态信息依然是192.168.4.11的从服务器
mysql: [Warning] Using a password on the command line interface can be insecure.Slave_IO_Running: Yes //IO线程正常Slave_SQL_Running: Yes //SQL线程正常
步骤三:配置第2台PXC服务器(192.168.4.10)
安装PXC软件
[root@pxcnode10 ~]# cd PXC //进软件目录
[root@pxcnode10 PXC]# rpm -ivh qpress-1.1-14.11.x86_64.rpm //安装依赖
警告:qpress-1.1-14.11.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 6cb7b81f: NOKEY
[root@pxcnode10 PXC]# tar -xvf Percona-XtraDB-Cluster-5.7.25-31.35-r463-el7-x86_64-bundle.tar //解压PXC软件包
[root@pxcnode10 PXC]# yum -y install Percona-XtraDB-Cluster-*.rpm //安装软件
修改配置文件
[root@pxcnode10 PXC]# vim /etc/percona-xtradb-cluster.conf.d/mysqld.cnf //修改数据库服务配置
[mysqld]
server-id=10 //指定server_id
:wq
[root@pxcnode10 PXC]#
[root@pxcnode10 PXC]# vim /etc/percona-xtradb-cluster.conf.d/wsrep.cnf //修改集群服务配置文件
wsrep_cluster_address=gcomm://192.168.4.66,192.168.4.10 //集群成员列表
wsrep_node_address=192.168.4.10 //指定本机Ip地址
wsrep_cluster_name=pxc-cluster //指定集群名称(另外2台的集群名称要于此相同)
wsrep_node_name=pxcnode10 //指定本机主机名
wsrep_sst_auth="sstuser:123qqq...A" //数据全量同步授权用户及密码
:wq
启动mysql服务
[root@pxcnode10 PXC]# systemctl start mysql //启动服务
[root@pxcnode10 PXC]# systemctl enable mysql //服务开机运行
[root@pxcnode10 PXC]# netstat -utnlp | grep :3306 //查看MySQL服务端口
tcp6 0 0 :::3306 :::* LISTEN 24482/mysqld
[root@pxcnode10 PXC]# netstat -utnlp | grep :4567 //查看集群端口
tcp6 0 0 :::4567 :::* LISTEN 24489/mysqld
mysql> show status like "%wsrep%"; //查看集群状态信息
| wsrep_incoming_addresses | 192.168.4.66:3306,192.168.4.10:3306|
| wsrep_cluster_weight | 1 |
| wsrep_desync_count | 0 |
| wsrep_evs_delayed | |
| wsrep_evs_evict_list | |
| wsrep_evs_repl_latency | 0/0/0/0/0 |
| wsrep_evs_state | OPERATIONAL |
| wsrep_gcomm_uuid | 73809cc5-cf00-11e9-aac3-b223959fecdf |
| wsrep_cluster_conf_id | 1 |
| wsrep_cluster_size | 1 |
| wsrep_cluster_state_uuid | 73848b1a-cf00-11e9-9058-36c1ac1e1359 |
| wsrep_cluster_status | Primary |
| wsrep_connected | ON |
| wsrep_local_bf_aborts | 0 |
| wsrep_local_index | 0 |
| wsrep_provider_name | Galera |
| wsrep_provider_vendor | Codership Oy <info@codership.com> |
| wsrep_provider_version | 3.35(rddf9876) |
| wsrep_ready | ON |
+----------------------------------+--------------------------------------+
71 rows in set (0.00 sec)
mysql> exit ;
步骤四:配置第3台PXC服务器(192.168.4.88)
[root@pxcnode88 ~]# cd PXC //进软件目录
[root@pxcnode88 PXC]# rpm -ivh qpress-1.1-14.11.x86_64.rpm //安装依赖
警告:qpress-1.1-14.11.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 6cb7b81f: NOKEY
准备中... ################################# [100%]
正在升级/安装...1:qpress-1.1-14.11 ################################# [100%]
[root@pxcnode88 PXC]#
[root@pxcnode88 PXC]# tar -xvf Percona-XtraDB-Cluster-5.7.25-31.35-r463-el7-x86_64-bundle.tar //解压PXC软件包
[root@pxcnode88 PXC]# yum -y install Percona-XtraDB-Cluster-*.rpm //安装软件
[root@pxcnode88 PXC]# vim /etc/percona-xtradb-cluster.conf.d/mysqld.cnf //修改数据库服务配置
[mysqld]
server-id=88 //指定server_id
:wq
[root@pxcnode88 PXC]#
[root@pxcnode88 PXC]# vim /etc/percona-xtradb-cluster.conf.d/wsrep.cnf //修改集群服务配置文件
wsrep_cluster_address=gcomm://192.168.4.66 //集群成员ip地址
wsrep_node_address=192.168.4.88 //指定本机Ip地址
wsrep_cluster_name=pxc-cluster //指定集群名称(另外2台的集群名称要于此相同)
wsrep_node_name=pxcnode88 //指定本机主机名
wsrep_sst_auth="sstuser:123qqq...A" //数据全量同步授权用户及密码
:wq
[root@pxcnode88 PXC]#
[root@pxcnode88 PXC]# systemctl start mysql //启动服务
[root@pxcnode88 PXC]# systemctl enable mysql //服务开机运行
[root@pxcnode88 PXC]# netstat -utnlp | grep :3306 //查看MySQL服务端口
tcp6 0 0 :::3306 :::* LISTEN 24472/mysqld
[root@pxcnode88 PXC]# netstat -utnlp | grep :4567 //查看集群端口
tcp6 0 0 :::4567 :::* LISTEN 24486/mysqld
步骤五:公共配置(192.168.4.88、192.168.4.10、192.168.4.66)
修改192.168.4.88主机的集群配置文件
[root@pxcnode88 ~]# vim /etc/percona-xtradb-cluster.conf.d/wsrep.cnf
wsrep_cluster_address=gcomm://192.168.4.66,192.168.4.10,192.168.4.88 //指定集群成员列表
:wq
修改192.168.4.10主机的集群配置文件
[root@pxcnode10 ~]# vim /etc/percona-xtradb-cluster.conf.d/wsrep.cnf
wsrep_cluster_address=gcomm://192.168.4.66,192.168.4.88,192.168.4.10 //指定集群成员列表
:wq
修改192.168.4.66主机的集群配置文件
[root@pxcnode66 ~]# vim /etc/percona-xtradb-cluster.conf.d/wsrep.cnf
wsrep_cluster_address=gcomm://192.168.4.66,192.168.4.88,192.168.4.10 //指定集群成员列表
:wq
案例4:部署LB集群
配置步骤如下:
- 安装软件
- 修改配置文件
- 启动服务
- 测试配置
拓扑结构如图-3所示。创建1台新的虚拟机,配置ip地址 eth0 192.168.4.99 主机名 haproxy99 ;运行haproxy服务 接受客户端访问数据库的连接请求,把请求平均分发给3台PXC集群主机。
步骤一:安装软件: 在haproxy99主机上安装haproxy软件
[root@haproxy99 ~]# yum -y install haproxy
步骤二:修改配置文件
[root@haproxy99 ~]# vim /etc/haproxy/haproxy.cfg
Global //全局配置默认即可log 127.0.0.1 local2chroot /var/lib/haproxypidfile /var/run/haproxy.pidmaxconn 4000user haproxygroup haproxydaemonstats socket /var/lib/haproxy/stats
defaults //默认配置(不需要修改)mode httplog globaloption httplogoption dontlognulloption http-server-closeoption forwardfor except 127.0.0.0/8option redispatchretries 3timeout http-request 10stimeout queue 1mtimeout connect 10stimeout client 1mtimeout server 1mtimeout http-keep-alive 10stimeout check 10smaxconn 3000
listen status //定义监控页面mode http //模式为httpbind *:80 //端口80stats enable //启用配置stats uri /admin //访问目录名stats auth admin:admin //登录用户与密码
listen mysql_3306 *:3306 //定义haproxy服务名称与端口号mode tcp //mysql服务 得使用 tcp 协议option tcpka //使用长连接 不需要每输入一个指令 就连接一次balance roundrobin //调度算法server mysql_01 192.168.4.66:3306 check //第1台数据库服务器server mysql_02 192.168.4.10:3306 check //第2台数据库服务器server mysql_03 192.168.4.88:3306 check //第3台数据库服务器
:wq
步骤三:启动服务
[root@haproxy99 ~]# systemctl start haproxy //启动服务
[root@haproxy99 ~]# systemctl enable haproxy //开机运行
Created symlink from /etc/systemd/system/multi-user.target.wants/haproxy.service to /usr/lib/systemd/system/haproxy.service.
[root@haproxy99 ~]# netstat -utnlp | grep :3306 //查看端口
tcp6 0 0 :::3306 :::* LISTEN 29768/haproxy
步骤四:测试配置:在网站服务器连接haproxy99主机访问数据
[root@web33 ~]# mysql –h192.168.4.99 –uyaya99 –p123qqq…A -e 'select @@hostname'
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------+
| @@hostname |
+------------+
| pxcnode66 | //第1次连接
+------------+
[root@web33 ~]#
[root@web33 ~]# mysql –h192.168.4.99 –uyaya99 –p123qqq…A -e 'select @@hostname'
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------+
| @@hostname |
+------------+
| pxcnode10 | //第2次连接
+------------+
[root@web33 ~]#
[root@web33 ~]# mysql –h192.168.4.99 –uyaya99 –p123qqq…A -e 'select @@hostname'
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------+
| @@hostname |
+------------+
| pxcnode88 | //第3次连接
+------------+
案例5:部署HA集群
步骤一:准备备用调度器主机
[root@haproxy98 ~]# yum -y install haproxy
# 修改haproxy98主机haproxy.conf文件(直接拷贝haproxy99主机的配置文件也可以)
[root@haproxy98 ~]# scp root@192.168.4.99:/etc/haproxy/haproxy.cfg /etc/haproxy/
Warning: Permanently added '192.168.4.99' (ECDSA) to the list of known hosts.
root@192.168.4.99's password: //输入haproxy99主机的密码
haproxy.cfg # 启动haproxy服务
[root@haproxy98 ~]# systemctl start haproxy //启动服务
[root@haproxy98 ~]# systemctl enable haproxy //服务开机运行
Created symlink from /etc/systemd/system/multi-user.target.wants/haproxy.service to /usr/lib/systemd/system/haproxy.service.
[root@haproxy98 ~]# netstat -utnlp | grep :3306 //查看端口
tcp6 0 0 :::3306 :::* LISTEN 29768/haproxy
步骤二:安装软件
在haproxy99主机安装keepalived软件
在haproxy98主机安装keepalived软件
[root@haproxy99 ~]# yum -y install keepalived.x86_64
[root@haproxy98 ~]# yum -y install keepalived.x86_64
步骤三:修改配置文件
修改haproxy99主机的配置文件
[root@haproxy99 ~]#vim /etc/keepalived/keepalived.conf
global_defs {
……
……
vrrp_iptables //禁止iptables
}
vrrp_instance VI_1 {state MASTER //主服务器标识interface eth0virtual_router_id 51priority 150 //haproxy99 主机做主服务器,优先级要比 haproxy88主机高advert_int 1authentication {auth_type PASS //主备服务器连接方式auth_pass 1111 //连接密码}virtual_ipaddress {192.168.4.100 //定义vip地址}
}
[root@haproxy99 ~]# scp /etc/keepalived/keepalived.conf root@192.168.4.98: /etc/keepalived/
root@192.168.4.98's password: //输入haproxy98主机的密码
修改haproxy98主机的配置文件
[root@haproxy98 ~]#vim /etc/keepalived/keepalived.conf
global_defs {
……
……
vrrp_iptables //禁止iptables
}
vrrp_instance VI_1 {state BACKUP //备用服务器标识interface eth0virtual_router_id 51priority 100 //优先级要比 haproxy99低advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.4.100 //定义vip地址}
}
步骤四:启动服务
# 在haproxy99主机启动keepalived服务
[root@haproxy99 ~]# systemctl start keepalived.service //启动服务
[root@haproxy99 ~]#
[root@haproxy99 ~]# ip addr show | grep 192.168.4.100 //查看vip地址inet 192.168.4.100/32 scope global eth0
# 在haproxy98主机启动keepalived服务
[root@haproxy98 ~]# systemctl start keepalived.service //启动服务
[root@haproxy98 ~]#
[root@haproxy98 ~]# ip addr show | grep 192.168.4.100 //查看不到vip
步骤五:测试配置
# 客户端连接vip地址,访问数据库服务
[root@web33 ~]# mysql -h192.168.4.100 –uyaya99 –p123qqq…A -e 'select @@hostname'
测试高可用
[root@haproxy99 ~]# ip addr show | grep 192.168.4.100 //在haproxy99 主机查看VIP地址inet 192.168.4.100/32 scope global eth0
[root@haproxy99 ~]# systemctl stop keepalived.service //停止keepalived服务
[root@haproxy99 ~]# ip addr show | grep 192.168.4.100 //查看不到vip地址
[root@haproxy98 ~]# ip addr show | grep 192.168.4.100 //在备用的haproxy98主机查看地址inet 192.168.4.100/32 scope global eth0
//客户端连接vip地址访问数据库服务
[root@web33 ~]# mysql -h192.168.4.100 –uyaya99 –p123qqq…A -e 'select @@hostname'
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------+
| @@hostname |
+------------+
| pxcnode66 |
+------------+