Top

NSD PROJECT2 DAY02

  1. 案例1:升级网站运行平台
  2. 案例2:部署内存存储服务
  3. 案例3:数据迁移
  4. 案例4:部署LB集群
  5. 案例5:部署HA集群

1 案例1:升级网站运行平台

1.1 问题

具体配置如下:

  1. 清除当前配置
  2. 部署LNMP
  3. 测试配置

1.2 步骤

实现此案例需要按照如下步骤进行。

步骤一:清除当前配置

1)停止网站服务

  1. [root@web33 ~]# /usr/local/tomcat/bin/shutdown.sh
  2. [root@web33 ~]# vim /etc/rc.local
  3. #/usr/local/tomcat/bin/startup.sh
  4. :wq

2)卸载共享存储

  1. [root@web33 ~]# umount /usr/local/tomcat/webapps/ROOT //卸载当前挂载
  2. [root@web33 ~]# vim /etc/fstab //清除开机挂载
  3. #192.168.4.30:/sitedir /usr/local/tomcat/webapps/ROOT nfs defaults 0 0
  4. :wq

步骤二:部署LNMP

1)安装软件

  1. [root@web33 ~]# yum -y install gcc zlib-devel pcre-devel //安装源码Nginx依赖软件
  2. 已安装:
  3. 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
  4.  
  5. 作为依赖被安装:
  6. 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
  7. mpfr.x86_64 0:3.1.1-4.el7
  8.  
  9. 完毕!
  10. [root@web33 ~]#
  11. [root@web33 ~]# tar -zxvf nginx-1.12.2.tar.gz //解压
  12. [root@web33 ~]# cd nginx-1.12.2 //进源码目录
  13. [root@web33 nginx-1.12.2]# ./configure //配置
  14. ......
  15. Configuration summary
  16. + using system PCRE library
  17. + OpenSSL library is not used
  18. + using system zlib library
  19.  
  20. nginx path prefix: "/usr/local/nginx"
  21. nginx binary file: "/usr/local/nginx/sbin/nginx"
  22. nginx modules path: "/usr/local/nginx/modules"
  23. nginx configuration prefix: "/usr/local/nginx/conf"
  24. nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  25. nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  26. nginx error log file: "/usr/local/nginx/logs/error.log"
  27. nginx http access log file: "/usr/local/nginx/logs/access.log"
  28. nginx http client request body temporary files: "client_body_temp"
  29. nginx http proxy temporary files: "proxy_temp"
  30. nginx http fastcgi temporary files: "fastcgi_temp"
  31. nginx http uwsgi temporary files: "uwsgi_temp"
  32. nginx http scgi temporary files: "scgi_temp"
  33.  
  34. [root@web33 nginx-1.12.2]# make //编译
  35. ……
  36. ……
  37. sed -e "s|%%PREFIX%%|/usr/local/nginx|" \
  38.     -e "s|%%PID_PATH%%|/usr/local/nginx/logs/nginx.pid|" \
  39.     -e "s|%%CONF_PATH%%|/usr/local/nginx/conf/nginx.conf|" \
  40.     -e "s|%%ERROR_LOG_PATH%%|/usr/local/nginx/logs/error.log|" \
  41.     < man/nginx.8 > objs/nginx.8
  42. make[1]: 离开目录“/root/nginx-1.12.2
  43. [root@web33 nginx-1.12.2]#
  44. [root@web33 nginx-1.12.2]# make install //安装
  45. ……
  46. ……
  47. cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf.default'
  48. test -d '/usr/local/nginx/logs' \
  49.     || mkdir -p '/usr/local/nginx/logs'
  50. test -d '/usr/local/nginx/logs' \
  51.     || mkdir -p '/usr/local/nginx/logs'
  52. test -d '/usr/local/nginx/html' \
  53.     || cp -R html '/usr/local/nginx'
  54. test -d '/usr/local/nginx/logs' \
  55.     || mkdir -p '/usr/local/nginx/logs'
  56. make[1]: 离开目录“/root/nginx-1.12.2
  57. [root@web33 nginx-1.12.2]#
  58. [root@web33 nginx-1.12.2]# ls /usr/local/nginx //查看安装目录
  59. conf html logs sbin
  60. [root@web33 nginx-1.12.2]#
  61.  
  62. [root@web33 ~]# yum -y install php-fpm //安装php-fpm 软件
  63. ……
  64. ……
  65. 已安装:
  66. php-fpm.x86_64 0:5.4.16-45.el7
  67.  
  68. 作为依赖被安装:
  69. libzip.x86_64 0:0.10.1-8.el7 php-common.x86_64 0:5.4.16-45.el7
  70.  
  71. 完毕!
  72. [root@web33 ~]# yum -y install php php-mysql //安装php 及 php-mysql 软件
  73. ……
  74. ……
  75. 已安装:
  76. php.x86_64 0:5.4.16-45.el7 php-mysql.x86_64 0:5.4.16-45.el7
  77.  
  78. 作为依赖被安装:
  79. 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
  80.  
  81. 完毕!
  82. [root@web33 ~]#

2)挂载共享存储

  1. [root@web33 ~]# vim /etc/fstab //开机挂载
  2. 192.168.4.30:/sitedir /usr/local/nginx/html nfs defaults 0 0
  3. :wq
  4. [root@web33 ~]# mount -a //挂载设备
  5. [root@web33 ~]# mount | grep "/usr/local/nginx/html" //查看挂载
  6. 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)
  7. [root@web33 ~]#

3)启动服务

  1. [root@web33 ~]# vim +65 /usr/local/nginx/conf/nginx.conf //修改主配置文件
  2. location ~ \.php$ {
  3. root html;
  4. fastcgi_pass 127.0.0.1:9000;
  5. fastcgi_index index.php;
  6. #fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  7. include fastcgi.conf;
  8. }
  9. :wq
  10. [root@web33 ~]# /usr/local/nginx/sbin/nginx //启动服务
  11. [root@web33 ~]#
  12. [root@web33 ~]# netstat -utnlp | grep :80 //查看端口
  13. tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 26335/nginx: master
  14. [root@web33 ~]#
  15. [root@web33 ~]# systemctl start php-fpm
  16. [root@web33 ~]#
  17. [root@web33 ~]# netstat -utnlp | grep :9000
  18. tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 26345/php-fpm: mast
  19. [root@web33 ~]#

步骤三:测试配置

1)编写PHP脚本

  1. [root@nfs30 ~]# vim /sitedir/linkdb2.php //在nfs30共享目录编写php脚本文件
  2. <?php
  3. $conn=mysql_connect("192.168.4.11","yaya99","123qqq...A");
  4. mysql_select_db("gamedb");
  5. $sql = 'insert into user (name) values ("PLJ")';
  6. mysql_query($sql);
  7. mysql_close();
  8. echo "save data ok";
  9. ?>
  10. :wq

2)访问脚本

  1. [root@client50 ~]# curl http://192.168.4.33/linkdb2.php //访问web33服务器
  2. save data ok
  3. [root@client50 ~]#

3)查看数据

  1. [root@mysql11 ~]# mysql –uyaya99 -p123qqq...A -e 'select * from gamedb.user where name="PLJ"' //在数据库服务器查看数据
  2. mysql: [Warning] Using a password on the command line interface can be insecure.
  3. +------+
  4. | name |
  5. +------+
  6. | PLJ |
  7. +------+
  8. [root@mysql11 ~]#

2 案例2:部署内存存储服务

2.1 问题

具体操作如下:

  1. 部署redis服务器
  2. 创建redis集群
  3. 配置网站服务器
  4. 测试配置

2.2 方案

克隆7台虚拟机配置要求如图-1所示。

图-1

2.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:部署redis服务器(6台都要配置)

