主从读写分离项目

  1. 案例1:磁盘管理
  2. 案例2:配置数据库服务器
  3. 案例3:配置主从同步
  4. 案例4:配置读写分离服务
  5. 案例5:配置网站服务

1 案例1:磁盘管理

1.1 问题

具体配置如下:

  • 添加磁盘
  • 磁盘分区
  • 格式化

1.2 方案

分别给2台虚拟机添加1块磁盘(磁盘容量3G、5G 、10G 都可以)

1.3 步骤

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

步骤一:磁盘分区

1)配置mysql11 主机

  1. [root@mysql11 ~]# fdisk -l /dev/sdb //查看磁盘容量
  2. 磁盘 /dev/sdb:5368 MB, 5368709120 字节,10485760 个扇区
  3. Units = 扇区 of 1 * 512 = 512 bytes
  4. 扇区大小(逻辑/物理):512 字节 / 512 字节
  5. I/O 大小(最小/最佳):512 字节 / 512 字节
  6. [root@mysql11 ~]# fdisk /dev/sdb //磁盘分区
  7. 欢迎使用 fdisk (util-linux 2.23.2)。
  8. 更改将停留在内存中,直到您决定将更改写入磁盘。
  9. 使用写入命令前请三思。
  10. Device does not contain a recognized partition table
  11. 使用磁盘标识符 0x882d04b7 创建新的 DOS 磁盘标签。
  12. 命令(输入 m 获取帮助):n //创建分区
  13. Partition type:
  14. p primary (0 primary, 0 extended, 4 free)
  15. e extended
  16. Select (default p): p //创建主分区
  17. 分区号 (1-4,默认 1):1 //指定分区编号
  18. 起始 扇区 (2048-10485759,默认为 2048): //起始柱面数默认 回车即可
  19. 将使用默认值 2048
  20. Last 扇区, +扇区 or +size{K,M,G} (2048-10485759,默认为 10485759)://结束柱面数
  21. 默认 回车即可
  22. 分区 1 已设置为 Linux 类型,大小设为 5 GiB
  23. 命令(输入 m 获取帮助):w //保存退出
  24. The partition table has been altered!
  25. Calling ioctl() to re-read partition table.
  26. 正在同步磁盘。
  27. [root@mysql11 ~]#

2)查看分区

  1. [root@mysql11 ~]# fdisk -l /dev/sdb #查看分区
  2. 磁盘 /dev/sdb:5368 MB, 5368709120 字节,10485760 个扇区
  3. Units = 扇区 of 1 * 512 = 512 bytes
  4. 扇区大小(逻辑/物理):512 字节 / 512 字节
  5. I/O 大小(最小/最佳):512 字节 / 512 字节
  6. 磁盘标签类型:dos
  7. 磁盘标识符:0x882d04b7
  8. 设备 Boot Start End Blocks Id System
  9. /dev/sdb1 2048 10485759 5241856 83 Linux #分区编号sdb1
  10. [root@mysql11 ~]#

5)格式化

  1. [root@mysql11 ~]# mkfs.xfs /dev/sdb1 #格式化为 xfs 文件系统
  2. /dev/sdb1 isize=512 agcount=4, agsize=654848 blks
  3. = sectsz=512 attr=2, projid32bit=1
  4. = crc=1 finobt=0, sparse=0
  5. data = bsize=4096 blocks=2619392, imaxpct=25
  6. = sunit=0 swidth=0 blks
  7. naming =version 2 bsize=4096 ascii-ci=0 ftype=1
  8. log =internal log bsize=4096 blocks=2560, version=2
  9. = sectsz=512 sunit=0 blks, lazy-count=1
  10. realtime =none extsz=4096 blocks=0, rtextents=0
  11. [root@mysql11 ~]# blkid /dev/sdb1 #查看文件系统类型
  12. /dev/sdb1: UUID="3e06f58f-8793-4800-b444-676f5a07fe10" TYPE="xfs"
  13. [root@mysql11 ~]#

