项目介绍
角色与技术
涉及技术
- 逻辑卷
- 数据库服务
- MySql主从同步
- 数据读写分离
-
部署流程
部署数据库服务mysql
配置逻辑卷
- 添加磁盘
- 创建lv、格式化分区、设置开机挂载
- 部署mysql服务
- 挂载mysql目录至lv分区
- 查看初始密码、登陆修改密码、重新登陆
- 设置服务开机自启
- 配置主从同步
- 主服务器
- 修改配置文件(my.cnf)、指定server_id、启动binlog日志(log_bin=master11)
- 用户授权(grant)、查看binlog日志信息(show master status \G)
- 从服务器
- 指定server_id
- 指定主服务器信息(change master to)
- 启动slave服务(start slave)、查看状态(show slave status)
- 主服务器
配置读写分离
分区、格式化、创建挂载
配置NFS
安装依赖软件(java-1.8.0-openjdk)# 安装java JDk环境
- 安装tomcat源码包、安装连接mysql服务软件(mysql-connector-java)、安装提供showmount命令的软件(nfs-utils)
- 挂载共享目录至网页目录(/etc/fstab)
启动服务设置开机自启(将启动命令添加到rc.local文件)
测试配置
测试NFS服务
- 在共享目录编写网页文件
- 客户端访问网页
- 测试mysql服务
- 主服务器添加客户端授权用户
- 创建新库新表,插入数据
- 客户端连接读写分离服务器访问,查询、插入数据
测试tomcat服务
数据存储结构存在单点故障(读写分离服务器和主数据库宕机都会影响数据库正常使用)
- 网站服务每次都从源站提取数据
- 升级网站运行平台LNMP
升级后架构
升级内容:
- 升级网站运行平台
- 部署内存存储服务(redis),网站提取速度加速
- 部署 PXC集群(高可用,自动备份)
- 解决数据库服务负载问题
解决调度器单点故障问题(haproxy+keepalived)
升级网站运行平台
清除当前配置
- 停止服务、禁止开机运行
- 卸载共享存储(nfs)、禁止开机加载配置
- 部署LNMP
- 安装依赖软件
- gcc、zlib-devel、pcre-devel
- php、php-mysql、php-fpm、php-devel
- 安装源码nginx软件
- 挂载共享目录(/usr/local/nginx/html),配置开机自启
- 启动服务
- 修改nginx配置文件(添加支持php文件)
- 启动nginx服务、php-fpm服务
- 安装依赖软件
测试配置
创建redis集群
- 安装软件(gcc、redis-4.0.8.tar.gz)
- 初始化配置(./utils/install_server.sh)
- 停止服务
- 修改配置文件启用集群配置(/etc/init.d/redis_6379 start)
- 启动服务、查看服务信息
- 配置管理主机
- 部署运行环境并创建管理脚本
- 软件:ruby、rubygems(提高gem命令)
- gem install redis-3.2.1.gem
- redis-4.0.8.tar.gz下redis-trib.rb脚本,添加执行权限
- 创建集群:redis-trib.rb create —replicas 1 redis集群ip ……
- 查看集群信息:redis-trib.rb info 、redis-trib.rb check
- 测试配置:redis-cli -c -h 192.168.4.51 -p 6379;创建、查看变量
- 部署运行环境并创建管理脚本
- 配置网站服务器
- 配置php支持redis集群
- 软件:php-devel gcc redis-cluster-4.3.0.tgz
- phpize 创建./configure配置命令
- make、make install 编译安装
- 修改配置文件:/etc/php.ini
- 重启php-fpm服务、查看php -m | grep -i redis
- 配置php支持redis集群
测试配置
配置思路
- 首先配置PXC其中一台主机为mysql主服务器的从服务器
- 在配置PXC集群结构
- 配置pxcnode66主机为从服务器
- 安装mysql软件
- 修改配置文件,指定server_id
- 启动mysqld服务
- 在mysql主机,使用innobackupex命令备份数据拷贝给pxc主机,注意属组属主,确保数据一致性
- 指定主服务器(change master to),配置主从结构,查看状态信息
创建PXC集群
部署LB集群
- 安装软件(haproxy)
- 修改配置文件(/etc/haproxy/haproxy.cfg),添加监听的服务与端口等
- 启动服务设置开机自启
- 客户端测试,登陆数据库调度主机,查看主机名是否轮询
- 部署HA集群
- 准备另一台调度主机,配置与第一台相同
- 在两台主机安装软件(keepalived)
- 修改l两台主机配置文件(/etc/keepalived/keepalived.conf)
- 启动服务设置自启,查看主调度器vip
- 客户端测试,连接vip访问数据库,查看主机名是否轮询,模拟主调度器故障,验证访问不受影响
遇到问题
挂载迁移是部分文件目录消失的问题
解决办法:
- 首先停止该文件目录有关服务
- umount卸载,创建跳板目录,将需挂载的文件目录复制到跳板目录
- mount挂载,将跳板目录下内容重新传回相关目录下,主要将目录下文件权限由root改回需要的属组属主
- 启动服务即可
无法启动的问题
- 开机后无法查看php文件,访问网页404报错
- 原因:php-fpm服务没启动,nginx无法解析php文件
- 开机后,nginx访问不到
- 可能原因:nginx服务未启动
- 可能原因:nfs服务未启动
- 可能原因:共享文件挂载未实现
PXC集群第二、三台主机无法启动服务
添加磁盘
- 磁盘分区
- 创建LV
- 格式化
1.2 方案
分别给2台虚拟机添加2块5G的磁盘。如图-1所示。
图-11.3 步骤
实现此案例需要按照如下步骤进行。步骤一:磁盘分区(2台数据库服务器都配置)
1)创建分区,每块盘分1个区即可
2)查看分区[root@mysql11 ~]# fdisk -l /dev/vdb //查看磁盘容量
磁盘 /dev/vdb:5368 MB, 5368709120 字节,10485760 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
[root@mysql11 ~]#
[root@mysql11 ~]# fdisk -l /dev/vdc //查看磁盘容量
磁盘 /dev/vdc:5368 MB, 5368709120 字节,10485760 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
[root@mysql11 ~]#
[root@mysql11 ~]# fdisk /dev/vdb //磁盘分区
欢迎使用 fdisk (util-linux 2.23.2)。
更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。
Device does not contain a recognized partition table
使用磁盘标识符 0x882d04b7 创建新的 DOS 磁盘标签。
命令(输入 m 获取帮助):n //创建分区
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p //创建主分区
分区号 (1-4,默认 1):1 //指定编号
起始 扇区 (2048-10485759,默认为 2048): //起始柱面数
将使用默认值 2048
Last 扇区, +扇区 or +size{K,M,G} (2048-10485759,默认为 10485759)://结束柱面数
将使用默认值 10485759
分区 1 已设置为 Linux 类型,大小设为 5 GiB
命令(输入 m 获取帮助):w //保存退出
The partition table has been altered!
Calling ioctl() to re-read partition table.
正在同步磁盘。
[root@mysql11 ~]#
[root@mysql11 ~]# fdisk /dev/vdc //给vdc盘分区
欢迎使用 fdisk (util-linux 2.23.2)。
更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。
Device does not contain a recognized partition table
使用磁盘标识符 0x5184f504 创建新的 DOS 磁盘标签。
命令(输入 m 获取帮助):n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
分区号 (1-4,默认 1):1
起始 扇区 (2048-10485759,默认为 2048):
将使用默认值 2048
Last 扇区, +扇区 or +size{K,M,G} (2048-10485759,默认为 10485759):
将使用默认值 10485759
分区 1 已设置为 Linux 类型,大小设为 5 GiB
命令(输入 m 获取帮助):w
The partition table has been altered!
Calling ioctl() to re-read partition table.
正在同步磁盘。
[root@mysql11 ~]# fdisk -l /dev/vdb 磁盘 /dev/vdb:5368 MB, 5368709120 字节,10485760 个扇区 Units = 扇区 of 1 * 512 = 512 bytes 扇区大小(逻辑/物理):512 字节 / 512 字节 I/O 大小(最小/最佳):512 字节 / 512 字节 磁盘标签类型:dos 磁盘标识符:0x882d04b7 设备 Boot Start End Blocks Id System /dev/vdb1 2048 10485759 5241856 83 Linux [root@mysql11 ~]# [root@mysql11 ~]# [root@mysql11 ~]# fdisk -l /dev/vdc 磁盘 /dev/vdc:5368 MB, 5368709120 字节,10485760 个扇区 Units = 扇区 of 1 * 512 = 512 bytes 扇区大小(逻辑/物理):512 字节 / 512 字节 I/O 大小(最小/最佳):512 字节 / 512 字节 磁盘标签类型:dos 磁盘标识符:0x5184f504 设备 Boot Start End Blocks Id System /dev/vdc1 2048 10485759 5241856 83 Linux
步骤二:创建LV
1)安装提供命令的软件
2)创建PV[root@mysql11 ~]# rpm -q lvm2 || yum -y install lvm2 //安装软件 已加载插件:fastestmirror Loading mirror speeds from cached hostfile local_repo | 3.6 kB 00:00:00 正在解决依赖关系 --> 正在检查事务 ---> 软件包 lvm2.x86_64.7.2.02.177-4.el7 将被 安装 …… …… 已安装: lvm2.x86_64 7:2.02.177-4.el7 作为依赖被安装: device-mapper-event.x86_64 7:1.02.146-4.el7 device-mapper-event-libs.x86_64 7:1.02.146-4.el7 device-mapper-persistent-data.x86_64 0:0.7.3-3.el7 libaio.x86_64 0:0.3.109-13.el7 lvm2-libs.x86_64 7:2.02.177-4.el7 完毕!
3)创建VG[root@mysql11 ~]# pvcreate /dev/vdb1 /dev/vdc1 //创建PV Physical volume "/dev/vdb1" successfully created. Physical volume "/dev/vdc1" successfully created. [root@mysql11 ~]# [root@mysql11 ~]# pvscan //查看PV信息 PV /dev/vdb1 lvm2 [<5.00 GiB / 0 free] PV /dev/vdc1 lvm2 [<5.00 GiB / 0 free] PV /dev/sda2 VG centos lvm2 [<9.00 GiB / 0 free] Total: 3 [<18.99 GiB] / in use: 3 [<18.99 GiB] / in no VG: 0 [0 ]
4)创建LV[root@mysql11 ~]# vgcreate vg0 /dev/vdb1 /dev/vdc1 //创建vg 名称vg0 Volume group "vg0" successfully created [root@mysql11 ~]# [root@mysql11 ~]# vgdisplay vg0 //查看vg0详细信息 --- Volume group --- VG Name vg0 System ID Format lvm2 Metadata Areas 2 Metadata Sequence No 2 VG Access read/write VG Status resizable MAX LV 0 Cur LV 1 Open LV 1 Max PV 0 Cur PV 2 Act PV 2 VG Size 9.99 GiB //总大小 PE Size 4.00 MiB Total PE 2558 //PE总个数 Alloc PE / Size 2558 / 9.99 GiB Free PE / Size 0 / 0 VG UUID l1MQ7u-CeXC-fdkP-mJCT-0GqP-1gFQ-qKxWNA
5)格式化[root@mysql11 ~]# lvcreate -L 9.99G -n lv0 vg0 //创建LV 名称为lv0 大小9.99G Rounding up size to full physical extent 9.99 GiB Logical volume "lv0" created. [root@mysql11 ~]# [root@mysql11 ~]# lvdisplay /dev/vg0/lv0 //查看lv0信息 --- Logical volume --- LV Path /dev/vg0/lv0 LV Name lv0 VG Name vg0 LV UUID SciSrm-cp8l-4ZR2-wydh-xg6V-KMiI-pgW0j5 LV Write Access read/write LV Creation host, time mysql11, 2021-02-02 10:33:35 -0500 LV Status available # open 1 LV Size 9.99 GiB //总大小 Current LE 2558 Segments 2 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:2
[root@mysql11 ~]# mkfs.xfs /dev/vg0/lv0 //格式化为 xfs 文件系统 meta-data=/dev/vg0/lv0 isize=512 agcount=4, agsize=654848 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0, sparse=0 data = bsize=4096 blocks=2619392, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=1 log =internal log bsize=4096 blocks=2560, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 [root@mysql11 ~]# blkid /dev/vg0/lv0 //查看文件系统类型 /dev/vg0/lv0: UUID="3e06f58f-8793-4800-b444-676f5a07fe10" TYPE="xfs"
2 案例2:配置数据库服务器
2.1 问题
具体操作如下:
- 安装MySQL软件
- 挂载LV分区
- 启动服务
-
2.2 步骤
步骤一:安装软件MySQL服务软件(2台数据库服务器都要安装)
1)解包软件
[root@mysql11 ~]# tar -xvf mysql-5.7.17.tar ./mysql-community-client-5.7.17-1.el7.x86_64.rpm ./mysql-community-common-5.7.17-1.el7.x86_64.rpm ./mysql-community-devel-5.7.17-1.el7.x86_64.rpm ./mysql-community-embedded-5.7.17-1.el7.x86_64.rpm ./mysql-community-embedded-compat-5.7.17-1.el7.x86_64.rpm ./mysql-community-embedded-devel-5.7.17-1.el7.x86_64.rpm ./mysql-community-libs-5.7.17-1.el7.x86_64.rpm ./mysql-community-libs-compat-5.7.17-1.el7.x86_64.rpm ./mysql-community-minimal-debuginfo-5.7.17-1.el7.x86_64.rpm ./mysql-community-server-5.7.17-1.el7.x86_64.rpm ./mysql-community-test-5.7.17-1.el7.x86_64.rpm [root@mysql11 ~]# [root@mysql11 ~]# ls *.rpm mysql-community-client-5.7.17-1.el7.x86_64.rpm mysql-community-common-5.7.17-1.el7.x86_64.rpm mysql-community-devel-5.7.17-1.el7.x86_64.rpm mysql-community-embedded-5.7.17-1.el7.x86_64.rpm mysql-community-embedded-compat-5.7.17-1.el7.x86_64.rpm mysql-community-embedded-devel-5.7.17-1.el7.x86_64.rpm mysql-community-libs-5.7.17-1.el7.x86_64.rpm mysql-community-libs-compat-5.7.17-1.el7.x86_64.rpm mysql-community-minimal-debuginfo-5.7.17-1.el7.x86_64.rpm mysql-community-server-5.7.17-1.el7.x86_64.rpm mysql-community-test-5.7.17-1.el7.x86_64.rpm
2)安装软件
[root@mysql11 ~]# yum -y install mysql-community-*.rpm 已加载插件:fastestmirror 正在检查 mysql-community-client-5.7.17-1.el7.x86_64.rpm: mysql-community-client-5.7.17-1.el7.x86_64 mysql-community-client-5.7.17-1.el7.x86_64.rpm 将被安装 …… …… 已安装: mysql-community-client.x86_64 0:5.7.17-1.el7 mysql-community-common.x86_64 0:5.7.17-1.el7 mysql-community-devel.x86_64 0:5.7.17-1.el7 mysql-community-embedded.x86_64 0:5.7.17-1.el7 mysql-community-embedded-compat.x86_64 0:5.7.17-1.el7 mysql-community-embedded-devel.x86_64 0:5.7.17-1.el7 mysql-community-libs.x86_64 0:5.7.17-1.el7 mysql-community-libs-compat.x86_64 0:5.7.17-1.el7 mysql-community-minimal-debuginfo.x86_64 0:5.7.17-1.el7 mysql-community-server.x86_64 0:5.7.17-1.el7 mysql-community-test.x86_64 0:5.7.17-1.el7 作为依赖被安装: perl-Data-Dumper.x86_64 0:2.145-3.el7 perl-JSON.noarch 0:2.59-2.el7 完毕!
步骤二:挂载lv设备
1)设置开机挂载
[root@mysql11 ~]# blkid /dev/vg0/lv0 /dev/vg0/lv0: UUID="3e06f58f-8793-4800-b444-676f5a07fe10" TYPE="xfs" [root@mysql11 ~]# [root@mysql11 ~]# vim /etc/fstab /dev/vg0/lv0 /var/lib/mysql xfs defaults 0 0 :wq
2)查看挂载信息
[root@mysql11 ~]# mount -a [root@mysql11 ~]# mount | grep “/var/lib/mysql” /dev/mapper/vg0-lv0 on /var/lib/mysql type xfs (rw,relatime,attr2,inode64,noquota)
步骤三:启动服务
1)启动服务(俩台数据库服务器都要启动服务)
[root@mysql11 ~]# systemctl start mysqld [root@mysql11 ~]# 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@mysql11 ~]# systemctl enable mysqld [root@mysql11 ~]# [root@mysql11 ~]# netstat -utnlp | grep :3306 tcp6 0 0 :::3306 :::* LISTEN 1531/mysqld [root@mysql11 ~]# [root@mysql11 ~]# df -h | grep "/var/lib/mysql" //查看数据库目录容量 /dev/mapper/vg0-lv0 10G 166M 9.9G 2% /var/lib/mysql
步骤四:管理员登录
1)查看初始密码
[root@mysql11 ~]# 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@mysql11 ~]# [root@mysql11 ~]# mysql -uroot -p'bB0*uCmu:.Kj' //初始密码登录 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.7.17 Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
2)修改登录密码
mysql> alter user root@"localhost" identified by "123qqq...A"; //修改密码 Query OK, 0 rows affected (0.01 sec) mysql> mysql> exit Bye
3)新密码登录
[root@mysql11 ~]# mysql -uroot -p123qqq...A mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.7.17 MySQL Community Server (GPL) Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec)
3 案例3:配置主从同步
3.1 问题
配置步骤如下:
配置主服务器
- 配置从服务器
3.2 步骤
实现此案例需要按照如下步骤进行。步骤一:配置主服务器
1)启用binlog日志
2)用户授权[root@mysql11 ~]# vim /etc/my.cnf [mysqld] server_id=11 log-bin=master11 :wq [root@mysql11 ~]# systemctl restart mysqld
3)查看日志信息 ```shell [root@mysql11 ~]# mysql -uroot -p123qqq…A mysql> show master status; +————————-+—————+———————+—————————+—————————-+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +————————-+—————+———————+—————————+—————————-+ | master11.000001 | 441 | | | | +————————-+—————+———————+—————————+—————————-+ 1 row in set (0.00 sec)[root@mysql11 ~]# mysql -uroot -p123qqq...A mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 mysql> mysql> grant replication slave on *.* to repluser@"%" identified by "123qqq...A"; Query OK, 0 rows affected, 1 warning (0.04 sec)
<a name="JpOvC"></a>
#### 步骤二:配置从服务器
1)指定server_id
```shell
[root@mysql22 ~]# vim /etc/my.cnf
[mysqld]
server_id=22
:wq
2)指定主服务器信息
[root@mysql22 ~]# mysql -uroot -p123qqq...A
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.17 MySQL Community Server (GPL)
Mysql>
mysql> change master to master_host="192.168.4.11",master_user="repluser",
-> master_password="123qqq...A",master_log_file="master11.000001",master_log_pos=441;
Query OK, 0 rows affected, 2 warnings (0.41 sec)
3)启动slave进程
mysql> start slave ;
Query OK, 0 rows affected (0.02 sec)
4)查看状态信息
[root@mysql22 ~]# 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
Slave_SQL_Running: Yes
[root@mysql22 ~]#
[root@mysql22 ~]# 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
4 案例4:配置读写分离服务
4.1 问题
配置步骤如下:
- 安装软件
- 修改配置文件
- 配置数据库服务器
- 启动服务
- 查看服务状态
-
4.2 步骤
步骤一:配置读写分离服务
1)安装软件
[root@maxscale77 ~]# rpm -ivh maxscale-2.1.2-1.rhel.7.x86_64.rpm //安装软件 警告:maxscale-2.1.2-1.rhel.7.x86_64.rpm: 头V4 RSA/SHA1 Signature, 密钥 ID 8167ee24: NOKEY 准备中... ################################# [100%] 正在升级/安装... 1:maxscale-2.1.2-1 ( 2%################################# [100%] [root@maxscale77 ~]# [root@maxscale77 ~]# ls /etc/maxscale.cnf //主配置文件 /etc/maxscale.cnf [root@maxscale77 ~]# ls /var/log/maxscale/ //日志目录
2)修改配置文件
[root@maxscale77 ~]# vim /etc/maxscale.cnf [maxscale] //服务线程数量 threads=auto [server1] //第1台数据库服务器 type=server address=192.168.4.11 port=3306 protocol=MySQLBackend [server2] //第2台数据库服务器 type=server address=192.168.4.22 port=3306 protocol=MySQLBackend [MySQL Monitor] //指定监控数据库服务器server1 和 server2 type=monitor module=mysqlmon servers=server1,server2 user=mysqla //监控用户 passwd=123qqq...A //密码 monitor_interval=10000 [Read-Write Service] //定义读写分离服务 type=service router=readwritesplit servers=server1,server2 user=mysqlb //路由用户 passwd=123qqq...A //密码 max_slave_connections=100% [MaxAdmin Service] //定义管理服务 type=service router=cli [Read-Write Listener] //定义读写分离服务端口 type=listener service=Read-Write Service protocol=MySQLClient port=4006 //端口号 [MaxAdmin Listener] //定义管理服务端口 type=listener service=MaxAdmin Service protocol=maxscaled socket=default port=4016 //端口号 :wq
步骤二:配置数据库服务器
1)创建用户:在主服务器上添加,从服务器查看即可
[root@mysql11 ~]# mysql -uroot -p123qqq...A mysql> grant replication slave, replication client on *.* -> to mysqla@"%"identified by "123qqq...A"; mysql> grant select on mysql.* to -> mysqlb@"%" identified by "123qqq...A";
2) 从服务器查看授权用户
[root@mysql22 ~]# mysql -uroot -p123qqq...A -e 'select user,host from mysql.user where user like "maxscale%" ' mysql: [Warning] Using a password on the command line interface can be insecure. +----------------+------+ | user | host | +----------------+------+ | mysqla | % | | mysqlb | % | +----------------+------+
步骤三:启动maxsacle服务
1)启动服务
[root@maxscale77 ~]# maxscale -f /etc/maxscale.cnf
2)查看服务信息(进程 和 端口)
[root@maxscale77 ~]# ps -C maxscale PID TTY TIME CMD 23254 ? 00:00:00 maxscale [root@maxscale77 ~]# [root@maxscale77 ~]# netstat -utnlp | grep maxscale tcp6 0 0 :::4006 :::* LISTEN 23254/maxscale tcp6 0 0 :::4016 :::* LISTEN 23254/maxscale
3)在maxscale服务本机访问管理服务,查看监控信息
[root@maxscale77 ~]# maxadmin -uadmin -pmariadb -P4016 MaxScale> list servers Servers. -------------------+-----------------+-------+-------------+-------------------- Server | Address | Port | Connections | Status -------------------+-----------------+-------+-------------+-------------------- server1 | 192.168.4.11 | 3306 | 0 | Master, Running server2 | 192.168.4.22 | 3306 | 0 | Slave, Running -------------------+-----------------+-------+-------------+-------------------- MaxScale> MaxScale> exit
5 案例5:准备NFS服务存储磁盘
5.1 问题
具体配置如下:
添加磁盘
- 磁盘分区
- 创建LV
-
5.2 方案
5.3 步骤
步骤一:磁盘分区
1)创建分区,分1个区即可
[root@nfs30 ~]# fdisk -l /dev/vdb //查看磁盘信息 磁盘 /dev/vdb:10.7 GB, 10737418240 字节,20971520 个扇区 Units = 扇区 of 1 * 512 = 512 bytes 扇区大小(逻辑/物理):512 字节 / 512 字节 I/O 大小(最小/最佳):512 字节 / 512 字节 [root@nfs30 ~]# [root@nfs30 ~]# fdisk /dev/vdb //磁盘分区 欢迎使用 fdisk (util-linux 2.23.2)。 更改将停留在内存中,直到您决定将更改写入磁盘。 使用写入命令前请三思。 Device does not contain a recognized partition table 使用磁盘标识符 0x67bb10cf 创建新的 DOS 磁盘标签。 命令(输入 m 获取帮助):n //新建分区 Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): Select (default p): p //创建主分区 分区号 (1-4,默认 1): //回车 起始 扇区 (2048-20971519,默认为 2048): //回车 将使用默认值 2048 Last 扇区, +扇区 or +size{K,M,G} (2048-20971519,默认为 20971519)://回车 将使用默认值 20971519 分区 1 已设置为 Linux 类型,大小设为 10 GiB 命令(输入 m 获取帮助):w //保存退出 The partition table has been altered! Calling ioctl() to re-read partition table. 正在同步磁盘。
2)查看分区
[root@nfs30 ~]# fdisk -l /dev/vdb 磁盘 /dev/vdb:10.7 GB, 10737418240 字节,20971520 个扇区 Units = 扇区 of 1 * 512 = 512 bytes 扇区大小(逻辑/物理):512 字节 / 512 字节 I/O 大小(最小/最佳):512 字节 / 512 字节 磁盘标签类型:dos 磁盘标识符:0x67bb10cf 设备 Boot Start End Blocks Id System /dev/vdb1 2048 20971519 10484736 83 Linux
步骤二:挂载磁盘
1)格式化
[root@nfs30 ~]# mkfs.xfs /dev/vdb1 //格式化 meta-data=/dev/vdb1 isize=512 agcount=4, agsize=655296 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0, sparse=0 data = bsize=4096 blocks=2621184, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=1 log =internal log bsize=4096 blocks=2560, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 [root@nfs30 ~]# [root@nfs30 ~]# blkid /dev/vdb1 //查看 /dev/vdb1: UUID="81740d7a-51f1-4ce1-a830-7b6517cc778e" TYPE="xfs"
2)配置开机挂载
[root@nfs30 ~]# vim /etc/fstab //修改配置文件 /dev/vdb1 /sitedir xfs defaults 0 0 [root@nfs30 ~]# [root@nfs30 ~]# mkdir /sitedir //创建挂载目录 [root@nfs30 ~]# chmod o+w /sitedir //赋予写权限 [root@nfs30 ~]# mount –a //加载文件中所有未加载的设备 [root@nfs30 ~]# mount | grep "/sitedir" //查看加载信息 /dev/vdb1 on /sitedir type xfs (rw,relatime,attr2,inode64,noquota)
6 案例6:配置NFS服务
6.1 问题
具体配置如下:
安装软件
- 修改配置文件
- 启动服务
-
6.2 步骤
步骤一:部署NFS服务
1)安装软件
[root@nfs30 ~]# yum -y install nfs-utils rpcbind 已安装: nfs-utils.x86_64 1:1.3.0-0.54.el7 rpcbind.x86_64 0:0.2.0-44.el7 作为依赖被安装: gssproxy.x86_64 0:0.7.0-17.el7 keyutils.x86_64 0:1.5.8-3.el7 libbasicobjects.x86_64 0:0.1.1-29.el7 libcollection.x86_64 0:0.7.0-29.el7 libevent.x86_64 0:2.0.21-4.el7 libini_config.x86_64 0:1.3.1-29.el7 libnfsidmap.x86_64 0:0.25-19.el7 libpath_utils.x86_64 0:0.2.1-29.el7 libref_array.x86_64 0:0.1.5-29.el7 libtirpc.x86_64 0:0.2.4-0.10.el7 libverto-libevent.x86_64 0:0.2.5-4.el7 quota.x86_64 1:4.01-17.el7 quota-nls.noarch 1:4.01-17.el7 tcp_wrappers.x86_64 0:7.6-77.el7 完毕!
2)修改配置文件
[root@nfs30 ~]# vim /etc/exports /sitedir *(rw) :wq [root@nfs30 ~]# exportfs –r //加载配置,无需重启nfs服务
步骤二:启动服务
1)启动服务
[root@nfs30 ~]# systemctl start rpcbind [root@nfs30 ~]# systemctl start nfs [root@nfs30 ~]# systemctl enable nfs Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service. [root@nfs30 ~]# [root@nfs30 ~]# systemctl enable rpcbind
2)查看共享信息
[root@nfs30 ~]# showmount -e localhost Export list for localhost: /sitedir *
7 案例7:配置网站服务
7.1 问题
具体配置如下:
安装软件
- 挂载共享存储
-
7.2 步骤
步骤一:安装软件
1)安装软件
[root@web33 ~]# yum -y install java-1.8.0-openjdk-1.8.0.161-2.b14.el7.x86_64 //安装JDK [root@web33 ~]# tar -zxvf apache-tomcat-8.0.30.tar.gz //解压tomcat源码 [root@web33 ~]#mv apache-tomcat-8.0.30 /usr/local/tomcat //方便管理移动目录 [root@web33 ~]#yum -y install mysql-connector-java //安装连接mysql服务软件 [root@web33 ~]#cp /usr/share/java/mysql-connector-java.jar /usr/local/tomcat/lib/ //拷贝连接命令 [root@web33 ~]# rm -rf /usr/local/tomcat/webapps/ROOT/* //删除默认的网页文件 [root@web33 ~]# yum -y install nfs-utils //安装提供showmount命令软件
步骤二:挂载共享
1)查看共享目录
[root@web33 ~]# showmount -e 192.168.4.30 //查看共享 Export list for 192.168.4.30: /sitedir *
2)挂载共享目录
[root@web33 ~]# vim /etc/fstab //配置开机挂载 192.168.4.30:/sitedir /usr/local/tomcat/webapps/ROOT nfs defaults 0 0 :wq [root@web33 ~]# mount –a //加载所有未加载的设备
3)查看挂载
[root@web33 ~]# mount | grep "sitedir" //查看挂载 192.168.4.30:/sitedir on /usr/local/tomcat/webapps/ROOT type nfs4 (rw,relatime,vers=4.1,rsize=65536,wsize=65536,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=192.168.4.33,local_lock=none,addr=192.168.4.30) [root@web33 ~]# systemctl enable remote-fs.target //设置远程加载服务开机运行
步骤三:启动服务
1)启动服务
[root@web33 ~]# /usr/local/tomcat/bin/startup.sh [root@web33 ~]# echo “/usr/local/tomcat/bin/startup.sh” >> /etc/rc.local //设置服务开机运行 [root@web33 ~]# chmod +x /etc/rc.local
2)查看服务运行信息
[root@web33 ~]# netstat -utnlp | grep java tcp6 0 0 127.0.0.1:8005 :::* LISTEN 83033/java tcp6 0 0 :::8009 :::* LISTEN 83033/java tcp6 0 0 :::8080 :::* LISTEN 83033/java
8 案例8:测试配置
8.1 问题
具体配置如下:
测试NFS服务
- 测试MySQL服务
-
8.2 步骤
步骤一:测试NFS服务
1)在nfs服务器创建网页 test.html
[root@nfs30 ~]# echo "web test page" > /sitedir/test.html [root@nfs30 ~]# [root@nfs30 ~]# ls /sitedir/ test.html
2)在客户端访问网站服务
[root@room9pc17 ~]# curl http://192.168.4.33:8080/test.html //访问网站33主机 web test page
步骤二:测试MySQL服务
1)在主数据库服务器上添加,访问数据的连接用户
[root@mysql11 ~]# mysql -uroot -p123qqq...A mysql> create database gamedb; //建库 mysql> create table gamedb.user(name char(10)); //建表 mysql> grant select,insert,update,delete on gamedb.* to yaya99@"%" identified by "123qqq...A"; //用户授权
2)在从服务器查看是否同步数据
[root@mysql22 ~]# mysql -uroot -p123qqq...A mysql> show grants for yaya99@"%"; //查看授权用户 +--------------------------------------------------------------------+ | Grants for yaya99@% | +--------------------------------------------------------------------+ | GRANT USAGE ON *.* TO 'yaya99'@'%' | | GRANT SELECT, INSERT, UPDATE, DELETE ON `gamedb`.* TO 'yaya99'@'%' | +--------------------------------------------------------------------+ 2 rows in set (0.00 sec) mysql> desc gamedb.user; //查看库表 +-------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+-------+ | name | char(10) | YES | | NULL | | +-------+----------+------+-----+---------+-------+ mysql> insert into gamedb.user values("xdd"); //用来验证数据读写分离 mysql> select * from gamedb.user; +------+ | name | +------+ | xdd | +------+ 1 row in set (0.00 sec)
3)测试读写分离服务
在网站服务器上,连接数据读写分离服务器77[root@web33 ~]# yum -y install mariadb //安装提供连接命令软件 已安装: mariadb.x86_64 1:5.5.56-2.el7 作为依赖被安装: mariadb-libs.x86_64 1:5.5.56-2.el7 完毕! [root@web33 ~]# [root@web33 ~]# mysql -h192.168.4.77 -P4006 -uyaya99 -p123qqq...A //连接读写分离服务 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MySQL connection id is 23258 Server version: 10.0.0 2.1.2-maxscale MySQL Community Server (GPL) Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MySQL [(none)]>
4)查询数据
MySQL [(none)]> select * from gamedb.user; //显示的是从服务器上的数据 +------+ | name | +------+ | xdd | +------+ 1 row in set (0.00 sec)
5)存储数据
MySQL [(none)]> insert into gamedb.user values("pmm"); //插入记录 Query OK, 1 row affected (0.05 sec) MySQL [(none)]> select * from gamedb.user; //查看记录 +------+ | name | +------+ | xdd | | pmm | +------+ 2 rows in set (0.00 sec)
6)在主服务器本机登录查看数据
[root@mysql11 ~]# mysql -uroot -p123qqq...A -e "select * from gamedb.user" mysql: [Warning] Using a password on the command line interface can be insecure. +------+ | name | +------+ | pmm | +------+
步骤三:Tomcat连接MySQL服务
1)在nfs服务共享目录编写网页文件
[root@nfs30 ~]# vim /sitedir/linkdb.jsp <%@ page language="java" import="java.util.*" pageEncoding="gbk"%> <%@ page import="java.naming.*" %> <%@ page import="java.sql.*" %> <html> <body> <% Class.forName("com.mysql.jdbc.Driver"); Connection con=DriverManager.getConnection("jdbc:mysql://192.168.4.11:3306/gamedb","yaya99","123qqq...A"); Statement state=con.createStatement(); String sql="insert into user values('TOM')"; state.executeUpdate(sql); %> <h1>data save ok</h1> </body> </html> :wq
2)在浏览器地址栏里输入网站访问
http://192.168.4.33:8080/linkdb.jsp
3)在据库服务器查看数据
[root@mysql11 ~]# mysql -uadmin -p123qqq...A -e 'select * from gamedb.user' mysql: [Warning] Using a password on the command line interface can be insecure. +------+ | name | +------+ | TOM | | xdd | | pmm | +------+
NSD PROJECT2 DAY02
1 案例1:升级网站运行平台
1.1 问题
具体配置如下:
清除当前配置
- 部署LNMP
-
1.2 步骤
步骤一:清除当前配置
1)停止网站服务
[root@web33 ~]# /usr/local/tomcat/bin/shutdown.sh [root@web33 ~]# vim /etc/rc.local #/usr/local/tomcat/bin/startup.sh
2)卸载共享存储
[root@web33 ~]# umount /usr/local/tomcat/webapps/ROOT //卸载当前挂载 [root@web33 ~]# vim /etc/fstab //清除开机挂载 #192.168.4.30:/sitedir /usr/local/tomcat/webapps/ROOT nfs defaults 0 0
步骤二:部署LNMP
1)安装软件
[root@web33 ~]# yum -y install gcc zlib-devel pcre-devel //安装源码Nginx依赖软件 已安装: gcc.x86_64 0:4.8.5-28.el7 pcre-devel.x86_64 0:8.32-17.el7 zlib-devel.x86_64 0:1.2.7-17.el7 作为依赖被安装: cpp.x86_64 0:4.8.5-28.el7 glibc-devel.x86_64 0:2.17-222.el7 glibc-headers.x86_64 0:2.17-222.el7 kernel-headers.x86_64 0:3.10.0-862.el7 libmpc.x86_64 0:1.0.1-3.el7 mpfr.x86_64 0:3.1.1-4.el7 完毕! [root@web33 ~]# [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 //编译 [root@web33 nginx-1.12.2]# make install //安装 [root@web33 nginx-1.12.2]# ls /usr/local/nginx //查看安装目录 conf html logs sbin [root@web33 nginx-1.12.2]# [root@web33 ~]# yum -y install php-fpm //安装php-fpm 软件 …… …… 已安装: php-fpm.x86_64 0:5.4.16-45.el7 作为依赖被安装: libzip.x86_64 0:0.10.1-8.el7 php-common.x86_64 0:5.4.16-45.el7 完毕! [root@web33 ~]# yum -y install php php-mysql //安装php 及 php-mysql 软件 …… …… 已安装: php.x86_64 0:5.4.16-45.el7 php-mysql.x86_64 0:5.4.16-45.el7 作为依赖被安装: mariadb-libs.x86_64 1:5.5.56-2.el7 php-cli.x86_64 0:5.4.16-45.el7 php-pdo.x86_64 0:5.4.16-45.el7 完毕!
2)挂载共享存储
[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" //查看挂载 192.168.4.30:/sitedir on /usr/local/nginx/html type nfs4 (rw,relatime,vers=4.1,rsize=262144,wsize=262144,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=192.168.4.33,local_lock=none,addr=192.168.4.30)
3)启动服务
[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 ~]# [root@web33 ~]# netstat -utnlp | grep :80 //查看端口 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 26335/nginx: master [root@web33 ~]# [root@web33 ~]# systemctl start php-fpm [root@web33 ~]# [root@web33 ~]# netstat -utnlp | grep :9000 tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 26345/php-fpm: mast
步骤三:测试配置
1)编写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"; ?>
2)访问脚本
[root@client50 ~]# curl http://192.168.4.33/linkdb2.php //访问web33服务器 save data ok
3)查看数据
[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 案例2:部署内存存储服务
2.1 问题
具体操作如下:
部署redis服务器
- 创建redis集群
- 配置网站服务器
- 测试配置
2.2 方案
克隆7台虚拟机配置要求如图-1所示。
图-12.3 步骤
实现此案例需要按照如下步骤进行。步骤一:部署redis服务器(6台都要配置)
1)搭建redis服务器 ```shell [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 //初始化配置 Welcome to the redis service installer This script will help you easily set up a running redis server Please select the redis port for this instance: [6379] //端口号 Selecting default: 6379 Please select the redis config file name [/etc/redis/6379.conf] //主配置文件 Selected default - /etc/redis/6379.conf Please select the redis log file name [/var/log/redis_6379.log] //日志文件 Selected default - /var/log/redis_6379.log Please select the data directory for this instance [/var/lib/redis/6379] //数据库目录 Selected default - /var/lib/redis/6379 Please select the redis executable path [/usr/local/bin/redis-server] //服务启动启动程序 Selected config: //配置总结 Port : 6379 Config file : /etc/redis/6379.conf Log file : /var/log/redis_6379.log Data dir : /var/lib/redis/6379 Executable : /usr/local/bin/redis-server Cli Executable : /usr/local/bin/redis-cli Is this ok? Then press ENTER to go on or Ctrl-C to abort. Copied /tmp/6379.conf => /etc/init.d/redis_6379 Installing service… Successfully added to chkconfig! Successfully added to runlevels 345! Starting Redis server… //服务启动提示 Installation successful! //安装完成提示
[root@redisA redis-4.0.8]# /etc/init.d/redis_6379 stop //停止服务 Stopping … Redis stopped [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 //集群端口
redisB~F主机同样配置
<a name="WfYz9"></a>
#### 步骤二:创建redis集群
1)配置管理主机
```shell
[root@mgm ~]# yum -y install ruby rubygems //安装依赖
……
……
已安装:
ruby.x86_64 0:2.0.0.648-33.el7_4 rubygems.noarch 0:2.0.14.1-33.el7_4
作为依赖被安装:
libyaml.x86_64 0:0.1.4-11.el7_0 ruby-irb.noarch 0:2.0.0.648-33.el7_4 ruby-libs.x86_64 0:2.0.0.648-33.el7_4 rubygem-bigdecimal.x86_64 0:1.2.0-33.el7_4
rubygem-io-console.x86_64 0:0.4.2-33.el7_4 rubygem-json.x86_64 0:1.7.7-33.el7_4 rubygem-psych.x86_64 0:2.0.0-33.el7_4 rubygem-rdoc.noarch 0:4.0.0-33.el7_4
完毕!
[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 ...>
create host1:port1 ... hostN:portN
--replicas <arg>
check host:port
info host:port
fix host:port
--timeout <arg>
reshard host:port
--from <arg>
--to <arg>
--slots <arg>
--yes
--timeout <arg>
--pipeline <arg>
rebalance host:port
--weight <arg>
--auto-weights
--use-empty-masters
--timeout <arg>
--simulate
--pipeline <arg>
--threshold <arg>
add-node new_host:new_port existing_host:existing_port
--slave
--master-id <arg>
del-node host:port node_id
set-timeout host:port milliseconds
call host:port command arg arg .. arg
import host:port
--from <arg>
--copy
--replace
help (show this help)
For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.
2)创建集群
]# 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:6379
slots:0-5460 (5461 slots) master
M: 324e05df3f143ef97e50d09be0328a695e655986 192.168.4.52:6379
slots:5461-10922 (5462 slots) master
M: 9e44139cffb8ebd7ed746aabbf4bcea9bf207645 192.168.4.53:6379
slots:10923-16383 (5461 slots) master
S: d9634ba0aa5c1a07193da4a013da6051c1515922 192.168.4.54:6379
replicates 9e44139cffb8ebd7ed746aabbf4bcea9bf207645
S: 2d343a9df48f6f6e207949e980ef498466a44dad 192.168.4.56:6379
replicates d9f8fe6d6d9dd391be8e7904501db1535e4d17cb
S: 894dd0008053f6fb65e9e4a36b755d9351607500 192.168.4.55:6379
replicates 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:6379
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: d9634ba0aa5c1a07193da4a013da6051c1515922 192.168.4.54:6379
slots: (0 slots) slave
replicates 9e44139cffb8ebd7ed746aabbf4bcea9bf207645
S: 894dd0008053f6fb65e9e4a36b755d9351607500 192.168.4.56:6379
slots: (0 slots) slave
replicates 324e05df3f143ef97e50d09be0328a695e655986
M: 324e05df3f143ef97e50d09be0328a695e655986 192.168.4.52:6379
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: 9e44139cffb8ebd7ed746aabbf4bcea9bf207645 192.168.4.53:6379
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 2d343a9df48f6f6e207949e980ef498466a44dad 192.168.4.57:6379
slots: (0 slots) slave
replicates d9f8fe6d6d9dd391be8e7904501db1535e4d17cb
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered. //提示16384个槽分配完毕
3)查看集群信息
[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.
[root@mgm ~]# redis-trib.rb check 192.168.4.51:6379 //检测集群
>>> Performing Cluster Check (using node 192.168.4.51:6379)
M: d9f8fe6d6d9dd391be8e7904501db1535e4d17cb 192.168.4.51:6379
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: d9634ba0aa5c1a07193da4a013da6051c1515922 192.168.4.54:6379
slots: (0 slots) slave
replicates 9e44139cffb8ebd7ed746aabbf4bcea9bf207645
S: 894dd0008053f6fb65e9e4a36b755d9351607500 192.168.4.55:6379
slots: (0 slots) slave
replicates 324e05df3f143ef97e50d09be0328a695e655986
M: 324e05df3f143ef97e50d09be0328a695e655986 192.168.4.52:6379
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: 9e44139cffb8ebd7ed746aabbf4bcea9bf207645 192.168.4.53:6379
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 2d343a9df48f6f6e207949e980ef498466a44dad 192.168.4.56:6379
slots: (0 slots) slave
replicates d9f8fe6d6d9dd391be8e7904501db1535e4d17cb
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
4)测试配置(在客户端连接集群中的任意一台服务器存取数据)
# 从其他主机拷贝redis-cli命令至/usr/bin下,添加执行权限
[root@client50 ~]# redis-cli -c -h 192.168.4.51 -p 6379 //连接服务器51
192.168.4.51:6379>
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> keys *
1) "x"
192.168.4.53:6379>
192.168.4.53:6379> set y 200
OK
192.168.4.53:6379> keys *
1) "y"
2) "x"
192.168.4.53:6379> set z 300 //存储
-> Redirected to slot [8157] located at 192.168.4.52:6379 //提示存储在52主机
OK
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"
192.168.4.53:6379> keys *
1) "y"
2) "x"
192.168.4.53:6379> get z
-> Redirected to slot [8157] located at 192.168.4.52:6379
"300"
192.168.4.52:6379> set i 400
-> Redirected to slot [15759] located at 192.168.4.53:6379
OK
192.168.4.53:6379> set j 500
-> Redirected to slot [3564] located at 192.168.4.51:6379
OK
步骤三:配置网站服务器
[root@web33 ~ ]# yum -y install php-devel //安装依赖
……
……
已安装:
php-devel.x86_64 0:5.4.16-45.el7
作为依赖被安装:
autoconf.noarch 0:2.69-11.el7 automake.noarch 0:1.13.4-3.el7 m4.x86_64 0:1.4.16-10.el7 perl-Test-Harness.noarch 0:3.28-3.el7 perl-Thread-Queue.noarch 0:3.02-2.el7
完毕!
[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
Configuring for:
PHP Api Version: 20100412
Zend Module Api No: 20100525
Zend Extension Api No: 220100525
[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]# make //编译
[root@web33 redis-4.3.0]# make install //安装
Installing shared extensions: /usr/lib64/php/modules/ //提示模块安装目录
[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
步骤四:测试配置
1)在存储服务器共享目录下,创建连接集群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
?>
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”); //取数据
?>
2)访问网站执行脚本(在任意主机访问网站服务器都可以)
]# 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)命令行连接任意一台redis服务器查看数据(在任意主机连接redis服务器都可以)
]# redis-cli -c -h 192.168.4.51 -p 6379
192.168.4.51:6379> keys *
1) i
192.168.4.51:6379> exit
]# redis-cli -c -h 192.168.4.52 -p 6379
192.168.4.52:6379> keys *
1) j
192.168.4.52:6379> exit
]# redis-cli -c -h 192.168.4.53 -p 6379
192.168.4.52:6379> keys *
1) k
192.168.4.52:6379> exit
3 案例3:数据迁移
3.1 问题
要求如下:
- 配置从服务器
- 配置第1台PXC服务器
- 配置第2台PXC服务器
- 配置第3台PXC服务器
- 公共配置
- 测试配置
3.2 方案
创建3台新的虚拟机,具体配置要求如图-2所示。
图-23.3 步骤
实现此案例需要按照如下步骤进行。步骤一:配置从服务器(把主机192.168.4.66 配置为192.168.4.11的从服务器)
1)在192.168.4.66主机安装数据库服务软件并启动mysqld服务 ```shell [root@pxcnode66 ~]# tar -xvf mysql-5.7.17.tar //解包 [root@pxcnode66 ~]# ls .rpm //查看软件列表 mysql-community-client-5.7.17-1.el7.x86_64.rpm mysql-community-common-5.7.17-1.el7.x86_64.rpm mysql-community-devel-5.7.17-1.el7.x86_64.rpm mysql-community-embedded-5.7.17-1.el7.x86_64.rpm mysql-community-embedded-compat-5.7.17-1.el7.x86_64.rpm mysql-community-embedded-devel-5.7.17-1.el7.x86_64.rpm mysql-community-libs-5.7.17-1.el7.x86_64.rpm mysql-community-libs-compat-5.7.17-1.el7.x86_64.rpm mysql-community-minimal-debuginfo-5.7.17-1.el7.x86_64.rpm mysql-community-server-5.7.17-1.el7.x86_64.rpm mysql-community-test-5.7.17-1.el7.x86_64.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 ~]# netstat -utnlp | grep :3306 //查看端口 tcp6 0 0 :::3306 :::* LISTEN 1531/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: bB0uCmu:.Kj [root@pxcnode66 ~]# mysql -uroot -p’bB0uCmu:.Kj’ //初始密码登录 mysql> alter user root@”localhost” identified by “123qqq…A”;//修改登录密码 Query OK, 0 rows affected (0.01 sec)
[root@pxcnode66 ~]# mysql -uroot -p123qqq…A //新密码登录 mysql> show databases; //查看数据库 +——————————+ | Database | +——————————+ | information_schema | | mysql | | performance_schema | | sys | +——————————+ 4 rows in set (0.00 sec)
2)修改服务主配置文件
```shell
[root@pxcnode66 ~]# vim /etc/my.cnf
[mysqld]
server_id=66 //指定server_id
:wq
[root@pxcnode66 ~]# systemctl restart mysqld //重启服务
3)确保数据一致 (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 //启动服务
4)指定主服务器
[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 ~]#
[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)
1)停止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 mysql-community-test mysql-community-libs-compat mysql-community-minimal-debuginfo mysql-community-libs mysql-community-embedded mysql-community-embedded-devel //卸载所有的MySQL服务软件
警告:/etc/my.cnf 已另存为 /etc/my.cnf.rpmsave
[root@pxcnode66 ~]#
2)安装PXC软件、修改配置文件、启动mysql服务
[root@pxcnode66 ~]# cd PXC //进软件目录
[root@pxcnode66 PXC]# rpm -ivh qpress-1.1-14.11.x86_64.rpm //安装依赖
[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
:wq
[root@pxcnode66 PXC]#
[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]#
[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 //设置服务开机运行
3)数据库管理员登录、用户授权、查看状态信息
[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)
1)安装PXC软件
[root@pxcnode10 ~]# cd PXC //进软件目录
[root@pxcnode10 PXC]# rpm -ivh qpress-1.1-14.11.x86_64.rpm //安装依赖
[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 //安装软件
已安装:
Percona-XtraDB-Cluster-57.x86_64 0:5.7.25-31.35.1.el7 Percona-XtraDB-Cluster-57-debuginfo.x86_64 0:5.7.25-31.35.1.el7
Percona-XtraDB-Cluster-client-57.x86_64 0:5.7.25-31.35.1.el7 Percona-XtraDB-Cluster-devel-57.x86_64 0:5.7.25-31.35.1.el7
Percona-XtraDB-Cluster-full-57.x86_64 0:5.7.25-31.35.1.el7 Percona-XtraDB-Cluster-garbd-57.x86_64 0:5.7.25-31.35.1.el7
Percona-XtraDB-Cluster-server-57.x86_64 0:5.7.25-31.35.1.el7 Percona-XtraDB-Cluster-shared-57.x86_64 0:5.7.25-31.35.1.el7
Percona-XtraDB-Cluster-shared-compat-57.x86_64 0:5.7.25-31.35.1.el7 Percona-XtraDB-Cluster-test-57.x86_64 0:5.7.25-31.35.1.el7
作为依赖被安装:
keyutils-libs-devel.x86_64 0:1.5.8-3.el7 krb5-devel.x86_64 0:1.15.1-18.el7 libcom_err-devel.x86_64 0:1.42.9-11.el7 libkadm5.x86_64 0:1.15.1-18.el7
libselinux-devel.x86_64 0:2.5-12.el7 libsepol-devel.x86_64 0:2.5-8.1.el7 libverto-devel.x86_64 0:0.2.5-4.el7 openssl-devel.x86_64 1:1.0.2k-12.el7
pcre-devel.x86_64 0:8.32-17.el7 perl-Env.noarch 0:1.04-2.el7 perl-Test-Harness.noarch 0:3.28-3.el7 perl-Test-Simple.noarch 0:0.98-243.el7
zlib-devel.x86_64 0:1.2.7-17.el7
完毕!
2)修改配置文件
[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
3)启动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
[root@pxcnode10 PXC]#
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)
1)安装PXC软件
[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软件包
Percona-XtraDB-Cluster-57-5.7.25-31.35.1.el7.x86_64.rpm
Percona-XtraDB-Cluster-57-debuginfo-5.7.25-31.35.1.el7.x86_64.rpm
Percona-XtraDB-Cluster-client-57-5.7.25-31.35.1.el7.x86_64.rpm
Percona-XtraDB-Cluster-devel-57-5.7.25-31.35.1.el7.x86_64.rpm
Percona-XtraDB-Cluster-full-57-5.7.25-31.35.1.el7.x86_64.rpm
Percona-XtraDB-Cluster-garbd-57-5.7.25-31.35.1.el7.x86_64.rpm
Percona-XtraDB-Cluster-server-57-5.7.25-31.35.1.el7.x86_64.rpm
Percona-XtraDB-Cluster-shared-57-5.7.25-31.35.1.el7.x86_64.rpm
Percona-XtraDB-Cluster-shared-compat-57-5.7.25-31.35.1.el7.x86_64.rpm
Percona-XtraDB-Cluster-test-57-5.7.25-31.35.1.el7.x86_64.rpm
[root@pxcnode88 PXC]#
[root@pxcnode88 PXC]# yum -y install Percona-XtraDB-Cluster-*.rpm //安装软件
已安装:
Percona-XtraDB-Cluster-57.x86_64 0:5.7.25-31.35.1.el7 Percona-XtraDB-Cluster-57-debuginfo.x86_64 0:5.7.25-31.35.1.el7
Percona-XtraDB-Cluster-client-57.x86_64 0:5.7.25-31.35.1.el7 Percona-XtraDB-Cluster-devel-57.x86_64 0:5.7.25-31.35.1.el7
Percona-XtraDB-Cluster-full-57.x86_64 0:5.7.25-31.35.1.el7 Percona-XtraDB-Cluster-garbd-57.x86_64 0:5.7.25-31.35.1.el7
Percona-XtraDB-Cluster-server-57.x86_64 0:5.7.25-31.35.1.el7 Percona-XtraDB-Cluster-shared-57.x86_64 0:5.7.25-31.35.1.el7
Percona-XtraDB-Cluster-shared-compat-57.x86_64 0:5.7.25-31.35.1.el7 Percona-XtraDB-Cluster-test-57.x86_64 0:5.7.25-31.35.1.el7
作为依赖被安装:
keyutils-libs-devel.x86_64 0:1.5.8-3.el7 krb5-devel.x86_64 0:1.15.1-18.el7 libcom_err-devel.x86_64 0:1.42.9-11.el7 libkadm5.x86_64 0:1.15.1-18.el7
libselinux-devel.x86_64 0:2.5-12.el7 libsepol-devel.x86_64 0:2.5-8.1.el7 libverto-devel.x86_64 0:0.2.5-4.el7 openssl-devel.x86_64 1:1.0.2k-12.el7
pcre-devel.x86_64 0:8.32-17.el7 perl-Env.noarch 0:1.04-2.el7 perl-Test-Harness.noarch 0:3.28-3.el7 perl-Test-Simple.noarch 0:0.98-243.el7
zlib-devel.x86_64 0:1.2.7-17.el7
完毕!
2)修改配置文件
[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
3)启动mysql服务
[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)
1)修改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 //指定集群成员列表
2)修改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 //指定集群成员列表
3)修改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 //指定集群成员列表
步骤6:测试配置:在网站服务器连接PXC集群主机存取数据:
1)存储数据:在网站服务器连接PXC集群主机存储数据
[root@web33 ~]# mysql -h192.168.4.66 -uyaya99 -p123qqq…A gamedb
Mysql> insert into gamedb.user values (“pljA”);
Mysql> exit ;
[root@web33 ~]# mysql -h192.168.4.10 -uyaya99 -p123qqq…A gamedb
Mysql> insert into gamedb.user values (“pljB”);
Mysql> exit ;
[root@web33 ~]# mysql -h192.168.4.88 -uyaya99 -p123qqq…A gamedb
Mysql> insert into gamedb.user values (“pljC”);
Mysql> exit ;
2)查询数据:在网站服务器连接PXC集群主机查询数据
[root@web44 ~]# mysql -h192.168.4.66 -uyaya99 -p123qqq…A gamedb
Mysql> select * from gamedb.user;
+------+
| name|
+------+
| pljA|
| pljB|
| pljC|
+------+
Mysql> exit ;
[root@web44 ~]# mysql -h192.168.4.10 -uyaya99 -p123qqq…A gamedb
Mysql> select * from gamedb.user;
+------+
| name|
+------+
| pljA|
| pljB|
| pljC|
+------+
Mysql> exit ;Mysql> exit ;
[root@web44 ~]# mysql -h192.168.4.88 -uyaya99 -p123qqq…A gamedb
Mysql> select * from gamedb.user;
+------+
| name|
+------+
| pljA|
| pljB|
| pljC|
+------+
Mysql> exit ;
4 案例4:部署LB集群
4.1 问题
配置步骤如下:
- 安装软件
- 修改配置文件
- 启动服务
-
4.2 方案
拓扑结构如图-3所示。创建1台新的虚拟机,配置ip地址 eth0 192.168.4.99 主机名 haproxy99 ;运行haproxy服务 接受客户端访问数据库的连接请求,把请求平均分发给3台PXC集群主机。
图-34.3 步骤
步骤一:安装软件: 在haproxy99主机上安装haproxy软件
[root@haproxy99 ~]# yum -y install haproxy …… Running transaction 正在安装 : haproxy-1.5.18-7.el7.x86_64 1/1 验证中 : haproxy-1.5.18-7.el7.x86_64 1/1 已安装: haproxy.x86_64 0:1.5.18-7.el7 完毕!
步骤二:修改配置文件
[root@haproxy99 ~]# vim /etc/haproxy/haproxy.cfg Global //全局配置默认即可 log 127.0.0.1 local2 chroot /var/lib/haproxy pidfile /var/run/haproxy.pid maxconn 4000 user haproxy group haproxy daemon stats socket /var/lib/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 status //定义监控页面 mode http //模式为http bind *:80 //端口80 stats 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 案例5:部署HA集群
5.1 问题
具体配置如下:
准备备用调度器主机
- 安装软件
- 修改配置文件
- 启动服务
- 测试配置
5.2 方案
拓扑结构如图-4所示。创建1台新的虚拟机,在eth0 接口配置ip地址为192.168.4.98做备用调度器。
图-45.3 步骤
实现此案例需要按照如下步骤进行。步骤一:准备备用调度器主机
2)修改haproxy98主机haproxy.conf文件(直接拷贝haproxy99主机的配置文件也可以)[root@haproxy98 ~]# yum -y install haproxy 正在安装 : haproxy-1.5.18-7.el7.x86_64 1/1 验证中 : haproxy-1.5.18-7.el7.x86_64 1/1 已安装: haproxy.x86_64 0:1.5.18-7.el7 完毕!
3)启动haproxy服务[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 100% 3142 6.0MB/s 00:00
[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
步骤二:安装软件
1)在haproxy99主机安装keepalived软件
2)在haproxy98主机安装keepalived软件[root@haproxy99 ~]# yum -y install keepalived.x86_64 已安装: keepalived.x86_64 0:1.3.5-6.el7 作为依赖被安装: lm_sensors-libs.x86_64 0:3.4.0-4.20160601gitf9185e5.el7 net-snmp-agent-libs.x86_64 1:5.7.2-32.el7 net-snmp-libs.x86_64 1:5.7.2-32.el7
[root@haproxy98 ~]# yum -y install keepalived.x86_64 已安装: keepalived.x86_64 0:1.3.5-6.el7 作为依赖被安装: lm_sensors-libs.x86_64 0:3.4.0-4.20160601gitf9185e5.el7 net-snmp-agent-libs.x86_64 1:5.7.2-32.el7 net-snmp-libs.x86_64 1:5.7.2-32.el7 [root@haproxy98 ~]# 完毕!
步骤三:修改配置文件
1)修改haproxy99主机的配置文件
2)修改haproxy98主机的配置文件[root@haproxy99 ~]# sed -i '36,$d' /etc/keepalived/keepalived.conf //删除无关的配置行 [root@haproxy99 ~]#vim /etc/keepalived/keepalived.conf global_defs { …… …… vrrp_iptables //禁止iptables } vrrp_instance VI_1 { state MASTER //主服务器标识 interface eth0 virtual_router_id 51 priority 150 //haproxy99 主机做主服务器,优先级要比 haproxy88主机高 advert_int 1 authentication { 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主机的密码
[root@haproxy98 ~]#vim /etc/keepalived/keepalived.conf global_defs { …… …… vrrp_iptables //禁止iptables } vrrp_instance VI_1 { state BACKUP //备用服务器标识 interface eth0 virtual_router_id 51 priority 100 //优先级要比 haproxy99低 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.4.100 //定义vip地址 } }
步骤四:启动服务
1)在haproxy99主机启动keepalived服务
2)在haproxy98主机启动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
[root@haproxy98 ~]# systemctl start keepalived.service //启动服务 [root@haproxy98 ~]# [root@haproxy98 ~]# ip addr show | grep 192.168.4.100 //查看不到vip
步骤五:测试配置
1)客户端连接vip地址,访问数据库服务
2)测试高可用[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 | +------------+ [root@web33 ~]# [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 | +------------+ | pxcnode10 | +------------+ [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 | +------------+ | pxcnode88 | +------------+
[root@haproxy99 ~]# ip addr show | grep 192.168.4.100 //在haproxy99 主机查看VIP地址 inet 192.168.4.100/32 scope global eth0 [root@haproxy99 ~]# [root@haproxy99 ~]# systemctl stop keepalived.service //停止keepalived服务 [root@haproxy99 ~]# [root@haproxy99 ~]# [root@haproxy99 ~]# ip addr show | grep 192.168.4.100 //查看不到vip地址 [root@haproxy99 ~]# [root@haproxy98 ~]# ip addr show | grep 192.168.4.100 //在备用的haproxy98主机查看地址 inet 192.168.4.100/32 scope global eth0 [root@haproxy98 ~]# //客户端连接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 | +------------+ [root@web33 ~]# [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 | +------------+ | pxcnode10 | +------------+ [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 | +------------+ | pxcnode88 | +------------+