1)搭建redis服务器

  1. [root@redisA ~]# rpm -q gcc || yum -y install gcc //安装编译工具
  2. [root@redisA ~]# tar -zxvf redis-4.0.8.tar.gz //解压
  3. [root@redisA ~]# cd redis-4.0.8/ //进源码目录
  4. [root@redisA redis-4.0.8]# make install //安装软件
  5. ……
  6. ……
  7. INSTALL install
  8. INSTALL install
  9. INSTALL install
  10. INSTALL install
  11. INSTALL install
  12. make[1]: 离开目录“/root/redis-4.0.8/src”
  13. [root@redisA redis-4.0.8]#
  14. [root@redisA redis-4.0.8]# ./utils/install_server.sh //初始化配置
  15. Welcome to the redis service installer
  16. This script will help you easily set up a running redis server
  17.  
  18. Please select the redis port for this instance: [6379] //端口号
  19. Selecting default: 6379
  20. Please select the redis config file name [/etc/redis/6379.conf] //主配置文件
  21. Selected default - /etc/redis/6379.conf
  22. Please select the redis log file name [/var/log/redis_6379.log] //日志文件
  23. Selected default - /var/log/redis_6379.log
  24. Please select the data directory for this instance [/var/lib/redis/6379] //数据库目录
  25. Selected default - /var/lib/redis/6379
  26. Please select the redis executable path [/usr/local/bin/redis-server] //服务启动启动程序
  27. Selected config: //配置总结
  28. Port : 6379
  29. Config file : /etc/redis/6379.conf
  30. Log file : /var/log/redis_6379.log
  31. Data dir : /var/lib/redis/6379
  32. Executable : /usr/local/bin/redis-server
  33. Cli Executable : /usr/local/bin/redis-cli
  34. Is this ok? Then press ENTER to go on or Ctrl-C to abort.
  35. Copied /tmp/6379.conf => /etc/init.d/redis_6379
  36. Installing service...
  37. Successfully added to chkconfig!
  38. Successfully added to runlevels 345!
  39. Starting Redis server... //服务启动提示
  40. Installation successful! //安装完成提示
  41. [root@redisA redis-4.0.8]#
  42. [root@redisA redis-4.0.8]# /etc/init.d/redis_6379 stop //停止服务
  43. Stopping ...
  44. Redis stopped
  45. [root@redisA redis-4.0.8]#
  46. [root@redisA redis-4.0.8]# vim /etc/redis/6379.conf //修改配置文件,启用集群配置
  47. 70 bind 192.168.4.51
  48. 815 cluster-enabled yes
  49. 823 cluster-config-file nodes-6379.conf
  50. 829 cluster-node-timeout 5000
  51. :wq
  52.  
  53. [root@redisA redis-4.0.8]# /etc/init.d/redis_6379 start //启动服务
  54. Starting Redis server...
  55. [root@redisA redis-4.0.8]# netstat -utnlp | grep redis-server //查看端口
  56. tcp 0 0 192.168.4.51:6379 0.0.0.0:* LISTEN 29720/redis-server //redis服务端口
  57. tcp 0 0 192.168.4.51:16379 0.0.0.0:* LISTEN 29720/redis-server //集群端口
  58.  
  59. [root@redisB ~]# rpm -q gcc || yum -y install gcc //安装编译工具
  60. [root@redisB ~]# tar -zxvf redis-4.0.8.tar.gz //解压
  61. [root@redisB ~]# cd redis-4.0.8/ //进源码目录
  62. [root@redisB redis-4.0.8]# make install //安装软件
  63. ……
  64. ……
  65. INSTALL install
  66. INSTALL install
  67. INSTALL install
  68. INSTALL install
  69. INSTALL install
  70. make[1]: 离开目录“/root/redis-4.0.8/src”
  71. [root@redisB redis-4.0.8]#
  72. [root@redisB redis-4.0.8]# ./utils/install_server.sh //初始化配置
  73. Welcome to the redis service installer
  74. This script will help you easily set up a running redis server
  75.  
  76. Please select the redis port for this instance: [6379] //端口号
  77. Selecting default: 6379
  78. Please select the redis config file name [/etc/redis/6379.conf] //主配置文件
  79. Selected default - /etc/redis/6379.conf
  80. Please select the redis log file name [/var/log/redis_6379.log] //日志文件
  81. Selected default - /var/log/redis_6379.log
  82. Please select the data directory for this instance [/var/lib/redis/6379] //数据库目录
  83. Selected default - /var/lib/redis/6379
  84. Please select the redis executable path [/usr/local/bin/redis-server] //服务启动启动程序
  85. Selected config: //配置总结
  86. Port : 6379
  87. Config file : /etc/redis/6379.conf
  88. Log file : /var/log/redis_6379.log
  89. Data dir : /var/lib/redis/6379
  90. Executable : /usr/local/bin/redis-server
  91. Cli Executable : /usr/local/bin/redis-cli
  92. Is this ok? Then press ENTER to go on or Ctrl-C to abort.
  93. Copied /tmp/6379.conf => /etc/init.d/redis_6379
  94. Installing service...
  95. Successfully added to chkconfig!
  96. Successfully added to runlevels 345!
  97. Starting Redis server... //服务启动提示
  98. Installation successful! //安装完成提示
  99. [root@redisB redis-4.0.8]#
  100. [root@redisB redis-4.0.8]# /etc/init.d/redis_6379 stop //停止服务
  101. Stopping ...
  102. Redis stopped
  103. [root@redisB redis-4.0.8]#
  104. [root@redisB redis-4.0.8]# vim /etc/redis/6379.conf //修改配置文件,启用集群配置
  105. 70 bind 192.168.4.52
  106. 815 cluster-enabled yes
  107. 823 cluster-config-file nodes-6379.conf
  108. 829 cluster-node-timeout 5000
  109. :wq
  110.  
  111. [root@redisB redis-4.0.8]# /etc/init.d/redis_6379 start //启动服务
  112. Starting Redis server...
  113. [root@redisB redis-4.0.8]# netstat -utnlp | grep redis-server //查看端口
  114. tcp 0 0 192.168.4.52:6379 0.0.0.0:* LISTEN 29720/redis-server //redis服务端口
  115. tcp 0 0 192.168.4.52:16379 0.0.0.0:* LISTEN 29720/redis-server //集群端口
  116.  
  117. [root@redisC ~]# rpm -q gcc || yum -y install gcc //安装编译工具
  118. [root@redisC ~]# tar -zxvf redis-4.0.8.tar.gz //解压
  119. [root@redisC ~]# cd redis-4.0.8/ //进源码目录
  120. [root@redisC redis-4.0.8]# make install //安装软件
  121. ……
  122. ……
  123. INSTALL install
  124. INSTALL install
  125. INSTALL install
  126. INSTALL install
  127. INSTALL install
  128. make[1]: 离开目录“/root/redis-4.0.8/src”
  129. [root@redisC redis-4.0.8]#
  130. [root@redisC redis-4.0.8]# ./utils/install_server.sh //初始化配置
  131. Welcome to the redis service installer
  132. This script will help you easily set up a running redis server
  133.  
  134. Please select the redis port for this instance: [6379] //端口号
  135. Selecting default: 6379
  136. Please select the redis config file name [/etc/redis/6379.conf] //主配置文件
  137. Selected default - /etc/redis/6379.conf
  138. Please select the redis log file name [/var/log/redis_6379.log] //日志文件
  139. Selected default - /var/log/redis_6379.log
  140. Please select the data directory for this instance [/var/lib/redis/6379] //数据库目录
  141. Selected default - /var/lib/redis/6379
  142. Please select the redis executable path [/usr/local/bin/redis-server] //服务启动启动程序
  143. Selected config: //配置总结
  144. Port : 6379
  145. Config file : /etc/redis/6379.conf
  146. Log file : /var/log/redis_6379.log
  147. Data dir : /var/lib/redis/6379
  148. Executable : /usr/local/bin/redis-server
  149. Cli Executable : /usr/local/bin/redis-cli
  150. Is this ok? Then press ENTER to go on or Ctrl-C to abort.
  151. Copied /tmp/6379.conf => /etc/init.d/redis_6379
  152. Installing service...
  153. Successfully added to chkconfig!
  154. Successfully added to runlevels 345!
  155. Starting Redis server... //服务启动提示
  156. Installation successful! //安装完成提示
  157. [root@redisC redis-4.0.8]#
  158. [root@redisC redis-4.0.8]# /etc/init.d/redis_6379 stop //停止服务
  159. Stopping ...
  160. Redis stopped
  161. [root@redisC redis-4.0.8]#
  162. [root@redisC redis-4.0.8]# vim /etc/redis/6379.conf //修改配置文件,启用集群配置
  163. 70 bind 192.168.4.53
  164. 815 cluster-enabled yes
  165. 823 cluster-config-file nodes-6379.conf
  166. 829 cluster-node-timeout 5000
  167. :wq
  168.  
  169. [root@redisC redis-4.0.8]# /etc/init.d/redis_6379 start //启动服务
  170. Starting Redis server...
  171. [root@redisC redis-4.0.8]# netstat -utnlp | grep redis-server //查看端口
  172. tcp 0 0 192.168.4.53:6379 0.0.0.0:* LISTEN 29720/redis-server //redis服务端口
  173. tcp 0 0 192.168.4.53:16379 0.0.0.0:* LISTEN 29720/redis-server //集群端口
  174.  
  175. [root@redisD ~]# rpm -q gcc || yum -y install gcc //安装编译工具
  176. [root@redisD ~]# tar -zxvf redis-4.0.8.tar.gz //解压
  177. [root@redisD ~]# cd redis-4.0.8/ //进源码目录
  178. [root@redisD redis-4.0.8]# make install //安装软件
  179. ……
  180. ……
  181. INSTALL install
  182. INSTALL install
  183. INSTALL install
  184. INSTALL install
  185. INSTALL install
  186. make[1]: 离开目录“/root/redis-4.0.8/src”
  187. [root@redisD redis-4.0.8]#
  188. [root@redisD redis-4.0.8]# ./utils/install_server.sh //初始化配置
  189. Welcome to the redis service installer
  190. This script will help you easily set up a running redis server
  191.  
  192. Please select the redis port for this instance: [6379] //端口号
  193. Selecting default: 6379
  194. Please select the redis config file name [/etc/redis/6379.conf] //主配置文件
  195. Selected default - /etc/redis/6379.conf
  196. Please select the redis log file name [/var/log/redis_6379.log] //日志文件
  197. Selected default - /var/log/redis_6379.log
  198. Please select the data directory for this instance [/var/lib/redis/6379] //数据库目录
  199. Selected default - /var/lib/redis/6379
  200. Please select the redis executable path [/usr/local/bin/redis-server] //服务启动启动程序
  201. Selected config: //配置总结
  202. Port : 6379
  203. Config file : /etc/redis/6379.conf
  204. Log file : /var/log/redis_6379.log
  205. Data dir : /var/lib/redis/6379
  206. Executable : /usr/local/bin/redis-server
  207. Cli Executable : /usr/local/bin/redis-cli
  208. Is this ok? Then press ENTER to go on or Ctrl-C to abort.
  209. Copied /tmp/6379.conf => /etc/init.d/redis_6379
  210. Installing service...
  211. Successfully added to chkconfig!
  212. Successfully added to runlevels 345!
  213. Starting Redis server... //服务启动提示
  214. Installation successful! //安装完成提示
  215. [root@redisD redis-4.0.8]#
  216. [root@redisD redis-4.0.8]# /etc/init.d/redis_6379 stop //停止服务
  217. Stopping ...
  218. Redis stopped
  219. [root@redisD redis-4.0.8]#
  220. [root@redisD redis-4.0.8]# vim /etc/redis/6379.conf //修改配置文件,启用集群配置
  221. 70 bind 192.168.4.54
  222. 815 cluster-enabled yes
  223. 823 cluster-config-file nodes-6379.conf
  224. 829 cluster-node-timeout 5000
  225. :wq
  226.  
  227. [root@redisD redis-4.0.8]# /etc/init.d/redis_6379 start //启动服务
  228. Starting Redis server...
  229. [root@redisD redis-4.0.8]# netstat -utnlp | grep redis-server //查看端口
  230. tcp 0 0 192.168.4.54:6379 0.0.0.0:* LISTEN 29720/redis-server //redis服务端口
  231. tcp 0 0 192.168.4.54:16379 0.0.0.0:* LISTEN 29720/redis-server //集群端口
  232.  
  233. [root@redisE ~]# rpm -q gcc || yum -y install gcc //安装编译工具
  234. [root@redisE ~]# tar -zxvf redis-4.0.8.tar.gz //解压
  235. [root@redisE ~]# cd redis-4.0.8/ //进源码目录
  236. [root@redisE redis-4.0.8]# make install //安装软件
  237. ……
  238. ……
  239. INSTALL install
  240. INSTALL install
  241. INSTALL install
  242. INSTALL install
  243. INSTALL install
  244. make[1]: 离开目录“/root/redis-4.0.8/src”
  245. [root@redisE redis-4.0.8]#
  246. [root@redisE redis-4.0.8]# ./utils/install_server.sh //初始化配置
  247. Welcome to the redis service installer
  248. This script will help you easily set up a running redis server
  249.  
  250. Please select the redis port for this instance: [6379] //端口号
  251. Selecting default: 6379
  252. Please select the redis config file name [/etc/redis/6379.conf] //主配置文件
  253. Selected default - /etc/redis/6379.conf
  254. Please select the redis log file name [/var/log/redis_6379.log] //日志文件
  255. Selected default - /var/log/redis_6379.log
  256. Please select the data directory for this instance [/var/lib/redis/6379] //数据库目录
  257. Selected default - /var/lib/redis/6379
  258. Please select the redis executable path [/usr/local/bin/redis-server] //服务启动启动程序
  259. Selected config: //配置总结
  260. Port : 6379
  261. Config file : /etc/redis/6379.conf
  262. Log file : /var/log/redis_6379.log
  263. Data dir : /var/lib/redis/6379
  264. Executable : /usr/local/bin/redis-server
  265. Cli Executable : /usr/local/bin/redis-cli
  266. Is this ok? Then press ENTER to go on or Ctrl-C to abort.
  267. Copied /tmp/6379.conf => /etc/init.d/redis_6379
  268. Installing service...
  269. Successfully added to chkconfig!
  270. Successfully added to runlevels 345!
  271. Starting Redis server... //服务启动提示
  272. Installation successful! //安装完成提示
  273. [root@redisE redis-4.0.8]#
  274. [root@redisE redis-4.0.8]# /etc/init.d/redis_6379 stop //停止服务
  275. Stopping ...
  276. Redis stopped
  277. [root@redisE redis-4.0.8]#
  278. [root@redisE redis-4.0.8]# vim /etc/redis/6379.conf //修改配置文件,启用集群配置
  279. 70 bind 192.168.4.55
  280. 815 cluster-enabled yes
  281. 823 cluster-config-file nodes-6379.conf
  282. 829 cluster-node-timeout 5000
  283. :wq
  284.  
  285. [root@redisE redis-4.0.8]# /etc/init.d/redis_6379 start //启动服务
  286. Starting Redis server...
  287. [root@redisE redis-4.0.8]# netstat -utnlp | grep redis-server //查看端口
  288. tcp 0 0 192.168.4.55:6379 0.0.0.0:* LISTEN 29720/redis-server //redis服务端口
  289. tcp 0 0 192.168.4.55:16379 0.0.0.0:* LISTEN 29720/redis-server //集群端口
  290. [root@redisF ~]# rpm -q gcc || yum -y install gcc //安装编译工具
  291. [root@redisF ~]# tar -zxvf redis-4.0.8.tar.gz //解压
  292. [root@redisF ~]# cd redis-4.0.8/ //进源码目录
  293. [root@redisF redis-4.0.8]# make install //安装软件
  294. ……
  295. ……
  296. INSTALL install
  297. INSTALL install
  298. INSTALL install
  299. INSTALL install
  300. INSTALL install
  301. make[1]: 离开目录“/root/redis-4.0.8/src”
  302. [root@redisF redis-4.0.8]#
  303. [root@redisF redis-4.0.8]# ./utils/install_server.sh //初始化配置
  304. Welcome to the redis service installer
  305. This script will help you easily set up a running redis server
  306.  
  307. Please select the redis port for this instance: [6379] //端口号
  308. Selecting default: 6379
  309. Please select the redis config file name [/etc/redis/6379.conf] //主配置文件
  310. Selected default - /etc/redis/6379.conf
  311. Please select the redis log file name [/var/log/redis_6379.log] //日志文件
  312. Selected default - /var/log/redis_6379.log
  313. Please select the data directory for this instance [/var/lib/redis/6379] //数据库目录
  314. Selected default - /var/lib/redis/6379
  315. Please select the redis executable path [/usr/local/bin/redis-server] //服务启动启动程序
  316. Selected config: //配置总结
  317. Port : 6379
  318. Config file : /etc/redis/6379.conf
  319. Log file : /var/log/redis_6379.log
  320. Data dir : /var/lib/redis/6379
  321. Executable : /usr/local/bin/redis-server
  322. Cli Executable : /usr/local/bin/redis-cli
  323. Is this ok? Then press ENTER to go on or Ctrl-C to abort.
  324. Copied /tmp/6379.conf => /etc/init.d/redis_6379
  325. Installing service...
  326. Successfully added to chkconfig!
  327. Successfully added to runlevels 345!
  328. Starting Redis server... //服务启动提示
  329. Installation successful! //安装完成提示
  330. [root@redisF redis-4.0.8]#
  331. [root@redisF redis-4.0.8]# /etc/init.d/redis_6379 stop //停止服务
  332. Stopping ...
  333. Redis stopped
  334. [root@redisF redis-4.0.8]#
  335. [root@redisF redis-4.0.8]# vim /etc/redis/6379.conf //修改配置文件,启用集群配置
  336. 70 bind 192.168.4.56
  337. 815 cluster-enabled yes
  338. 823 cluster-config-file nodes-6379.conf
  339. 829 cluster-node-timeout 5000
  340. :wq
  341.  
  342. [root@redisF redis-4.0.8]# /etc/init.d/redis_6379 start //启动服务
  343. Starting Redis server...
  344. [root@redisF redis-4.0.8]# netstat -utnlp | grep redis-server //查看端口
  345. tcp 0 0 192.168.4.56:6379 0.0.0.0:* LISTEN 29720/redis-server //redis服务端口
  346. tcp 0 0 192.168.4.56:16379 0.0.0.0:* LISTEN 29720/redis-server //集群端口