2)配置mysql22 主机

  1. [root@mysql22 ~]# fdisk -l /dev/sdb //查看磁盘容量
  2. 磁盘 /dev/sdb:5368 MB, 5368709120 字节,10485760 个扇区
  3. Units = 扇区 of 1 * 512 = 512 bytes
  4. 扇区大小(逻辑/物理):512 字节 / 512 字节
  5. I/O 大小(最小/最佳):512 字节 / 512 字节
  6. [root@mysql22 ~]# fdisk /dev/sdb //磁盘分区
  7. 欢迎使用 fdisk (util-linux 2.23.2)。
  8. 更改将停留在内存中,直到您决定将更改写入磁盘。
  9. 使用写入命令前请三思。
  10. Device does not contain a recognized partition table
  11. 使用磁盘标识符 0x882d04b7 创建新的 DOS 磁盘标签。
  12. 命令(输入 m 获取帮助):n //创建分区
  13. Partition type:
  14. p primary (0 primary, 0 extended, 4 free)
  15. e extended
  16. Select (default p): p //创建主分区
  17. 分区号 (1-4,默认 1):1 //指定分区编号
  18. 起始 扇区 (2048-10485759,默认为 2048): //起始柱面数默认 回车即可
  19. 将使用默认值 2048
  20. Last 扇区, +扇区 or +size{K,M,G} (2048-10485759,默认为 10485759)://结束柱面数
  21. 默认 回车即可
  22. 分区 1 已设置为 Linux 类型,大小设为 5 GiB
  23. 命令(输入 m 获取帮助):w //保存退出
  24. The partition table has been altered!
  25. Calling ioctl() to re-read partition table.
  26. 正在同步磁盘。
  27. [root@mysql22 ~]#

2)查看分区

  1. [root@mysql22 ~]# fdisk -l /dev/sdb #查看分区
  2. 磁盘 /dev/sdb:5368 MB, 5368709120 字节,10485760 个扇区
  3. Units = 扇区 of 1 * 512 = 512 bytes
  4. 扇区大小(逻辑/物理):512 字节 / 512 字节
  5. I/O 大小(最小/最佳):512 字节 / 512 字节
  6. 磁盘标签类型:dos
  7. 磁盘标识符:0x882d04b7
  8. 设备 Boot Start End Blocks Id System
  9. /dev/sdb1 2048 10485759 5241856 83 Linux #分区编号sdb1
  10. [root@mysql22 ~]#

5)格式化

  1. [root@mysql22 ~]# mkfs.xfs /dev/sdb1 #格式化为 xfs 文件系统
  2. /dev/sdb1 isize=512 agcount=4, agsize=654848 blks
  3. = sectsz=512 attr=2, projid32bit=1
  4. = crc=1 finobt=0, sparse=0
  5. data = bsize=4096 blocks=2619392, imaxpct=25
  6. = sunit=0 swidth=0 blks
  7. naming =version 2 bsize=4096 ascii-ci=0 ftype=1
  8. log =internal log bsize=4096 blocks=2560, version=2
  9. = sectsz=512 sunit=0 blks, lazy-count=1
  10. realtime =none extsz=4096 blocks=0, rtextents=0
  11. [root@mysql22 ~]# blkid /dev/sdb1 #查看文件系统类型
  12. /dev/sdb1: UUID="3e06f58f-8793-4800-b444-676f5a07fe10" TYPE="xfs"
  13. [root@mysql22 ~]#

2 案例2:配置数据库服务器

2.1 问题

具体操作如下:

  1. 安装MySQL软件
  2. 启动服务
  3. 管理员登录
  4. 挂载分区

2.2 步骤

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

步骤一:安装MySQL软件