步骤二:创建redis集群

1)配置管理主机

  1. [root@mgm ~]# yum -y install ruby rubygems //安装依赖
  2. ……
  3. ……
  4. 已安装:
  5. ruby.x86_64 0:2.0.0.648-33.el7_4 rubygems.noarch 0:2.0.14.1-33.el7_4
  6.  
  7. 作为依赖被安装:
  8. 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
  9. 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
  10.  
  11. 完毕!
  12. [root@mgm ~]#
  13. [root@mgm ~]# gem install redis-3.2.1.gem //安装依赖软件gem程序
  14. Successfully installed redis-3.2.1
  15. Parsing documentation for redis-3.2.1
  16. Installing ri documentation for redis-3.2.1
  17. 1 gem installed
  18. [root@mgm ~]# mkdir /root/bin
  19. [root@mgm ~]# tar -zxvf redis-4.0.8.tar.gz
  20. [root@mgm ~]# cp redis-4.0.8/src/redis-trib.rb /root/bin/ //拷贝脚本
  21. [root@mgm ~]#
  22. [root@mgm ~]# chmod +x /root/bin/redis-trib.rb //确保脚本有执行权限
  23. [root@mgm ~]#
  24. [root@mgm ~]# redis-trib.rb help //查看帮助
  25. Usage: redis-trib <command> <options> <arguments ...>
  26.  
  27. create host1:port1 ... hostN:portN
  28. --replicas <arg>
  29. check host:port
  30. info host:port
  31. fix host:port
  32. --timeout <arg>
  33. reshard host:port
  34. --from <arg>
  35. --to <arg>
  36. --slots <arg>
  37. --yes
  38. --timeout <arg>
  39. --pipeline <arg>
  40. rebalance host:port
  41. --weight <arg>
  42. --auto-weights
  43. --use-empty-masters
  44. --timeout <arg>
  45. --simulate
  46. --pipeline <arg>
  47. --threshold <arg>
  48. add-node new_host:new_port existing_host:existing_port
  49. --slave
  50. --master-id <arg>
  51. del-node host:port node_id
  52. set-timeout host:port milliseconds
  53. call host:port command arg arg .. arg
  54. import host:port
  55. --from <arg>
  56. --copy
  57. --replace
  58. help (show this help)
  59.  
  60. For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.
  61. [root@mgm ~]#

2)创建集群

  1. ]# redis-trib.rb create --replicas 1 \
  2. 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
  3.  
  4. >>> Performing hash slots allocation on 6 nodes...
  5. Using 3 masters:
  6. 192.168.4.51:6379
  7. 192.168.4.52:6379
  8. 192.168.4.53:6379
  9. Adding replica 192.168.4.56:6379 to 192.168.4.51:6379
  10. Adding replica 192.168.4.55:6379 to 192.168.4.52:6379
  11. Adding replica 192.168.4.54:6379 to 192.168.4.53:6379
  12. M: d9f8fe6d6d9dd391be8e7904501db1535e4d17cb 192.168.4.51:6379
  13. slots:0-5460 (5461 slots) master
  14. M: 324e05df3f143ef97e50d09be0328a695e655986 192.168.4.52:6379
  15. slots:5461-10922 (5462 slots) master
  16. M: 9e44139cffb8ebd7ed746aabbf4bcea9bf207645 192.168.4.53:6379
  17. slots:10923-16383 (5461 slots) master
  18. S: d9634ba0aa5c1a07193da4a013da6051c1515922 192.168.4.54:6379
  19. replicates 9e44139cffb8ebd7ed746aabbf4bcea9bf207645
  20. S: 2d343a9df48f6f6e207949e980ef498466a44dad 192.168.4.56:6379
  21. replicates d9f8fe6d6d9dd391be8e7904501db1535e4d17cb
  22. S: 894dd0008053f6fb65e9e4a36b755d9351607500 192.168.4.55:6379
  23. replicates 324e05df3f143ef97e50d09be0328a695e655986
  24. Can I set the above configuration? (type 'yes' to accept): yes //同意以上配置
  25. >>> Nodes configuration updated
  26. >>> Assign a different config epoch to each node
  27. >>> Sending CLUSTER MEET messages to join the cluster
  28. Waiting for the cluster to join...
  29. >>> Performing Cluster Check (using node 192.168.4.51:6379)
  30. M: d9f8fe6d6d9dd391be8e7904501db1535e4d17cb 192.168.4.51:6379
  31. slots:0-5460 (5461 slots) master
  32. 1 additional replica(s)
  33. S: d9634ba0aa5c1a07193da4a013da6051c1515922 192.168.4.54:6379
  34. slots: (0 slots) slave
  35. replicates 9e44139cffb8ebd7ed746aabbf4bcea9bf207645
  36. S: 894dd0008053f6fb65e9e4a36b755d9351607500 192.168.4.56:6379
  37. slots: (0 slots) slave
  38. replicates 324e05df3f143ef97e50d09be0328a695e655986
  39. M: 324e05df3f143ef97e50d09be0328a695e655986 192.168.4.52:6379
  40. slots:5461-10922 (5462 slots) master
  41. 1 additional replica(s)
  42. M: 9e44139cffb8ebd7ed746aabbf4bcea9bf207645 192.168.4.53:6379
  43. slots:10923-16383 (5461 slots) master
  44. 1 additional replica(s)
  45. S: 2d343a9df48f6f6e207949e980ef498466a44dad 192.168.4.57:6379
  46. slots: (0 slots) slave
  47. replicates d9f8fe6d6d9dd391be8e7904501db1535e4d17cb
  48. [OK] All nodes agree about slots configuration.
  49. >>> Check for open slots...
  50. >>> Check slots coverage...
  51. [OK] All 16384 slots covered. //提示16384个槽分配完毕
  52. [root@mgm ~]#