1)配置MySQL11主机

  1. #解包
  2. [root@mysql11 ~]# tar -xf mysql-5.7.17.tar
  3. [root@mysql11 ~]# ls *.rpm
  4. mysql-community-client-5.7.17-1.el7.x86_64.rpm
  5. mysql-community-common-5.7.17-1.el7.x86_64.rpm
  6. mysql-community-devel-5.7.17-1.el7.x86_64.rpm
  7. mysql-community-embedded-5.7.17-1.el7.x86_64.rpm
  8. mysql-community-embedded-compat-5.7.17-1.el7.x86_64.rpm
  9. mysql-community-embedded-devel-5.7.17-1.el7.x86_64.rpm
  10. mysql-community-libs-5.7.17-1.el7.x86_64.rpm
  11. mysql-community-libs-compat-5.7.17-1.el7.x86_64.rpm
  12. mysql-community-minimal-debuginfo-5.7.17-1.el7.x86_64.rpm
  13. mysql-community-server-5.7.17-1.el7.x86_64.rpm
  14. mysql-community-test-5.7.17-1.el7.x86_64.rpm
  15. [root@mysql11 ~]#
  16. #安装软件(需要事先配好yum源)
  17. [root@mysql11 ~]# yum -y install mysql-community-*.rpm
  18. 已加载插件:fastestmirror
  19. 正在检查 mysql-community-client-5.7.17-1.el7.x86_64.rpm: mysql-community-client-5.7.17-1.el7.x86_64
  20. mysql-community-client-5.7.17-1.el7.x86_64.rpm 将被安装
  21. ……
  22. ……
  23. 已安装:
  24. mysql-community-client.x86_64 0:5.7.17-1.el7 mysql-community-common.x86_64 0:5.7.17-1.el7
  25. mysql-community-devel.x86_64 0:5.7.17-1.el7 mysql-community-embedded.x86_64 0:5.7.17-1.el7
  26. mysql-community-embedded-compat.x86_64 0:5.7.17-1.el7 mysql-community-embedded-devel.x86_64 0:5.7.17-1.el7
  27. mysql-community-libs.x86_64 0:5.7.17-1.el7 mysql-community-libs-compat.x86_64 0:5.7.17-1.el7
  28. mysql-community-minimal-debuginfo.x86_64 0:5.7.17-1.el7 mysql-community-server.x86_64 0:5.7.17-1.el7
  29. mysql-community-test.x86_64 0:5.7.17-1.el7
  30. 作为依赖被安装:
  31. perl-Data-Dumper.x86_64 0:2.145-3.el7 perl-JSON.noarch 0:2.59-2.el7
  32. 完毕!
  33. [root@mysql11 ~]#

2)配置MySQL22主机

  1. #解包
  2. [root@mysql22 ~]# tar -xf mysql-5.7.17.tar
  3. [root@mysql22 ~]# ls *.rpm
  4. mysql-community-client-5.7.17-1.el7.x86_64.rpm
  5. mysql-community-common-5.7.17-1.el7.x86_64.rpm
  6. mysql-community-devel-5.7.17-1.el7.x86_64.rpm
  7. mysql-community-embedded-5.7.17-1.el7.x86_64.rpm
  8. mysql-community-embedded-compat-5.7.17-1.el7.x86_64.rpm
  9. mysql-community-embedded-devel-5.7.17-1.el7.x86_64.rpm
  10. mysql-community-libs-5.7.17-1.el7.x86_64.rpm
  11. mysql-community-libs-compat-5.7.17-1.el7.x86_64.rpm
  12. mysql-community-minimal-debuginfo-5.7.17-1.el7.x86_64.rpm
  13. mysql-community-server-5.7.17-1.el7.x86_64.rpm
  14. mysql-community-test-5.7.17-1.el7.x86_64.rpm
  15. [root@mysql22 ~]#
  16. #安装软件
  17. [root@mysql22 ~]# yum -y install mysql-community-*.rpm
  18. 已加载插件:fastestmirror
  19. 正在检查 mysql-community-client-5.7.17-1.el7.x86_64.rpm: mysql-community-client-5.7.17-1.el7.x86_64
  20. mysql-community-client-5.7.17-1.el7.x86_64.rpm 将被安装
  21. ……
  22. ……
  23. 已安装:
  24. mysql-community-client.x86_64 0:5.7.17-1.el7 mysql-community-common.x86_64 0:5.7.17-1.el7
  25. mysql-community-devel.x86_64 0:5.7.17-1.el7 mysql-community-embedded.x86_64 0:5.7.17-1.el7
  26. mysql-community-embedded-compat.x86_64 0:5.7.17-1.el7 mysql-community-embedded-devel.x86_64 0:5.7.17-1.el7
  27. mysql-community-libs.x86_64 0:5.7.17-1.el7 mysql-community-libs-compat.x86_64 0:5.7.17-1.el7
  28. mysql-community-minimal-debuginfo.x86_64 0:5.7.17-1.el7 mysql-community-server.x86_64 0:5.7.17-1.el7
  29. mysql-community-test.x86_64 0:5.7.17-1.el7
  30. 作为依赖被安装:
  31. perl-Data-Dumper.x86_64 0:2.145-3.el7 perl-JSON.noarch 0:2.59-2.el7
  32. 完毕!
  33. [root@mysql22 ~]#