3)查看集群信息

  1. [root@mgm ~]# redis-trib.rb info 192.168.4.51:6379 //查看集群信息
  2. 192.168.4.51:6379 (d9f8fe6d...) -> 0 keys | 5461 slots | 1 slaves.
  3. 192.168.4.52:6379 (324e05df...) -> 0 keys | 5462 slots | 1 slaves.
  4. 192.168.4.53:6379 (9e44139c...) -> 0 keys | 5461 slots | 1 slaves.
  5. [OK] 0 keys in 3 masters.
  6. keys per slot on average

[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)测试配置(在客户端连接集群中的任意一台服务器存取数据)

  1. [root@client50 ~]# redis-cli -c -h 192.168.4.51 -p 6379 //连接服务器51
  2. 192.168.4.51:6379>
  3. 192.168.4.51:6379> set x 100 //存储
  4. -> Redirected to slot [16287] located at 192.168.4.53:6379 //提示存储在53主机
  5. OK
  6. 192.168.4.53:6379> keys *
  7. 1) "x"
  8. 192.168.4.53:6379>
  9. 192.168.4.53:6379> set y 200
  10. OK
  11. 192.168.4.53:6379> keys *
  12. 1) "y"
  13. 2) "x"
  14. 192.168.4.53:6379> set z 300 //存储
  15. -> Redirected to slot [8157] located at 192.168.4.52:6379 //提示存储在52主机
  16. OK
  17. 192.168.4.52:6379> keys * //在52主机查看数据 只有变量z
  18. 1) "z"
  19. 192.168.4.52:6379> get x
  20. -> Redirected to slot [16287] located at 192.168.4.53:6379 //连接53主机获取数据
  21. "100"
  22. 192.168.4.53:6379> keys *
  23. 1) "y"
  24. 2) "x"
  25. 192.168.4.53:6379> get z
  26. -> Redirected to slot [8157] located at 192.168.4.52:6379
  27. "300"
  28. 192.168.4.52:6379> set i 400
  29. -> Redirected to slot [15759] located at 192.168.4.53:6379
  30. OK
  31. 192.168.4.53:6379> set j 500
  32. -> Redirected to slot [3564] located at 192.168.4.51:6379
  33. OK
  34. 192.168.4.51:6379>

步骤三:配置网站服务器

  1. [root@web33 ~ ]# yum -y install php-devel //安装依赖
  2. ……
  3. ……
  4. 已安装:
  5. php-devel.x86_64 0:5.4.16-45.el7
  6.  
  7. 作为依赖被安装:
  8. 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
  9.  
  10. 完毕!
  11. [root@web33 ~]#
  12. [root@web33 ~]# tar -zxvf redis-cluster-4.3.0.tgz //解压
  13. [root@web33 ~]# cd redis-4.3.0/ //进入源码目录
  14. [root@web33 redis-4.3.0]# phpize //创建configure命令及配置信息文件/usr/bin/php-config
  15. Configuring for:
  16. PHP Api Version: 20100412
  17. Zend Module Api No: 20100525
  18. Zend Extension Api No: 220100525
  19. [root@web33 redis-4.3.0]#
  20. [root@web33 redis-4.3.0]# ./configure --with-php-config=/usr/bin/php-config
  21. ……
  22. ……
  23. configure: creating config.status
  24. config.status: creating config.h
  25. config.status: config.h is unchanged
  26. config.status: executing libtool commands
  27. [root@web33 redis-4.3.0]#
  28.  
  29. [root@web33 redis-4.3.0]# make //编译
  30. ……
  31. ……
  32. Build complete.
  33. Don't forget to run 'make test'.
  34.  
  35. [root@web33 redis-4.3.0]#
  36. [root@web33 redis-4.3.0]# make install //安装
  37. Installing shared extensions: /usr/lib64/php/modules/ //提示模块安装目录
  38. [root@web33 redis-4.3.0]#
  39. [root@web33 redis-4.3.0]# ls /usr/lib64/php/modules/ //查看目录列表
  40. 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
  41. [root@web33 redis-4.3.0]#

修改配置文件

  1. [root@web33 redis-4.3.0]# vim /etc/php.ini
  2. 728 extension_dir = "/usr/lib64/php/modules/" //模块目录
  3. 730 extension = "redis.so" //模块名
  4. :wq
  5. [root@web33 redis-4.3.0]# systemctl restart php-fpm //重启php-fpm服务
  6. [root@web33 redis-4.3.0]# php -m | grep -i redis //查看模块
  7. redis
  8. [root@web33 redis-4.3.0]#

步骤四:测试配置

1)在存储服务器共享目录下,创建连接集群PHP脚本

  1. nfs30~ ]# vim /sitedir/set_data.php //存储数据脚本
  2. <?php
  3. $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服务器列表
  4. $client = new RedisCluster(NUll,$redis_list); //定义连接redis服务器变量
  5. $client->set("i","tarenaA "); //存储数据 变量名 i
  6. $client->set("j","tarenaB "); //存储数据 变量名 j
  7. $client->set("k","tarenaC "); //存储数据 变量名 k
  8. ?>
  9. :wq
  10. nfs30~ ]# vim /sitedir/get_data.php //获取数据脚本
  11. <?php
  12. $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服务器列表
  13. $client = new RedisCluster(NUll,$redis_list); //定义连接redis服务器变量
  14. echo $client->get("i"); //获取变量i 的数据
  15. echo $client->get("j"); //获取变量j 的数据
  16. echo $client->get("k"); //获取变量k 的数据
  17. ?>
  18. :wq
  19. nfs30~ ]# vim /sitedir/test3.php //存/取数据脚本
  20. <?php
  21. $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'];
  22. $client = new RedisCluster(NUll,$redis_list);
  23. $client->set(“name“,”panglijing”); //存数据
  24. echo $client->get(“name”); //取数据
  25. ?>
  26. :wq

2)访问网站执行脚本(在任意主机访问网站服务器都可以)

  1. ]# curl http://192.168.4.33/set_data.php
  2. ]# curl http://192.168.4.33/get_data.php
  3. ]# curl http://192.168.4.33/test3.php

3)命令行连接任意一台redis服务器查看数据(在任意主机连接redis服务器都可以)

  1. ]# redis-cli -c -h 192.168.4.51 -p 6379
  2. 192.168.4.51:6379> keys *
  3. 1) i
  4. 192.168.4.51:6379> exit
  5. ]# redis-cli -c -h 192.168.4.52 -p 6379
  6. 192.168.4.52:6379> keys *
  7. 1) j
  8. 192.168.4.52:6379> exit
  9. ]# redis-cli -c -h 192.168.4.53 -p 6379
  10. 192.168.4.52:6379> keys *
  11. 1) k
  12. 192.168.4.52:6379> exit

3 案例3:数据迁移

3.1 问题

要求如下:

  1. 配置从服务器
  2. 配置第1台PXC服务器
  3. 配置第2台PXC服务器
  4. 配置第3台PXC服务器
  5. 公共配置
  6. 测试配置

3.2 方案

创建3台新的虚拟机,具体配置要求如图-2所示。

图-2

3.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:配置从服务器(把主机192.168.4.66 配置为192.168.4.11的从服务器)

1)在192.168.4.66主机安装数据库服务软件并启动mysqld服务

  1. [root@pxcnode66 ~]# tar -xvf mysql-5.7.17.tar //解包
  2. mysql-community-client-5.7.17-1.el7.x86_64.rpm
  3. mysql-community-common-5.7.17-1.el7.x86_64.rpm
  4. mysql-community-devel-5.7.17-1.el7.x86_64.rpm
  5. mysql-community-embedded-5.7.17-1.el7.x86_64.rpm
  6. mysql-community-embedded-compat-5.7.17-1.el7.x86_64.rpm
  7. mysql-community-embedded-devel-5.7.17-1.el7.x86_64.rpm
  8. mysql-community-libs-5.7.17-1.el7.x86_64.rpm
  9. mysql-community-libs-compat-5.7.17-1.el7.x86_64.rpm
  10. mysql-community-minimal-debuginfo-5.7.17-1.el7.x86_64.rpm
  11. mysql-community-server-5.7.17-1.el7.x86_64.rpm
  12. mysql-community-test-5.7.17-1.el7.x86_64.rpm
  13. [root@pxcnode66 ~]#
  14.  
  15. [root@pxcnode66 ~]# ls *.rpm //查看软件列表
  16. mysql-community-client-5.7.17-1.el7.x86_64.rpm
  17. mysql-community-common-5.7.17-1.el7.x86_64.rpm
  18. mysql-community-devel-5.7.17-1.el7.x86_64.rpm
  19. mysql-community-embedded-5.7.17-1.el7.x86_64.rpm
  20. mysql-community-embedded-compat-5.7.17-1.el7.x86_64.rpm
  21. mysql-community-embedded-devel-5.7.17-1.el7.x86_64.rpm
  22. mysql-community-libs-5.7.17-1.el7.x86_64.rpm
  23. mysql-community-libs-compat-5.7.17-1.el7.x86_64.rpm
  24. mysql-community-minimal-debuginfo-5.7.17-1.el7.x86_64.rpm
  25. mysql-community-server-5.7.17-1.el7.x86_64.rpm
  26. mysql-community-test-5.7.17-1.el7.x86_64.rpm
  27. [root@pxcnode66 ~]#
  28.  
  29. [root@pxcnode66 ~]# yum -y install mysql-community-*.rpm //安装软件
  30. 已加载插件:fastestmirror
  31. 正在检查 mysql-community-client-5.7.17-1.el7.x86_64.rpm: mysql-community-client-5.7.17-1.el7.x86_64
  32. mysql-community-client-5.7.17-1.el7.x86_64.rpm 将被安装
  33. ……
  34. ……
  35. 已安装:
  36. mysql-community-client.x86_64 0:5.7.17-1.el7 mysql-community-common.x86_64 0:5.7.17-1.el7
  37. mysql-community-devel.x86_64 0:5.7.17-1.el7 mysql-community-embedded.x86_64 0:5.7.17-1.el7
  38. mysql-community-embedded-compat.x86_64 0:5.7.17-1.el7 mysql-community-embedded-devel.x86_64 0:5.7.17-1.el7
  39. mysql-community-libs.x86_64 0:5.7.17-1.el7 mysql-community-libs-compat.x86_64 0:5.7.17-1.el7
  40. mysql-community-minimal-debuginfo.x86_64 0:5.7.17-1.el7 mysql-community-server.x86_64 0:5.7.17-1.el7
  41. mysql-community-test.x86_64 0:5.7.17-1.el7
  42.  
  43. 作为依赖被安装:
  44. perl-Data-Dumper.x86_64 0:2.145-3.el7 perl-JSON.noarch 0:2.59-2.el7
  45.  
  46. 完毕!
  47. [root@pxcnode66 ~]#
  48.  
  49. [root@pxcnode66 ~]# systemctl start mysqld //启动服务
  50.  
  51. [root@pxcnode66 ~]# ls /var/lib/mysql //查看数据库文件列表
  52. auto.cnf client-cert.pem ibdata1 ibtmp1 mysql.sock.lock public_key.pem sys
  53. ca-key.pem client-key.pem ib_logfile0 mysql performance_schema server-cert.pem
  54. ca.pem ib_buffer_pool ib_logfile1 mysql.sock private_key.pem server-key.pem
  55.  
  56. [root@pxcnode66 ~]# systemctl enable mysqld //设置服务开机运行
  57.  
  58. [root@pxcnode66 ~]# netstat -utnlp | grep :3306 //查看端口
  59. tcp6 0 0 :::3306 :::* LISTEN 1531/mysqld
  60. [root@pxcnode66 ~]#
  61.  
  62. [root@pxcnode66 ~]# grep password /var/log/mysqld.log //查看初始密码
  63. 2019-07-05T01:56:51.895852Z 1 [Note] A temporary password is generated for root@localhost: bB0*uCmu:.Kj
  64.  
  65. [root@pxcnode66 ~]# mysql -uroot -p'bB0*uCmu:.Kj' //初始密码登录
  66. mysql: [Warning] Using a password on the command line interface can be insecure.
  67. Welcome to the MySQL monitor. Commands end with ; or \g.
  68. Your MySQL connection id is 3
  69. Server version: 5.7.17
  70.  
  71. Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
  72.  
  73. Oracle is a registered trademark of Oracle Corporation and/or its
  74. affiliates. Other names may be trademarks of their respective
  75. owners.
  76.  
  77. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  78.  
  79. mysql>
  80. mysql> alter user root@"localhost" identified by "123qqq...A";//修改登录密码
  81. Query OK, 0 rows affected (0.01 sec)
  82. mysql>
  83. mysql> exit //断开连接
  84. Bye
  85. [root@pxcnode66 ~]# mysql -uroot -p123qqq...A //新密码登录
  86. mysql: [Warning] Using a password on the command line interface can be insecure.
  87. Welcome to the MySQL monitor. Commands end with ; or \g.
  88. Your MySQL connection id is 4
  89. Server version: 5.7.17 MySQL Community Server (GPL)
  90.  
  91. Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
  92.  
  93. Oracle is a registered trademark of Oracle Corporation and/or its
  94. affiliates. Other names may be trademarks of their respective
  95. owners.
  96.  
  97. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  98.  
  99. mysql> show databases; //查看数据库
  100. +--------------------+
  101. | Database |
  102. +--------------------+
  103. | information_schema |
  104. | mysql |
  105. | performance_schema |
  106. | sys |
  107. +--------------------+
  108. 4 rows in set (0.00 sec)
  109. Mysql>

2)修改服务主配置文件

  1. [root@pxcnode66 ~]# vim /etc/my.cnf
  2. [mysqld]
  3. server_id=66 //指定server_id
  4. :wq
  5. [root@pxcnode66 ~]# systemctl restart mysqld //重启服务
  6. [root@pxcnode66 ~]#

3)确保数据一致 (pxcnode66主机 使用mysql11主机的完全备份恢复数据确保数据一致 )

  1. [root@mysql11 ~]# rpm -ivh libev-4.15-1.el6.rf.x86_64.rpm //安装依赖软件
  2. [root@mysql11 ~]# yum -y install percona-xtrabackup-24-2.4.7-1.el7.x86_64.rpm //安装在线热备软件
  3. [root@mysql11 ~]# innobackupex --user root --password 123qqq...A --slave-info /allbak --no-timestamp //备份所有数据,并记录备份数据对应的binlog日志名
  4.  
  5. [root@mysql11 ~]# scp -r /allbak root@192.168.4.66:/root/ //把备份文件发送给pxcnode66主机
  6.  
  7.  
  8. [root@pxcnode66 ~]# rpm -ivh libev-4.15-1.el6.rf.x86_64.rpm //安装依赖软件
  9. [root@pxcnode66 ~]# yum -y install percona-xtrabackup-24-2.4.13-1.el7.x86_64.rpm //安装在线热备软件
  10. [root@pxcnode66 ~]# systemctl stop mysqld //停止服务
  11. [root@pxcnode66 ~]# rm -rf /var/lib/mysql/* //清空数据库目录
  12. [root@pxcnode66 ~]# innobackupex --apply-log /root/allbak/ //准备恢复数据
  13. [root@pxcnode66 ~]# innobackupex --copy-back /root/allbak/ //恢复数据
  14. [root@pxcnode66 ~]# chown -R mysql:mysql /var/lib/mysql //修改所有者
  15. [root@pxcnode66 ~]# systemctl start mysqld //启动服务

4)指定主服务器

  1. [root@pxcnode66 ~]# cat /root/allbak/xtrabackup_info | grep master11 //查binlog日志
  2. binlog_pos = filename 'master11.000001', position '7700'
  3.  
  4. [root@pxcnode66 ~]# mysql -uroot -p123qqq...A //管理员登录指定主服务器信息
  5. mysql> change master to
  6. master_host="192.168.4.11", //主服务器ip地址
  7. master_user="repluser", //主服务器授权用户
  8. master_password="123qqq...A", //授权密码
  9. master_log_file="master11.000001", //binlog日志名
  10. master_log_pos=7700; //日志偏移量
  11. Query OK, 0 rows affected, 2 warnings (0.31 sec)
  12.  
  13. mysql> start slave ; //启动slave 程序
  14. Query OK, 0 rows affected (0.09 sec)
  15. mysql> exit //断开连接
  16. Bye
  17. [root@pxcnode66 ~]#
  18.  
  19. [root@pxcnode66 ~]# mysql -uroot -p123qqq...A -e "show slave status\G" | grep -i 192.168.4.11 //查看主服务器地址
  20. mysql: [Warning] Using a password on the command line interface can be insecure.
  21. Master_Host: 192.168.4.11 //主服务器ip地址
  22.  
  23. [root@pxcnode66 ~]# mysql -uroot -p123qqq...A -e "show slave status\G" | grep -i "yes" //查看状态信息
  24. mysql: [Warning] Using a password on the command line interface can be insecure.
  25. Slave_IO_Running: Yes //IO线程正常
  26. Slave_SQL_Running: Yes //SQL线程正常
  27. [root@pxcnode66 ~]#

步骤二:配置第1台PXC服务器(192.168.4.66)

1)停止mysqld服务、卸载mysqld服务软件

  1. [root@pxcnode66 ~]# systemctl stop mysqld //停止服务
  2. [root@pxnode66 ~]# rpm -qa | grep -i mysql //查看安装的MySQL服务软件
  3. mysql-community-server-5.7.17-1.el7.x86_64
  4. mysql-community-embedded-compat-5.7.17-1.el7.x86_64
  5. mysql-community-common-5.7.17-1.el7.x86_64
  6. mysql-community-client-5.7.17-1.el7.x86_64
  7. mysql-community-devel-5.7.17-1.el7.x86_64
  8. mysql-community-test-5.7.17-1.el7.x86_64
  9. mysql-community-libs-compat-5.7.17-1.el7.x86_64
  10. mysql-community-minimal-debuginfo-5.7.17-1.el7.x86_64
  11. perl-DBD-MySQL-4.023-6.el7.x86_64
  12. mysql-community-libs-5.7.17-1.el7.x86_64
  13. mysql-community-embedded-5.7.17-1.el7.x86_64
  14. mysql-community-embedded-devel-5.7.17-1.el7.x86_64
  15. [root@pxcnode66 ~]#
  16.  
  17. [root@pxcnode66 ~]# rpm -e --nodeps mysql-community-server mysql-community-embedded-compat mysql-community-common mysql-community-client mysql-community-devel \
  18. > mysql-community-test mysql-community-libs-compat mysql-community-minimal-debuginfo mysql-community-libs mysql-community-embedded mysql-community-embedded-devel //卸载所有的MySQL服务软件
  19. 警告:/etc/my.cnf 已另存为 /etc/my.cnf.rpmsave
  20. [root@pxcnode66 ~]#