步骤二:启动服务

1)配置mysql11

  1. [root@mysql11 ~]# systemctl start mysqld
  2. [root@mysql11 ~]# systemctl enable mysqld
  3. [root@mysql11 ~]# netstat -utnlp | grep :3306
  4. tcp6 0 0 :::3306 :::* LISTEN 1531/mysqld

2)配置mysql22

  1. [root@mysql22 ~]# systemctl start mysqld
  2. [root@mysql22 ~]# systemctl enable mysqld
  3. [root@mysql22 ~]# netstat -utnlp | grep :3306
  4. tcp6 0 0 :::3306 :::* LISTEN 1731/mysqld

步骤三:管理员登录

1)配置mysql11

  1. #查看初始密码
  2. [root@mysql11 ~]# grep "password" /var/log/mysqld.log
  3. 2019-07-05T01:56:51.895852Z 1 [Note] A temporary password is generated for root@localhost: bB0*uCmu:.Kj
  4. [root@mysql11 ~]# mysql -uroot -p'bB0*uCmu:.Kj' //初始密码登录
  5. mysql: [Warning] Using a password on the command line interface can be insecure.
  6. mysql>
  7. #修改登录密码
  8. mysql> alter user root@"localhost" identified by "123qqq…A"; //修改密码
  9. Query OK, 0 rows affected (0.01 sec)
  10. mysql> exit
  11. Bye
  12. [root@mysql11 ~]#
  13. #新密码登录
  14. [root@mysql11 ~]# mysql -uroot -p123qqq…A
  15. mysql> show databases;
  16. +--------------------+
  17. | Database |
  18. +--------------------+
  19. | information_schema |
  20. | mysql |
  21. | performance_schema |
  22. | sys |
  23. +--------------------+
  24. 4 rows in set (0.00 sec)
  25. Mysql> create database gamedb; #创建存储数据的gamedb库

2)配置mysql22

  1. #查看初始密码
  2. [root@mysql22 ~]# grep "password" /var/log/mysqld.log
  3. 2019-07-05T01:56:51.895852Z 1 [Note] A temporary password is generated for root@localhost: cc90*uCmu:.jj
  4. [root@mysql22 ~]# mysql -uroot -p'cc90*uCmu:.jj' //初始密码登录
  5. mysql: [Warning] Using a password on the command line interface can be insecure.
  6. mysql>
  7. #修改登录密码
  8. mysql> alter user root@"localhost" identified by "123qqq…A"; //修改密码
  9. Query OK, 0 rows affected (0.01 sec)
  10. mysql> exit
  11. Bye
  12. [root@mysql22 ~]#
  13. #新密码登录
  14. [root@mysql22 ~]# mysql -uroot -p123qqq…A
  15. mysql> show databases;
  16. +--------------------+
  17. | Database |
  18. +--------------------+
  19. | information_schema |
  20. | mysql |
  21. | performance_schema |
  22. | sys |
  23. +--------------------+
  24. 4 rows in set (0.00 sec)
  25. Mysql> create database gamedb; #创建存储数据的gamedb库

步骤四:挂载分区

1)配置mysql11

  1. #设置开机挂载
  2. [root@mysql11 ~]# vim /etc/fstab
  3. /dev/sdb1 /var/lib/mysql/gamedb xfs defaults 0 0
  4. :wq
  5. [root@mysql11 ~]#
  6. #加载没有挂载的设备
  7. [root@mysql11 ~]# mount -a
  8. [root@mysql11 ~]# mount | grep “/var/lib/mysql/gamedb”
  9. #查看挂载信息
  10. [root@mysql11 ~]# df -h | grep "/var/lib/mysql/gamedb"
  11. /dev/sdb1 10G 166M 9.9G 2% /var/lib/mysql/gamedb
  12. [root@mysql11 ~]#

2)配置mysql22

  1. #设置开机挂载
  2. [root@mysql22 ~]# vim /etc/fstab
  3. /dev/sdb1 /var/lib/mysql/gamedb xfs defaults 0 0
  4. :wq
  5. [root@mysql22 ~]#
  6. #加载没有挂载的设备
  7. [root@mysql22 ~]# mount -a
  8. [root@mysql22 ~]# mount | grep “/var/lib/mysql/gamedb”
  9. #查看挂载信息
  10. [root@mysql22 ~]# df -h | grep "/var/lib/mysql/gamedb"
  11. /dev/sdb1 10G 166M 9.9G 2% /var/lib/mysql/gamedb
  12. [root@mysql22 ~]#

3 案例3:配置主从同步

3.1 问题

配置步骤如下:

  1. 配置主服务器
  2. 配置从服务器

3.2 步骤

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

步骤一:配置主服务器mysql11

1)启用binlog日志

 
  1. [root@mysql11 ~]# vim /etc/my.cnf
  2. [mysqld]
  3. server_id=11
  4. log_bin=master11
  5. :wq
  6. [root@mysql11 ~]# systemctl restart mysqld

2)用户授权

 
  1. [root@mysql11 ~]# mysql -uroot -p123qqq…A
  2. mysql> grant replication slave on *.* to repluser@"%" identified by "123qqq...A";

3)查看日志信息

 
  1. mysql> show master status;
  2. +-----------------+----------+--------------+------------------+-------------------+
  3. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  4. +-----------------+----------+--------------+------------------+-------------------+
  5. | master11.000001 | 441 | | | |
  6. +-----------------+----------+--------------+------------------+-------------------+
  7. 1 row in set (0.00 sec)
  8. mysql>

步骤二:配置从服务器

1)指定server_id

 
  1. [root@mysql22 ~]# vim /etc/my.cnf
  2. [mysqld]
  3. server_id=22
  4. :wq
  5. [root@mysql22 ~]# systemctl restart mysqld

2)指定主服务器信息

 
  1. [root@mysql22 ~]# mysql -uroot -p123qqq…A
  2. mysql> change master to master_host="192.168.88.11",master_user="repluser",
  3. master_password="123qqq...A",master_log_file="master11.000001",master_log_pos=441;

3)启动slave进程

 
  1. mysql> start slave;
  2. Query OK, 0 rows affected (0.02 sec)
  3. mysql>

4)查看状态信息

 
  1. [root@mysql22 ~]# mysql -uroot -p123qqq…A -e "show slave status \G" | grep -i yes
  2. mysql: [Warning] Using a password on the command line interface can be insecure.
  3. Slave_IO_Running: Yes
  4. Slave_SQL_Running: Yes
  5. [root@mysql22 ~]#
  6. [root@mysql22 ~]# mysql -uroot -p123qqq…A -e "show slave status \G" |grep -i 192.168.4.11
  7. mysql: [Warning] Using a password on the command line interface can be insecure.
  8. Master_Host: 192.168.88.11
  9. [root@mysql22 ~]#

4 案例4:配置读写分离服务

4.1 问题

配置步骤如下:

  1. 安装软件
  2. 修改配置文件
  3. 配置数据库服务器
  4. 启动服务
  5. 查看服务状态
  6. 查看监控信息

4.2 步骤

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

步骤一:配置读写分离服务