2)安装PXC软件、修改配置文件、启动mysql服务

  1. [root@pxcnode66 ~]# cd PXC //进软件目录
  2. [root@pxcnode66 PXC]# rpm -ivh qpress-1.1-14.11.x86_64.rpm //安装依赖
  3. 警告:qpress-1.1-14.11.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 6cb7b81f: NOKEY
  4. 准备中... ################################# [100%]
  5. 正在升级/安装...
  6. 1:qpress-1.1-14.11 ################################# [100%]
  7. [root@pxcnode66 PXC]#
  8.  
  9. [root@pxcnode66 PXC]# tar -xvf Percona-XtraDB-Cluster-5.7.25-31.35-r463-el7-x86_64-bundle.tar //解压PXC软件包
  10. Percona-XtraDB-Cluster-57-5.7.25-31.35.1.el7.x86_64.rpm
  11. Percona-XtraDB-Cluster-57-debuginfo-5.7.25-31.35.1.el7.x86_64.rpm
  12. Percona-XtraDB-Cluster-client-57-5.7.25-31.35.1.el7.x86_64.rpm
  13. Percona-XtraDB-Cluster-devel-57-5.7.25-31.35.1.el7.x86_64.rpm
  14. Percona-XtraDB-Cluster-full-57-5.7.25-31.35.1.el7.x86_64.rpm
  15. Percona-XtraDB-Cluster-garbd-57-5.7.25-31.35.1.el7.x86_64.rpm
  16. Percona-XtraDB-Cluster-server-57-5.7.25-31.35.1.el7.x86_64.rpm
  17. Percona-XtraDB-Cluster-shared-57-5.7.25-31.35.1.el7.x86_64.rpm
  18. Percona-XtraDB-Cluster-shared-compat-57-5.7.25-31.35.1.el7.x86_64.rpm
  19. Percona-XtraDB-Cluster-test-57-5.7.25-31.35.1.el7.x86_64.rpm
  20. [root@pxcnode66 PXC]#
  21.  
  22. [root@pxcnode66 PXC]# yum -y install Percona-XtraDB-Cluster-*.rpm //安装软件
  23. 已安装:
  24. 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
  25. 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
  26. 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
  27. 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
  28. 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
  29.  
  30. 作为依赖被安装:
  31. 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
  32. 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
  33. 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
  34. zlib-devel.x86_64 0:1.2.7-17.el7
  35.  
  36. 完毕!
  37. [root@pxcnode66 PXC]#
  38.  
  39. [root@pxcnode66 PXC]# vim /etc/percona-xtradb-cluster.conf.d/mysqld.cnf //修改数据库服务配置文件
  40. [mysqld]
  41. server-id=66 //指定server_id
  42. :wq
  43. [root@pxcnode66 PXC]#
  44. [root@pxcnode66 PXC]# vim /etc/percona-xtradb-cluster.conf.d/wsrep.cnf //修改集群服务配置文件
  45. wsrep_cluster_address=gcomm://     不需要写ip地址
  46. wsrep_node_address=192.168.4.66 //指定本机Ip地址
  47. wsrep_cluster_name=pxc-cluster //指定集群名称(另外2台的集群名称要于此相同)
  48. wsrep_node_name=pxcnode66 //指定本机主机名
  49. wsrep_sst_auth="sstuser:123qqq...A" //数据全量同步授权用户及密码
  50. :wq
  51. [root@pxcnode66 PXC]#
  52. [root@pxcnode66 PXC]# systemctl start mysql //启动服务
  53.  
  54. [root@pxcnode66 PXC]# netstat -utnlp | grep :3306 //查看MySQL服务端口
  55. tcp6 0 0 :::3306 :::* LISTEN 24482/mysqld
  56.  
  57. [root@pxcnode66 PXC]# netstat -utnlp | grep :4567 //查看集群通信端口
  58. tcp 0 0 0.0.0.0:4567 0.0.0.0:* LISTEN 24472/mysqld
  59. [root@pxcnode66 PXC]# systemctl enable mysql //设置服务开机运行
  60. [root@pxcnode66 PXC]#

3)数据库管理员登录、用户授权、查看状态信息

  1. [root@pxcnode66 PXC]# mysql -uroot -p123qqq...A //管理员登录
  2.  
  3. mysql> grant all on *.* to sstuser@"localhost" identified by "123qqq...A"; //用户授权
  4. Query OK, 0 rows affected, 1 warning (0.10 sec)
  5.  
  6. mysql> show status like "%wsrep%"; //查看集群状态信息
  7. | wsrep_incoming_addresses | 192.168.4.66:3306 |
  8. | wsrep_cluster_weight | 1 |
  9. | wsrep_desync_count | 0 |
  10. | wsrep_evs_delayed | |
  11. | wsrep_evs_evict_list | |
  12. | wsrep_evs_repl_latency | 0/0/0/0/0 |
  13. | wsrep_evs_state | OPERATIONAL |
  14. | wsrep_gcomm_uuid | 73809cc5-cf00-11e9-aac3-b223959fecdf |
  15. | wsrep_cluster_conf_id | 1 |
  16. | wsrep_cluster_size | 1 |
  17. | wsrep_cluster_state_uuid | 73848b1a-cf00-11e9-9058-36c1ac1e1359 |
  18. | wsrep_cluster_status | Primary |
  19. | wsrep_connected | ON |
  20. | wsrep_local_bf_aborts | 0 |
  21. | wsrep_local_index | 0 |
  22. | wsrep_provider_name | Galera |
  23. | wsrep_provider_vendor | Codership Oy <info@codership.com> |
  24. | wsrep_provider_version | 3.35(rddf9876) |
  25. | wsrep_ready | ON |
  26. +----------------------------------+--------------------------------------+
  27. 71 rows in set (0.00 sec)
  28. mysql> exit ;
  29. [root@pxcnode66 ~]#
  30.  
  31. [root@pxcnode66 ~]# mysql -uroot -p123qqq...A -e "show slave status\G" | grep -i "yes" //查看状态信息依然是192.168.4.11的从服务器
  32. mysql: [Warning] Using a password on the command line interface can be insecure.
  33. Slave_IO_Running: Yes //IO线程正常
  34. Slave_SQL_Running: Yes //SQL线程正常
  35. [root@pxcnode66 ~]#

步骤三:配置第2台PXC服务器(192.168.4.10)

1)安装PXC软件

  1. [root@pxcnode10 ~]# cd PXC //进软件目录
  2. [root@pxcnode10 PXC]# rpm -ivh qpress-1.1-14.11.x86_64.rpm //安装依赖
  3. 警告:qpress-1.1-14.11.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 6cb7b81f: NOKEY
  4. 准备中... ################################# [100%]
  5. 正在升级/安装...
  6. 1:qpress-1.1-14.11 ################################# [100%]
  7. [root@pxcnode10 PXC]#
  8.  
  9. [root@pxcnode10 PXC]# tar -xvf Percona-XtraDB-Cluster-5.7.25-31.35-r463-el7-x86_64-bundle.tar //解压PXC软件包
  10. Percona-XtraDB-Cluster-57-5.7.25-31.35.1.el7.x86_64.rpm
  11. Percona-XtraDB-Cluster-57-debuginfo-5.7.25-31.35.1.el7.x86_64.rpm
  12. Percona-XtraDB-Cluster-client-57-5.7.25-31.35.1.el7.x86_64.rpm
  13. Percona-XtraDB-Cluster-devel-57-5.7.25-31.35.1.el7.x86_64.rpm
  14. Percona-XtraDB-Cluster-full-57-5.7.25-31.35.1.el7.x86_64.rpm
  15. Percona-XtraDB-Cluster-garbd-57-5.7.25-31.35.1.el7.x86_64.rpm
  16. Percona-XtraDB-Cluster-server-57-5.7.25-31.35.1.el7.x86_64.rpm
  17. Percona-XtraDB-Cluster-shared-57-5.7.25-31.35.1.el7.x86_64.rpm
  18. Percona-XtraDB-Cluster-shared-compat-57-5.7.25-31.35.1.el7.x86_64.rpm
  19. Percona-XtraDB-Cluster-test-57-5.7.25-31.35.1.el7.x86_64.rpm
  20. [root@pxcnode10 PXC]#
  21.  
  22. [root@pxcnode10 PXC]# yum -y install Percona-XtraDB-Cluster-*.rpm //安装软件
  23. 已安装:
  24. 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
  25. 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
  26. 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
  27. 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
  28. 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
  29.  
  30. 作为依赖被安装:
  31. 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
  32. 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
  33. 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
  34. zlib-devel.x86_64 0:1.2.7-17.el7
  35.  
  36. 完毕!
  37. [root@pxcnode10 PXC]#

2)修改配置文件

  1. [root@pxcnode10 PXC]# vim /etc/percona-xtradb-cluster.conf.d/mysqld.cnf //修改数据库服务配置
  2. [mysqld]
  3. server-id=10 //指定server_id
  4. :wq
  5. [root@pxcnode10 PXC]#
  6. [root@pxcnode10 PXC]# vim /etc/percona-xtradb-cluster.conf.d/wsrep.cnf //修改集群服务配置文件
  7. wsrep_cluster_address=gcomm://192.168.4.66,192.168.4.10     //集群成员列表
  8. wsrep_node_address=192.168.4.10 //指定本机Ip地址
  9. wsrep_cluster_name=pxc-cluster //指定集群名称(另外2台的集群名称要于此相同)
  10. wsrep_node_name=pxcnode10 //指定本机主机名
  11. wsrep_sst_auth="sstuser:123qqq...A" //数据全量同步授权用户及密码
  12. :wq
  13. [root@pxcnode10 PXC]#

3)启动mysql服务

  1. [root@pxcnode10 PXC]# systemctl start mysql //启动服务
  2. [root@pxcnode10 PXC]# systemctl enable mysql //服务开机运行
  3. [root@pxcnode10 PXC]# netstat -utnlp | grep :3306 //查看MySQL服务端口
  4. tcp6 0 0 :::3306 :::* LISTEN 24482/mysqld
  5.  
  6. [root@pxcnode10 PXC]# netstat -utnlp | grep :4567 //查看集群端口
  7. tcp6 0 0 :::4567 :::* LISTEN 24489/mysqld
  8. [root@pxcnode10 PXC]#
  9.  
  10. mysql> show status like "%wsrep%"; //查看集群状态信息
  11. | wsrep_incoming_addresses | 192.168.4.66:3306192.168.4.10:3306|
  12. | wsrep_cluster_weight | 1 |
  13. | wsrep_desync_count | 0 |
  14. | wsrep_evs_delayed | |
  15. | wsrep_evs_evict_list | |
  16. | wsrep_evs_repl_latency | 0/0/0/0/0 |
  17. | wsrep_evs_state | OPERATIONAL |
  18. | wsrep_gcomm_uuid | 73809cc5-cf00-11e9-aac3-b223959fecdf |
  19. | wsrep_cluster_conf_id | 1 |
  20. | wsrep_cluster_size | 1 |
  21. | wsrep_cluster_state_uuid | 73848b1a-cf00-11e9-9058-36c1ac1e1359 |
  22. | wsrep_cluster_status | Primary |
  23. | wsrep_connected | ON |
  24. | wsrep_local_bf_aborts | 0 |
  25. | wsrep_local_index | 0 |
  26. | wsrep_provider_name | Galera |
  27. | wsrep_provider_vendor | Codership Oy <info@codership.com> |
  28. | wsrep_provider_version | 3.35(rddf9876) |
  29. | wsrep_ready | ON |
  30. +----------------------------------+--------------------------------------+
  31. 71 rows in set (0.00 sec)
  32. mysql> exit ;
  33. [root@pxcnode10 ~]#