1)安装软件

 
  1. //安装软件
  2. [root@maxscale77 ~]# rpm -ivh maxscale-2.1.2-1.rhel.7.x86_64.rpm
  3. 警告:maxscale-2.1.2-1.rhel.7.x86_64.rpm: 头V4 RSA/SHA1 Signature, 密钥 ID 8167ee24: NOKEY
  4. 准备中... ################################# [100%]
  5. 正在升级/安装...
  6. 1:maxscale-2.1.2-1 ( 2%################################# [100%]
  7. [root@maxscale77 ~]#

2)修改配置文件

 
  1. [root@maxscale77 ~]# vim /etc/maxscale.cnf
  2. //服务线程数量
  3. [maxscale]
  4. threads=auto #根据CPU核数创建线程个数
  5. //第1台数据库服务器
  6. [server1]
  7. type=server
  8. address=192.168.88.11 #master数据库服务器ip地址
  9. port=3306
  10. protocol=MySQLBackend
  11. //第2台数据库服务器
  12. [server2]
  13. type=server
  14. address=192.168.88.22 #slave数据库服务器ip地址
  15. port=3306
  16. protocol=MySQLBackend
  17. //指定监控数据库服务器server1 和 server2
  18. [MySQL Monitor]
  19. type=monitor
  20. module=mysqlmon
  21. servers=server1,server2
  22. user=mysqla //监控用户
  23. passwd=123qqq…A //密码
  24. monitor_interval=10000
  25. //注释只读服务
  26. #[Read-Only Service]
  27. #type=service
  28. #router=readconnroute
  29. #servers=server1
  30. #user=myuser
  31. #passwd=mypwd
  32. #router_options=slave
  33. //定义读写分离服务
  34. [Read-Write Service]
  35. type=service
  36. router=readwritesplit
  37. servers=server1,server2
  38. user=mysqlb //路由用户
  39. passwd=123qqq…A //密码
  40. max_slave_connections=100%
  41. //定义管理服务
  42. [MaxAdmin Service]
  43. type=service
  44. router=cli
  45. //注释只读服务端口
  46. #[Read-Only Listener]
  47. #type=listener
  48. #service=Read-Only Service
  49. #protocol=MySQLClient
  50. #port=4008
  51. //定义读写分离服务端口
  52. [Read-Write Listener]
  53. type=listener
  54. service=Read-Write Service
  55. protocol=MySQLClient
  56. port=3306 //端口号
  57. //定义管理服务端口
  58. [MaxAdmin Listener]
  59. type=listener
  60. service=MaxAdmin Service
  61. protocol=maxscaled
  62. socket=default
  63. port=4016 //端口号
  64. :wq
  65. [root@maxscale77 ~]#

步骤二:配置数据库服务器

1)创建用户

 
  1. #在主服务器上添加用户
  2. [root@mysql11 ~]# mysql -uroot -p123qqq…A
  3. mysql> grant replication slave, replication client on *.*
  4. -> to mysqla@"%" identified by "123qqq...A"; #监控用户
  5. mysql> grant select on mysql.* to
  6. -> to mysqlb@"%" identified by "123qqq...A"; #路由用户
  7. mysql>
  8. #从服务器查看用户
  9. [root@mysql22 ~]# mysql -uroot -p123qqq…A -e 'select user,host from mysql.user where user like "mysql%"'
  10. mysql: [Warning] Using a password on the command line interface can be insecure.
  11. +---------------+------+
  12. | user | host |
  13. +---------------+------+
  14. | mysqla | % |
  15. | mysqlb | % |
  16. +---------------+------+
  17. [root@mysql22 ~]#

步骤三:启动maxsacle服务

1)启动服务

 
  1. [root@maxscale77 ~]# maxscale -f /etc/maxscale.cnf

2)查看服务信息(进程 和 端口)

 
  1. [root@maxscale77 ~]# ps -C maxscale #查看进程
  2. PID TTY TIME CMD
  3. 23254 ? 00:00:00 maxscale
  4. [root@maxscale77 ~]#
  5. [root@maxscale77 ~]# netstat -utnlp | grep maxscale #查看进程的端口
  6. tcp6 0 0 :::3306 :::* LISTEN 23254/maxscale
  7. tcp6 0 0 :::4016 :::* LISTEN 23254/maxscale
  8. [root@maxscale77 ~]#

3)在maxscale服务本机访问管理服务,查看监控信息

 
  1. [root@maxscale77 ~]# maxadmin -uadmin -pmariadb -P4016
  2. MaxScale> list servers
  3. Servers.
  4. -------------------+-----------------+-------+-------------+--------------------
  5. Server | Address | Port | Connections | Status
  6. -------------------+-----------------+-------+-------------+--------------------
  7. server1 | 192.168.88.11 | 3306 | 0 | Master, Running
  8. server2 | 192.168.88.22 | 3306 | 0 | Slave, Running
  9. -------------------+-----------------+-------+-------------+--------------------
  10. MaxScale> exit
  11. [root@maxscale77 ~]#