步骤四:配置第3台PXC服务器(192.168.4.88)

1)安装PXC软件

  1. [root@pxcnode88 ~]# cd PXC //进软件目录
  2. [root@pxcnode88 PXC]# rpm -ivh qpress-1.1-14.11.x86_64.rpm //安装依赖
  3. 警告:qpress-1.1-14.11.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 6cb7b81f: NOKEY
  4. 准备中... ################################# [100%]
  5. 正在升级/安装...
  6. 1:qpress-1.1-14.11 ################################# [100%]
  7. [root@pxcnode88 PXC]#
  8.  
  9. [root@pxcnode88 PXC]# tar -xvf Percona-XtraDB-Cluster-5.7.25-31.35-r463-el7-x86_64-bundle.tar //解压PXC软件包
  10. Percona-XtraDB-Cluster-57-5.7.25-31.35.1.el7.x86_64.rpm
  11. Percona-XtraDB-Cluster-57-debuginfo-5.7.25-31.35.1.el7.x86_64.rpm
  12. Percona-XtraDB-Cluster-client-57-5.7.25-31.35.1.el7.x86_64.rpm
  13. Percona-XtraDB-Cluster-devel-57-5.7.25-31.35.1.el7.x86_64.rpm
  14. Percona-XtraDB-Cluster-full-57-5.7.25-31.35.1.el7.x86_64.rpm
  15. Percona-XtraDB-Cluster-garbd-57-5.7.25-31.35.1.el7.x86_64.rpm
  16. Percona-XtraDB-Cluster-server-57-5.7.25-31.35.1.el7.x86_64.rpm
  17. Percona-XtraDB-Cluster-shared-57-5.7.25-31.35.1.el7.x86_64.rpm
  18. Percona-XtraDB-Cluster-shared-compat-57-5.7.25-31.35.1.el7.x86_64.rpm
  19. Percona-XtraDB-Cluster-test-57-5.7.25-31.35.1.el7.x86_64.rpm
  20. [root@pxcnode88 PXC]#
  21.  
  22. [root@pxcnode88 PXC]# yum -y install Percona-XtraDB-Cluster-*.rpm //安装软件
  23. 已安装:
  24. 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
  25. 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
  26. 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
  27. 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
  28. 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
  29.  
  30. 作为依赖被安装:
  31. 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
  32. 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
  33. 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
  34. zlib-devel.x86_64 0:1.2.7-17.el7
  35.  
  36. 完毕!
  37. [root@pxcnode88 PXC]#

2)修改配置文件

  1. [root@pxcnode88 PXC]# vim /etc/percona-xtradb-cluster.conf.d/mysqld.cnf //修改数据库服务配置
  2. [mysqld]
  3. server-id=88 //指定server_id
  4. :wq
  5. [root@pxcnode88 PXC]#
  6. [root@pxcnode88 PXC]# vim /etc/percona-xtradb-cluster.conf.d/wsrep.cnf //修改集群服务配置文件
  7. wsrep_cluster_address=gcomm://192.168.4.66     //集群成员ip地址
  8. wsrep_node_address=192.168.4.88 //指定本机Ip地址
  9. wsrep_cluster_name=pxc-cluster //指定集群名称(另外2台的集群名称要于此相同)
  10. wsrep_node_name=pxcnode88 //指定本机主机名
  11. wsrep_sst_auth="sstuser:123qqq...A" //数据全量同步授权用户及密码
  12. :wq
  13. [root@pxcnode88 PXC]#

3)启动mysql服务

  1. [root@pxcnode88 PXC]# systemctl start mysql //启动服务
  2. [root@pxcnode88 PXC]# systemctl enable mysql //服务开机运行
  3. [root@pxcnode88 PXC]# netstat -utnlp | grep :3306 //查看MySQL服务端口
  4. tcp6 0 0 :::3306 :::* LISTEN 24472/mysqld
  5.  
  6. [root@pxcnode88 PXC]# netstat -utnlp | grep :4567 //查看集群端口
  7. tcp6 0 0 :::4567 :::* LISTEN 24486/mysqld
  8. [root@pxcnode88 PXC]#
  9. #

步骤五:公共配置(192.168.4.88、192.168.4.10、192.168.4.66)

1)修改192.168.4.88主机的集群配置文件

  1. [root@pxcnode88 ~]# vim /etc/percona-xtradb-cluster.conf.d/wsrep.cnf
  2. wsrep_cluster_address=gcomm://192.168.4.66,192.168.4.10,192.168.4.88 //指定集群成员列表
  3. :wq
  4. [root@pxcnode88 ~]#

2)修改192.168.4.10主机的集群配置文件

  1. [root@pxcnode10 ~]# vim /etc/percona-xtradb-cluster.conf.d/wsrep.cnf
  2. wsrep_cluster_address=gcomm://192.168.4.66,192.168.4.88,192.168.4.10 //指定集群成员列表
  3. :wq
  4. [root@pxcnode10 ~]#

3)修改192.168.4.66主机的集群配置文件

  1. [root@pxcnode66 ~]# vim /etc/percona-xtradb-cluster.conf.d/wsrep.cnf
  2. wsrep_cluster_address=gcomm://192.168.4.66,192.168.4.88,192.168.4.10 //指定集群成员列表
  3. :wq
  4. [root@pxcnode66 ~]#

步骤6:测试配置:在网站服务器连接PXC集群主机存取数据:

1)存储数据:在网站服务器连接PXC集群主机存储数据

  1. [root@web33 ~]# mysql -h192.168.4.66 -uyaya99 -p123qqq…A gamedb
  2. Mysql> insert into gamedb.user values (“pljA”);
  3. Mysql> exit ;
  4.  
  5. [root@web33 ~]# mysql -h192.168.4.10 -uyaya99 -p123qqq…A gamedb
  6. Mysql> insert into gamedb.user values (“pljB”);
  7. Mysql> exit ;
  8.  
  9. [root@web33 ~]# mysql -h192.168.4.88 -uyaya99 -p123qqq…A gamedb
  10. Mysql> insert into gamedb.user values (“pljC”);
  11. Mysql> exit ;

2)查询数据:在网站服务器连接PXC集群主机查询数据

  1. [root@web44 ~]# mysql -h192.168.4.66 -uyaya99 -p123qqq…A gamedb
  2. Mysql> select * from gamedb.user;
  3. +------+
  4. | name|
  5. +------+
  6. | pljA|
  7. | pljB|
  8. | pljC|
  9. +------+
  10. Mysql> exit ;
  11.  
  12. [root@web44 ~]# mysql -h192.168.4.10 -uyaya99 -p123qqq…A gamedb
  13. Mysql> select * from gamedb.user;
  14. +------+
  15. | name|
  16. +------+
  17. | pljA|
  18. | pljB|
  19. | pljC|
  20. +------+
  21. Mysql> exit ;Mysql> exit ;
  22.  
  23. [root@web44 ~]# mysql -h192.168.4.88 -uyaya99 -p123qqq…A gamedb
  24. Mysql> select * from gamedb.user;
  25. +------+
  26. | name|
  27. +------+
  28. | pljA|
  29. | pljB|
  30. | pljC|
  31. +------+
  32. Mysql> exit ;Mysql> exit ;

4 案例4:部署LB集群

4.1 问题

配置步骤如下:

  1. 安装软件
  2. 修改配置文件
  3. 启动服务
  4. 测试配置

4.2 方案

拓扑结构如图-3所示。创建1台新的虚拟机,配置ip地址 eth0 192.168.4.99 主机名 haproxy99 ;运行haproxy服务 接受客户端访问数据库的连接请求,把请求平均分发给3台PXC集群主机。

图-3

4.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:安装软件: 在haproxy99主机上安装haproxy软件

  1. [root@haproxy99 ~]# yum -y install haproxy
  2.     ……
  3. Running transaction
  4. 正在安装 : haproxy-1.5.18-7.el7.x86_64 1/1
  5. 验证中 : haproxy-1.5.18-7.el7.x86_64 1/1
  6.  
  7. 已安装:
  8. haproxy.x86_64 0:1.5.18-7.el7
  9.  
  10. 完毕!
  11. [root@haproxy99 ~]#

步骤二:修改配置文件

  1. [root@haproxy99 ~]# vim /etc/haproxy/haproxy.cfg
  2.  
  3. Global //全局配置默认即可
  4. log 127.0.0.1 local2
  5. chroot /var/lib/haproxy
  6. pidfile /var/run/haproxy.pid
  7. maxconn 4000
  8. user haproxy
  9. group haproxy
  10. daemon
  11. stats socket /var/lib/haproxy/stats
  12.  
  13. defaults //默认配置(不需要修改)
  14. mode http
  15. log global
  16. option httplog
  17. option dontlognull
  18. option http-server-close
  19. option forwardfor except 127.0.0.0/8
  20. option redispatch
  21. retries 3
  22. timeout http-request 10s
  23. timeout queue 1m
  24. timeout connect 10s
  25. timeout client 1m
  26. timeout server 1m
  27. timeout http-keep-alive 10s
  28. timeout check 10s
  29. maxconn 3000
  30.  
  31. listen status //定义监控页面
  32. mode http //模式为http
  33. bind *:80 //端口80
  34. stats enable //启用配置
  35. stats uri /admin //访问目录名
  36. stats auth admin:admin //登录用户与密码
  37.  
  38. listen mysql_3306 *:3306 //定义haproxy服务名称与端口号
  39. mode tcp //mysql服务 得使用 tcp 协议
  40. option tcpka //使用长连接
  41. balance roundrobin //调度算法
  42. server mysql_01 192.168.4.66:3306 check //第1台数据库服务器
  43. server mysql_02 192.168.4.10:3306 check //第2台数据库服务器
  44. server mysql_03 192.168.4.88:3306 check //第3台数据库服务器
  45. :wq
  46.  
  47. [root@haproxy99 haproxy]#

步骤三:启动服务

  1. [root@haproxy99 ~]# systemctl start haproxy //启动服务
  2.  
  3. [root@haproxy99 ~]# systemctl enable haproxy //开机运行
  4. Created symlink from /etc/systemd/system/multi-user.target.wants/haproxy.service to /usr/lib/systemd/system/haproxy.service.
  5.  
  6. [root@haproxy99 ~]# netstat -utnlp | grep :3306 //查看端口
  7. tcp6 0 0 :::3306 :::* LISTEN 29768/haproxy
  8. [root@haproxy99 ~]#

步骤四:测试配置:在网站服务器连接haproxy99主机访问数据

  1. [root@web33 ~]# mysql –h192.168.4.99 –uyaya99 –p123qqq…A -e 'select @@hostname'
  2. mysql: [Warning] Using a password on the command line interface can be insecure.
  3. +------------+
  4. | @@hostname |
  5. +------------+
  6. | pxcnode66 | //第1次连接
  7. +------------+
  8. [root@web33 ~]#
  9.  
  10. [root@web33 ~]# mysql –h192.168.4.99 –uyaya99 –p123qqq…A -e 'select @@hostname'
  11. mysql: [Warning] Using a password on the command line interface can be insecure.
  12. +------------+
  13. | @@hostname |
  14. +------------+
  15. | pxcnode10 | //第2次连接
  16. +------------+
  17. [root@web33 ~]#
  18.  
  19. [root@web33 ~]# mysql –h192.168.4.99 –uyaya99 –p123qqq…A -e 'select @@hostname'
  20. mysql: [Warning] Using a password on the command line interface can be insecure.
  21. +------------+
  22. | @@hostname |
  23. +------------+
  24. | pxcnode88 | //第3次连接
  25. +------------+
  26. [root@web33 ~]#

5 案例5:部署HA集群

5.1 问题

具体配置如下:

  1. 准备备用调度器主机
  2. 安装软件
  3. 修改配置文件
  4. 启动服务
  5. 测试配置

5.2 方案

拓扑结构如图-4所示。创建1台新的虚拟机,在eth0 接口配置ip地址为192.168.4.98做备用调度器。

图-4

5.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:准备备用调度器主机

  1. [root@haproxy98 ~]# yum -y install haproxy
  2. 正在安装 : haproxy-1.5.18-7.el7.x86_64 1/1
  3. 验证中 : haproxy-1.5.18-7.el7.x86_64 1/1
  4.  
  5. 已安装:
  6. haproxy.x86_64 0:1.5.18-7.el7
  7.  
  8. 完毕!
  9. [root@haproxy98 ~]#

2)修改haproxy98主机haproxy.conf文件(直接拷贝haproxy99主机的配置文件也可以)

  1. [root@haproxy98 ~]# scp root@192.168.4.99:/etc/haproxy/haproxy.cfg /etc/haproxy/
  2. Warning: Permanently added '192.168.4.99' (ECDSA) to the list of known hosts.
  3. root@192.168.4.99's password: //输入haproxy99主机的密码
  4. haproxy.cfg 100% 3142 6.0MB/s 00:00
  5. [root@haproxy98 ~]#

3)启动haproxy服务

  1. [root@haproxy98 ~]# systemctl start haproxy //启动服务
  2. [root@haproxy98 ~]# systemctl enable haproxy //服务开机运行
  3. Created symlink from /etc/systemd/system/multi-user.target.wants/haproxy.service to /usr/lib/systemd/system/haproxy.service.
  4.  
  5. [root@haproxy98 ~]# netstat -utnlp | grep :3306 //查看端口
  6. tcp6 0 0 :::3306 :::* LISTEN 29768/haproxy

步骤二:安装软件

1)在haproxy99主机安装keepalived软件

  1. [root@haproxy99 ~]# yum -y install keepalived.x86_64
  2. 已安装:
  3. keepalived.x86_64 0:1.3.5-6.el7
  4.  
  5. 作为依赖被安装:
  6. lm_sensors-libs.x86_64 0:3.4.0-4.20160601gitf9185e5.el7 net-snmp-agent-libs.x86_64 1:5.7.2-32.el7
  7. net-snmp-libs.x86_64 1:5.7.2-32.el7
  8. [root@haproxy99 ~]#

2)在haproxy98主机安装keepalived软件

  1. [root@haproxy98 ~]# yum -y install keepalived.x86_64
  2. 已安装:
  3. keepalived.x86_64 0:1.3.5-6.el7
  4.  
  5. 作为依赖被安装:
  6. lm_sensors-libs.x86_64 0:3.4.0-4.20160601gitf9185e5.el7 net-snmp-agent-libs.x86_64 1:5.7.2-32.el7
  7. net-snmp-libs.x86_64 1:5.7.2-32.el7
  8. [root@haproxy98 ~]#
  9. 完毕!
  10. [root@haproxy98 ~]#

步骤三:修改配置文件

1)修改haproxy99主机的配置文件

  1. [root@haproxy99 ~]# sed -i '36,$d' /etc/keepalived/keepalived.conf //删除无关的配置行
  2.  
  3. [root@haproxy99 ~]#vim /etc/keepalived/keepalived.conf
  4.  
  5. global_defs {
  6. ……
  7. ……
  8. vrrp_iptables //禁止iptables
  9. }
  10. vrrp_instance VI_1 {
  11. state MASTER //主服务器标识
  12. interface eth0
  13. virtual_router_id 51
  14. priority 150 //haproxy99 主机做主服务器,优先级要比 haproxy88主机高
  15. advert_int 1
  16. authentication {
  17. auth_type PASS //主备服务器连接方式
  18. auth_pass 1111 //连接密码
  19. }
  20. virtual_ipaddress {
  21. 192.168.4.100 //定义vip地址
  22. }
  23. }
  24. [root@haproxy99 ~]# scp /etc/keepalived/keepalived.conf root@192.168.4.98: /etc/keepalived/
  25. root@192.168.4.98's password: //输入haproxy98主机的密码

2)修改haproxy98主机的配置文件

  1. [root@haproxy98 ~]#vim /etc/keepalived/keepalived.conf
  2.  
  3. global_defs {
  4. ……
  5. ……
  6. vrrp_iptables //禁止iptables
  7. }
  8. vrrp_instance VI_1 {
  9. state BACKUP //备用服务器标识
  10. interface eth0
  11. virtual_router_id 51
  12. priority 100 //优先级要比 haproxy99低
  13. advert_int 1
  14. authentication {
  15. auth_type PASS
  16. auth_pass 1111
  17. }
  18. virtual_ipaddress {
  19. 192.168.4.100 //定义vip地址
  20. }
  21. }
  22. [root@haproxy98 ~]#

步骤四:启动服务

1)在haproxy99主机启动keepalived服务

  1. [root@haproxy99 ~]# systemctl start keepalived.service //启动服务
  2. [root@haproxy99 ~]#
  3. [root@haproxy99 ~]# ip addr show | grep 192.168.4.100 //查看vip地址
  4. inet 192.168.4.100/32 scope global eth0
  5. [root@haproxy99 ~]#

2)在haproxy98主机启动keepalived服务

  1. [root@haproxy98 ~]# systemctl start keepalived.service //启动服务
  2. [root@haproxy98 ~]#
  3. [root@haproxy98 ~]# ip addr show | grep 192.168.4.100 //查看不到vip
  4. [root@haproxy98 ~]#

步骤五:测试配置

1)客户端连接vip地址,访问数据库服务

  1. [root@web33 ~]# mysql -h192.168.4.100 –uyaya99 –p123qqq…A -e 'select @@hostname'
  2. mysql: [Warning] Using a password on the command line interface can be insecure.
  3. +------------+
  4. | @@hostname |
  5. +------------+
  6. | pxcnode66 |
  7. +------------+
  8. [root@web33 ~]#
  9. [root@web33 ~]# mysql -h192.168.4.100 –uyaya99 –p123qqq…A -e 'select @@hostname'
  10. mysql: [Warning] Using a password on the command line interface can be insecure.
  11. +------------+
  12. | @@hostname |
  13. +------------+
  14. | pxcnode10 |
  15. +------------+
  16. [root@web33 ~]# mysql -h192.168.4.100 –uyaya99 –p123qqq…A -e 'select @@hostname'
  17. mysql: [Warning] Using a password on the command line interface can be insecure.
  18. +------------+
  19. | @@hostname |
  20. +------------+
  21. | pxcnode88 |
  22. +------------+
  23. [root@web33 ~]#

2)测试高可用

  1. [root@haproxy99 ~]# ip addr show | grep 192.168.4.100 //在haproxy99 主机查看VIP地址
  2. inet 192.168.4.100/32 scope global eth0
  3. [root@haproxy99 ~]#
  4. [root@haproxy99 ~]# systemctl stop keepalived.service //停止keepalived服务
  5. [root@haproxy99 ~]#
  6. [root@haproxy99 ~]#
  7. [root@haproxy99 ~]# ip addr show | grep 192.168.4.100 //查看不到vip地址
  8. [root@haproxy99 ~]#
  9.  
  10. [root@haproxy98 ~]# ip addr show | grep 192.168.4.100 //在备用的haproxy98主机查看地址
  11. inet 192.168.4.100/32 scope global eth0
  12. [root@haproxy98 ~]#
  13.  
  14. //客户端连接vip地址访问数据库服务
  15. [root@web33 ~]# mysql -h192.168.4.100 –uyaya99 –p123qqq…A -e 'select @@hostname'
  16. mysql: [Warning] Using a password on the command line interface can be insecure.
  17. +------------+
  18. | @@hostname |
  19. +------------+
  20. | pxcnode66 |
  21. +------------+
  22. [root@web33 ~]#
  23. [root@web33 ~]# mysql -h192.168.4.100 –uyaya99 –p123qqq…A -e 'select @@hostname'
  24. mysql: [Warning] Using a password on the command line interface can be insecure.
  25. +------------+
  26. | @@hostname |
  27. +------------+
  28. | pxcnode10 |
  29. +------------+
  30. [root@web33 ~]# mysql -h192.168.4.100 –uyaya99 –p123qqq…A -e 'select @@hostname'
  31. mysql: [Warning] Using a password on the command line interface can be insecure.
  32. +------------+
  33. | @@hostname |
  34. +------------+
  35. | pxcnode88 |
  36. +------------+
  37. [root@web33 ~]#