5 案例5:配置网站服务

5.1 问题

具体配置如下:

  1. 部署LNP
  2. 测试PHP
  3. 测试mysqld数据库服务

5.2 步骤

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

步骤一:部署LNP

1)安装软件

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

步骤二:测试PHP

1)创建网页 test.php

 
  1. [root@web33 ~]# vim /usr/local/nginx/html/test.php
  2. <?php
  3. $i = 99;
  4. echo $i;
  5. ?>
  6. [root@web33 ~]# curl http://localhost/test.php
  7. 99
  8. [root@web33 ~]#

步骤二:测试MySQL服务

1)在主数据库服务器上添加连接MySQL服务的用户

 
  1. [root@mysql11 ~]# mysql -uroot -p123qqq…A
  2. mysql> create table gamedb.user(
  3. name char(10)); //建表
  4. mysql> grant select,insert,update,delete on gamedb.* to yaya@"%" identified by "123qqq...A"; //用户授权

2)在从服务器查看是否同步数据

 
  1. [root@mysql22 ~]# mysql -uroot -p123qqq…A
  2. mysql> show grants for yaya@"%"; //查看授权用户
  3. +--------------------------------------------------------------------+
  4. | Grants for yaya@% |
  5. +--------------------------------------------------------------------+
  6. | GRANT USAGE ON *.* TO 'yaya'@'%' |
  7. | GRANT SELECT, INSERT, UPDATE, DELETE ON `gamedb`.* TO 'yaya'@'%' |
  8. +--------------------------------------------------------------------+
  9. 2 rows in set (0.00 sec)
  10. mysql> desc gamedb.user; //查看库表
  11. +-------+----------+------+-----+---------+-------+    
  12. | Field | Type | Null | Key | Default | Extra |
  13. +-------+----------+------+-----+---------+-------+
  14. | name | char(10) | YES | | NULL | |
  15. +-------+----------+------+-----+---------+-------+

3)测试读写分离服务

在网站服务器连接数据读写分离服务器77

 
  1. [root@web33 ~]# yum -y install mariadb //安装提供连接命令软件
  2. [root@web33 ~]# mysql -h192.168.88.77 -uyaya -p123qqq…A //连接读写分离服务
  3. #存储数据
  4. MySQL > insert into gamedb.user values("pmm");
  5. Query OK, 1 row affected (0.05 sec)
  6. #查询数据
  7. MySQL > select * from gamedb.user;
  8. +------+
  9. | name |
  10. +------+
  11. | pmm |
  12. +------+

6)在主服务器本机登录查看数据

 
  1. [root@mysql11 ~]# mysql -uroot -p123qqq…A -e 'select * from gamedb.user'
  2. mysql: [Warning] Using a password on the command line interface can be insecure.
  3. +------+
  4. | name |
  5. +------+
  6. | pmm |
  7. +------+
  8. [root@mysql11 ~]#

7)在web33主机编写访问php脚本存储数据

 
  1. [root@web33 ~]# vim /usr/local/nginx/html/test2.php
  2. <?php
  3. $conn=mysql_connect("192.168.4.77","yaya","123qqq...A");
  4. $sql='insert into gamedb.user(name) values("plj")';
  5. mysql_query($sql);
  6. mysql_close($conn);
  7. echo "save ok";
  8. ?>
  9. :wq
  10. #访问脚本
  11. [root@web33 ~]# curl http://localhost/test2.php
  12. save ok
  13. #在主数据库服务器查看数据
  14. [root@mysql11 ~]# mysql -uroot -p123qqq...A -e 'select * from gamedb.user'
  15. mysql: [Warning] Using a password on the command line interface can be insecure.
  16. +------+
  17. | name |
  18. +------+
  19. | yaya |
  20. | pmm |
  21. +------+
  22. [root@localhost ~]#
  23. #在从数据库服务器查看数据
  24. [root@mysql22 ~]# mysql -uroot -p123qqq...A -e 'select * from gamedb.user'
  25. mysql: [Warning] Using a password on the command line interface can be insecure.
  26. +------+
  27. | name |
  28. +------+
  29. | yaya |
  30. | pmm |
  31. +------+
  32. [root@localhost ~]#
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

small white poplar

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值