Top

NSD PROJECT2 DAY01

  1. 案例1:配置逻辑卷
  2. 案例2:配置数据库服务器
  3. 案例3:配置主从同步
  4. 案例4:配置读写分离服务
  5. 案例5:准备NFS服务存储磁盘
  6. 案例6:配置NFS服务
  7. 案例7:配置网站服务
  8. 案例8:测试配置

1 案例1:配置逻辑卷

1.1 问题

具体配置如下:

1.2 方案

分别给2台虚拟机添加2块5G的磁盘。如图-1所示。

图-1

1.3

1.4 步骤

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

步骤一:磁盘分区(2台数据库服务器都配置)

1)创建分区,每块盘分1个区即可

  1. [root@mysql11 ~]# fdisk -l /dev/vdb //查看磁盘容量
  2.  
  3. 磁盘 /dev/vdb:5368 MB, 5368709120 字节,10485760 个扇区
  4. Units = 扇区 of 1 * 512 = 512 bytes
  5. 扇区大小(逻辑/物理):512 字节 / 512 字节
  6. I/O 大小(最小/最佳):512 字节 / 512 字节
  7.  
  8. [root@mysql11 ~]#
  9. [root@mysql11 ~]# fdisk -l /dev/vdc //查看磁盘容量
  10.  
  11. 磁盘 /dev/vdc:5368 MB, 5368709120 字节,10485760 个扇区
  12. Units = 扇区 of 1 * 512 = 512 bytes
  13. 扇区大小(逻辑/物理):512 字节 / 512 字节
  14. I/O 大小(最小/最佳):512 字节 / 512 字节
  15.  
  16. [root@mysql11 ~]#
  17.  
  18. [root@mysql11 ~]# fdisk /dev/vdb //磁盘分区
  19. 欢迎使用 fdisk (util-linux 2.23.2)
  20.  
  21. 更改将停留在内存中,直到您决定将更改写入磁盘。
  22. 使用写入命令前请三思。
  23.  
  24. Device does not contain a recognized partition table
  25. 使用磁盘标识符 0x882d04b7 创建新的 DOS 磁盘标签。
  26.  
  27. 命令(输入 m 获取帮助):n //创建分区
  28. Partition type:
  29. p primary (0 primary, 0 extended, 4 free)
  30. e extended
  31. Select (default p): p //创建主分区
  32. 分区号 (1-4,默认 1)1 //指定编号
  33. 起始 扇区 (2048-10485759,默认为 2048)//起始柱面数
  34. 将使用默认值 2048
  35. Last 扇区, +扇区 or +size{K,M,G} (2048-10485759,默认为 10485759)//结束柱面数
  36. 将使用默认值 10485759
  37. 分区 1 已设置为 Linux 类型,大小设为 5 GiB
  38.  
  39. 命令(输入 m 获取帮助):w //保存退出
  40. The partition table has been altered!
  41.  
  42. Calling ioctl() to re-read partition table.
  43. 正在同步磁盘。
  44. [root@mysql11 ~]#
  45.  
  46.  
  47. [root@mysql11 ~]# fdisk /dev/vdc //给vdc盘分区
  48. 欢迎使用 fdisk (util-linux 2.23.2)
  49.  
  50. 更改将停留在内存中,直到您决定将更改写入磁盘。
  51. 使用写入命令前请三思。
  52.  
  53. Device does not contain a recognized partition table
  54. 使用磁盘标识符 0x5184f504 创建新的 DOS 磁盘标签。
  55.  
  56. 命令(输入 m 获取帮助):n
  57. Partition type:
  58. p primary (0 primary, 0 extended, 4 free)
  59. e extended
  60. Select (default p): p
  61. 分区号 (1-4,默认 1)1
  62. 起始 扇区 (2048-10485759,默认为 2048)
  63. 将使用默认值 2048
  64. Last 扇区, +扇区 or +size{K,M,G} (2048-10485759,默认为 10485759)
  65. 将使用默认值 10485759
  66. 分区 1 已设置为 Linux 类型,大小设为 5 GiB
  67.  
  68. 命令(输入 m 获取帮助):w
  69. The partition table has been altered!
  70.  
  71. Calling ioctl() to re-read partition table.
  72. 正在同步磁盘。
  73. [root@mysql11 ~]#

2)查看分区

  1. [root@mysql11 ~]# fdisk -l /dev/vdb
  2.  
  3. 磁盘 /dev/vdb:5368 MB, 5368709120 字节,10485760 个扇区
  4. Units = 扇区 of 1 * 512 = 512 bytes
  5. 扇区大小(逻辑/物理):512 字节 / 512 字节
  6. I/O 大小(最小/最佳):512 字节 / 512 字节
  7. 磁盘标签类型:dos
  8. 磁盘标识符:0x882d04b7
  9.  
  10. 设备 Boot Start End Blocks Id System
  11. /dev/vdb1 2048 10485759 5241856 83 Linux
  12. [root@mysql11 ~]#
  13. [root@mysql11 ~]#
  14. [root@mysql11 ~]# fdisk -l /dev/vdc
  15.  
  16. 磁盘 /dev/vdc:5368 MB, 5368709120 字节,10485760 个扇区
  17. Units = 扇区 of 1 * 512 = 512 bytes
  18. 扇区大小(逻辑/物理):512 字节 / 512 字节
  19. I/O 大小(最小/最佳):512 字节 / 512 字节
  20. 磁盘标签类型:dos
  21. 磁盘标识符:0x5184f504
  22.  
  23. 设备 Boot Start End Blocks Id System
  24. /dev/vdc1 2048 10485759 5241856 83 Linux
  25. [root@mysql11 ~]#

步骤二:创建LV

1)安装提供命令的软件

  1.     [root@mysql11 ~]# rpm -q lvm2 || yum -y install lvm2 //安装软件
  2. 已加载插件:fastestmirror
  3. Loading mirror speeds from cached hostfile
  4. local_repo | 3.6 kB 00:00:00
  5. 正在解决依赖关系
  6. --> 正在检查事务
  7. ---> 软件包 lvm2.x86_64.7.2.02.177-4.el7 将被 安装
  8. ……
  9. ……
  10. 已安装:
  11. lvm2.x86_64 7:2.02.177-4.el7
  12.  
  13. 作为依赖被安装:
  14. device-mapper-event.x86_64 7:1.02.146-4.el7
  15. device-mapper-event-libs.x86_64 7:1.02.146-4.el7
  16. device-mapper-persistent-data.x86_64 0:0.7.3-3.el7
  17. libaio.x86_64 0:0.3.109-13.el7
  18. lvm2-libs.x86_64 7:2.02.177-4.el7
  19.  
  20. 完毕!
  21. [root@mysql11 ~]#

2)创建PV

  1. [root@mysql11 ~]#
  2. [root@mysql11 ~]# pvcreate /dev/vdb1 /dev/vdc1 //创建PV
  3. Physical volume "/dev/vdb1" successfully created.
  4. Physical volume "/dev/vdc1" successfully created.
  5. [root@mysql11 ~]#
  6. [root@mysql11 ~]# pvscan //查看PV信息
  7. PV /dev/vdb1 lvm2 [<5.00 GiB / 0 free]
  8. PV /dev/vdc1 lvm2 [<5.00 GiB / 0 free]
  9. PV /dev/sda2 VG centos lvm2 [<9.00 GiB / 0 free]
  10. Total: 3 [<18.99 GiB] / in use: 3 [<18.99 GiB] / in no VG: 0 [0 ]
  11. [root@mysql11 ~]# [root@mysql11 ~]#

3)创建VG

  1. [root@mysql11 ~]# vgcreate vg0 /dev/vdb1 /dev/vdc1 //创建vg 名称vg0
  2. Volume group "vg0" successfully created
  3. [root@mysql11 ~]#
  4. [root@mysql11 ~]# vgdisplay vg0 //查看vg0详细信息
  5. --- Volume group ---
  6. VG Name vg0
  7. System ID
  8. Format lvm2
  9. Metadata Areas 2
  10. Metadata Sequence No 2
  11. VG Access read/write
  12. VG Status resizable
  13. MAX LV 0
  14. Cur LV 1
  15. Open LV 1
  16. Max PV 0
  17. Cur PV 2
  18. Act PV 2
  19. VG Size 9.99 GiB //总大小
  20. PE Size 4.00 MiB
  21. Total PE 2558 //PE总个数
  22. Alloc PE / Size 2558 / 9.99 GiB
  23. Free PE / Size 0 / 0
  24. VG UUID l1MQ7u-CeXC-fdkP-mJCT-0GqP-1gFQ-qKxWNA
  25. [root@mysql11 ~]#

4)创建LV

  1. [root@mysql11 ~]# lvcreate -L 9.99G -n lv0 vg0 //创建LV 名称为lv0 大小9.99G
  2. Rounding up size to full physical extent 9.99 GiB
  3. Logical volume "lv0" created.
  4. [root@mysql11 ~]#
  5. [root@mysql11 ~]# lvdisplay /dev/vg0/lv0 //查看lv0信息
  6. --- Logical volume ---
  7. LV Path /dev/vg0/lv0
  8. LV Name lv0
  9. VG Name vg0
  10. LV UUID SciSrm-cp8l-4ZR2-wydh-xg6V-KMiI-pgW0j5
  11. LV Write Access read/write
  12. LV Creation host, time mysql11, 2021-02-02 10:33:35 -0500
  13. LV Status available
  14. # open 1
  15. LV Size 9.99 GiB //总大小
  16. Current LE 2558
  17. Segments 2
  18. Allocation inherit
  19. Read ahead sectors auto
  20. - currently set to 8192
  21. Block device 253:2
  22. [root@mysql11 ~]#

5)格式化

  1. [root@mysql11 ~]# mkfs.xfs /dev/vg0/lv0 //格式化为 xfs 文件系统
  2. meta-data=/dev/vg0/lv0 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.  
  12. [root@mysql11 ~]# blkid /dev/vg0/lv0 //查看文件系统类型
  13. /dev/vg0/lv0: UUID="3e06f58f-8793-4800-b444-676f5a07fe10" TYPE="xfs"
  14. [root@mysql11 ~]#

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

2.1 问题

具体操作如下:

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

2.2 步骤

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

步骤一:安装软件MySQL服务软件(2台数据库服务器都要安装)

1)解包软件

  1. [root@mysql11 ~]# 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@mysql11 ~]#
  14.  
  15. [root@mysql11 ~]# 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@mysql11 ~]#

2)安装软件

  1. [root@mysql11 ~]# yum -y install mysql-community-*.rpm
  2. 已加载插件:fastestmirror
  3. 正在检查 mysql-community-client-5.7.17-1.el7.x86_64.rpm: mysql-community-client-5.7.17-1.el7.x86_64
  4. mysql-community-client-5.7.17-1.el7.x86_64.rpm 将被安装
  5. ……
  6. ……
  7. 已安装:
  8. mysql-community-client.x86_64 0:5.7.17-1.el7 mysql-community-common.x86_64 0:5.7.17-1.el7
  9. mysql-community-devel.x86_64 0:5.7.17-1.el7 mysql-community-embedded.x86_64 0:5.7.17-1.el7
  10. mysql-community-embedded-compat.x86_64 0:5.7.17-1.el7 mysql-community-embedded-devel.x86_64 0:5.7.17-1.el7
  11. mysql-community-libs.x86_64 0:5.7.17-1.el7 mysql-community-libs-compat.x86_64 0:5.7.17-1.el7
  12. mysql-community-minimal-debuginfo.x86_64 0:5.7.17-1.el7 mysql-community-server.x86_64 0:5.7.17-1.el7
  13. mysql-community-test.x86_64 0:5.7.17-1.el7
  14.  
  15. 作为依赖被安装:
  16. perl-Data-Dumper.x86_64 0:2.145-3.el7 perl-JSON.noarch 0:2.59-2.el7
  17.  
  18. 完毕!
  19. [root@mysql11 ~]#

步骤二:挂载lv设备

1)设置开机挂载

  1. [root@mysql11 ~]# blkid /dev/vg0/lv0
  2. /dev/vg0/lv0: UUID="3e06f58f-8793-4800-b444-676f5a07fe10" TYPE="xfs"
  3. [root@mysql11 ~]#
  4.  
  5. [root@mysql11 ~]# vim /etc/fstab
  6. /dev/vg0/lv0 /var/lib/mysql xfs defaults 0 0
  7. :wq
  8. [root@mysql11 ~]#

2)查看挂载信息

  1. [root@mysql11 ~]# mount -a
  2.  
  3. [root@mysql11 ~]# mount | grep “/var/lib/mysql”
  4. /dev/mapper/vg0-lv0 on /var/lib/mysql type xfs (rw,relatime,attr2,inode64,noquota)
  5. [root@mysql11 ~]#

步骤三:启动服务

1)启动服务(俩台数据库服务器都要启动服务)

  1. [root@mysql11 ~]# systemctl start mysqld
  2. [root@mysql11 ~]#
  3. [root@mysql11 ~]# ls /var/lib/mysql
  4. auto.cnf client-cert.pem ibdata1 ibtmp1 mysql.sock.lock public_key.pem sys
  5. ca-key.pem client-key.pem ib_logfile0 mysql performance_schema server-cert.pem
  6. ca.pem ib_buffer_pool ib_logfile1 mysql.sock private_key.pem server-key.pem
  7. [root@mysql11 ~]#
  8. [root@mysql11 ~]# systemctl enable mysqld
  9. [root@mysql11 ~]#
  10.  
  11. [root@mysql11 ~]# netstat -utnlp | grep :3306
  12. tcp6 0 0 :::3306 :::* LISTEN 1531/mysqld
  13. [root@mysql11 ~]#
  14.  
  15. [root@mysql11 ~]# df -h | grep "/var/lib/mysql" //查看数据库目录容量
  16. /dev/mapper/vg0-lv0 10G 166M 9.9G 2% /var/lib/mysql
  17. [root@mysql11 ~]#

步骤四:管理员登录

1)查看初始密码

  1. [root@mysql11 ~]# grep “password” /var/log/mysqld.log
  2. 2019-07-05T01:56:51.895852Z 1 [Note] A temporary password is generated for root@localhost: bB0*uCmu:.Kj
  3.  
  4. [root@mysql11 ~]#
  5. [root@mysql11 ~]# mysql -uroot -p'bB0*uCmu:.Kj' //初始密码登录
  6. mysql: [Warning] Using a password on the command line interface can be insecure.
  7. Welcome to the MySQL monitor. Commands end with ; or \g.
  8. Your MySQL connection id is 3
  9. Server version: 5.7.17
  10.  
  11. Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
  12.  
  13. Oracle is a registered trademark of Oracle Corporation and/or its
  14. affiliates. Other names may be trademarks of their respective
  15. owners.
  16.  
  17. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  18.  
  19. mysql>

2)修改登录密码

  1. mysql> alter user root@"localhost" identified by "123qqq...A"; //修改密码
  2. Query OK, 0 rows affected (0.01 sec)
  3. mysql>
  4. mysql> exit
  5. Bye
  6. [root@mysql11 ~]#

3)新密码登录

  1. [root@mysql11 ~]# mysql -uroot -p123qqq...A
  2. mysql: [Warning] Using a password on the command line interface can be insecure.
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 4
  5. Server version: 5.7.17 MySQL Community Server (GPL)
  6.  
  7. Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
  8.  
  9. Oracle is a registered trademark of Oracle Corporation and/or its
  10. affiliates. Other names may be trademarks of their respective
  11. owners.
  12.  
  13. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  14.  
  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)

3 案例3:配置主从同步

3.1 问题

配置步骤如下:

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

3.2 步骤

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

步骤一:配置主服务器

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
  7. [root@mysql11 ~]#

2)用户授权

  1. [root@mysql11 ~]# mysql -uroot -p123qqq...A
  2. mysql: [Warning] Using a password on the command line interface can be insecure.
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 3
  5. mysql>
  6. mysql> grant replication slave on *.* to repluser@"%" identified by "123qqq...A";
  7. Query OK, 0 rows affected, 1 warning (0.04 sec)
  8. mysql>

3)查看日志信息

  1. [root@mysql11 ~]# mysql -uroot -p123qqq...A
  2. mysql> show master status;
  3. +-----------------+----------+--------------+------------------+-------------------+
  4. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  5. +-----------------+----------+--------------+------------------+-------------------+
  6. | master11.000001 | 441 | | | |
  7. +-----------------+----------+--------------+------------------+-------------------+
  8. 1 row in set (0.00 sec)
  9.  
  10. 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: [Warning] Using a password on the command line interface can be insecure.
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 3
  5. Server version: 5.7.17 MySQL Community Server (GPL)
  6. Mysql>
  7.  
  8. mysql> change master to master_host="192.168.4.11",master_user="repluser",
  9. -> master_password="123qqq...A",master_log_file="master11.000001",master_log_pos=441;
  10. Query OK, 0 rows affected, 2 warnings (0.41 sec)
  11.  
  12. mysql>

3)启动slave进程

  1. mysql> start slave ;
  2. Query OK, 0 rows affected (0.02 sec)
  3.  
  4. 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.  
  7. [root@mysql22 ~]# mysql -uroot -p123qqq...A -e "show slave status\G" |grep -i 192.168.4.11
  8. mysql: [Warning] Using a password on the command line interface can be insecure.
  9. Master_Host: 192.168.4.11
  10. [root@mysql22 ~]#

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

4.1 问题

配置步骤如下:

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

4.2 步骤

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

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

1)安装软件

  1. [root@maxscale77 ~]# rpm -ivh maxscale-2.1.2-1.rhel.7.x86_64.rpm //安装软件
  2. 警告:maxscale-2.1.2-1.rhel.7.x86_64.rpm: 头V4 RSA/SHA1 Signature, 密钥 ID 8167ee24: NOKEY
  3. 准备中... ################################# [100%]
  4. 正在升级/安装...
  5. 1:maxscale-2.1.2-1 ( 2%################################# [100%]
  6. [root@maxscale77 ~]#
  7. [root@maxscale77 ~]# ls /etc/maxscale.cnf //主配置文件
  8. /etc/maxscale.cnf
  9. [root@maxscale77 ~]# ls /var/log/maxscale/ //日志目录

2)修改配置文件

  1. [root@maxscale77 ~]# vim /etc/maxscale.cnf
  2.  
  3. [maxscale] //服务线程数量
  4. threads=auto
  5.  
  6. [server1] //第1台数据库服务器
  7. type=server
  8. address=192.168.4.11
  9. port=3306
  10. protocol=MySQLBackend
  11.  
  12. [server2] //第2台数据库服务器
  13. type=server
  14. address=192.168.4.22
  15. port=3306
  16. protocol=MySQLBackend
  17.  
  18. [MySQL Monitor] //指定监控数据库服务器server1 和 server2
  19. type=monitor
  20. module=mysqlmon
  21. servers=server1,server2
  22. user=mysqla //监控用户
  23. passwd=123qqq...A //密码
  24. monitor_interval=10000
  25.  
  26. [Read-Write Service] //定义读写分离服务
  27. type=service
  28. router=readwritesplit
  29. servers=server1,server2
  30. user=mysqlb //路由用户
  31. passwd=123qqq...A //密码
  32. max_slave_connections=100%
  33.  
  34. [MaxAdmin Service] //定义管理服务
  35. type=service
  36. router=cli
  37.  
  38. [Read-Write Listener] //定义读写分离服务端口
  39. type=listener
  40. service=Read-Write Service
  41. protocol=MySQLClient
  42. port=4006 //端口号
  43.  
  44. [MaxAdmin Listener] //定义管理服务端口
  45. type=listener
  46. service=MaxAdmin Service
  47. protocol=maxscaled
  48. socket=default
  49. port=4016 //端口号
  50. :wq
  51. [root@maxscale77 ~]#

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

1)创建用户:在主服务器上添加,从服务器查看即可

  1. [root@mysql11 ~]# mysql -uroot -p123qqq...A
  2. mysql> grant replication slave, replication client on *.*
  3. -> to mysqla@"%"identified by "123qqq...A";
  4. mysql> grant select on mysql.* to
  5. -> mysqlb@"%" identified by "123qqq...A";
  6. mysql>

2) 从服务器查看授权用户

  1. [root@mysql22 ~]# mysql -uroot -p123qqq...A -e 'select user,host from mysql.user where user like "maxscale%" '
  2. mysql: [Warning] Using a password on the command line interface can be insecure.
  3. +----------------+------+
  4. | user | host |
  5. +----------------+------+
  6. | mysqla | % |
  7. | mysqlb | % |
  8. +----------------+------+
  9. [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.  
  6. [root@maxscale77 ~]# netstat -utnlp | grep maxscale
  7. tcp6 0 0 :::4006 :::* LISTEN 23254/maxscale
  8. tcp6 0 0 :::4016 :::* LISTEN 23254/maxscale
  9. [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.4.11 | 3306 | 0 | Master, Running
  8. server2 | 192.168.4.22 | 3306 | 0 | Slave, Running
  9. -------------------+-----------------+-------+-------------+--------------------
  10. MaxScale>
  11. MaxScale> exit
  12. [root@maxscale77 ~]#

5 案例5:准备NFS服务存储磁盘

5.1 问题

具体配置如下:

  1. 添加磁盘
  2. 磁盘分区
  3. 创建LV
  4. 格式化

5.2 方案

给NFS服务器主机添加1块3G的磁盘。如图-2所示。

图-2

5.3

5.4 步骤

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

步骤一:磁盘分区

1)创建分区,分1个区即可

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

2)查看分区

  1. [root@nfs30 ~]# fdisk -l /dev/vdb
  2.  
  3. 磁盘 /dev/vdb:10.7 GB, 10737418240 字节,20971520 个扇区
  4. Units = 扇区 of 1 * 512 = 512 bytes
  5. 扇区大小(逻辑/物理):512 字节 / 512 字节
  6. I/O 大小(最小/最佳):512 字节 / 512 字节
  7. 磁盘标签类型:dos
  8. 磁盘标识符:0x67bb10cf
  9.  
  10. 设备 Boot Start End Blocks Id System
  11. /dev/vdb1 2048 20971519 10484736 83 Linux
  12. [root@nfs30 ~]#

步骤二:挂载磁盘

1)格式化

  1. [root@nfs30 ~]# mkfs.xfs /dev/vdb1 //格式化
  2. meta-data=/dev/vdb1 isize=512 agcount=4, agsize=655296 blks
  3. = sectsz=512 attr=2, projid32bit=1
  4. = crc=1 finobt=0, sparse=0
  5. data = bsize=4096 blocks=2621184, 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@nfs30 ~]#
  12. [root@nfs30 ~]# blkid /dev/vdb1 //查看
  13. /dev/vdb1: UUID="81740d7a-51f1-4ce1-a830-7b6517cc778e" TYPE="xfs"
  14. [root@nfs30 ~]#

2)配置开机挂载

  1. [root@nfs30 ~]# vim /etc/fstab //修改配置文件
  2. /dev/vdb1 /sitedir xfs defaults 0 0
  3. [root@nfs30 ~]#
  4.  
  5. [root@nfs30 ~]# mkdir /sitedir //创建挂载目录
  6.  
  7. [root@nfs30 ~]# chmod o+w /sitedir //赋予写权限
  8.  
  9. [root@nfs30 ~]# mount –a //加载文件中所有未加载的设备
  10.  
  11. [root@nfs30 ~]# mount | grep "/sitedir" //查看加载信息
  12. /dev/vdb1 on /sitedir type xfs (rw,relatime,attr2,inode64,noquota)
  13. [root@nfs30 ~]#

6 案例6:配置NFS服务

6.1 问题

具体配置如下:

  1. 安装软件
  2. 修改配置文件
  3. 启动服务
  4. 查看共享信息

6.2 步骤

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

步骤一:部署NFS服务

1)安装软件

  1. [root@nfs30 ~]# yum -y install nfs-utils rpcbind
  2. 已安装:
  3. nfs-utils.x86_64 1:1.3.0-0.54.el7 rpcbind.x86_64 0:0.2.0-44.el7
  4.  
  5. 作为依赖被安装:
  6. gssproxy.x86_64 0:0.7.0-17.el7 keyutils.x86_64 0:1.5.8-3.el7
  7. libbasicobjects.x86_64 0:0.1.1-29.el7 libcollection.x86_64 0:0.7.0-29.el7
  8. libevent.x86_64 0:2.0.21-4.el7 libini_config.x86_64 0:1.3.1-29.el7
  9. libnfsidmap.x86_64 0:0.25-19.el7 libpath_utils.x86_64 0:0.2.1-29.el7
  10. libref_array.x86_64 0:0.1.5-29.el7 libtirpc.x86_64 0:0.2.4-0.10.el7
  11. libverto-libevent.x86_64 0:0.2.5-4.el7 quota.x86_64 1:4.01-17.el7
  12. quota-nls.noarch 1:4.01-17.el7 tcp_wrappers.x86_64 0:7.6-77.el7
  13.  
  14. 完毕!
  15. [root@nfs30 ~]#

2)修改配置文件

  1. [root@nfs30 ~]#
  2. [root@nfs30 ~]# vim /etc/exports
  3. /sitedir *(rw)
  4. :wq
  5. [root@nfs30 ~]# exportfs –r //加载配置
  6. [root@nfs30 ~]#

步骤二:启动服务

1)启动服务

  1. [root@nfs30 ~]# systemctl start rpcbind
  2. [root@nfs30 ~]#
  3. [root@nfs30 ~]# systemctl start nfs
  4. [root@nfs30 ~]#
  5.  
  6. [root@nfs30 ~]# systemctl enable nfs
  7. Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
  8. [root@nfs30 ~]#
  9. [root@nfs30 ~]# systemctl enable rpcbind
  10. [root@nfs30 ~]#

2)查看共享信息

  1. [root@nfs30 ~]# showmount -e localhost
  2. Export list for localhost:
  3. /sitedir *
  4. [root@nfs30 ~]#

7 案例7:配置网站服务

7.1 问题

具体配置如下:

  1. 安装软件
  2. 挂载共享存储
  3. 启动服务

7.2 步骤

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

步骤一:安装软件

1)安装软件

  1. [root@web33 ~]# yum -y install java-1.8.0-openjdk-1.8.0.161-2.b14.el7.x86_64 //安装JDK
  2. [root@web33 ~]# tar -zxvf apache-tomcat-8.0.30.tar.gz //解压tomcat源码
  3. [root@web33 ~]#mv apache-tomcat-8.0.30 /usr/local/tomcat //方便管理移动目录
  4. [root@web33 ~]#yum -y install mysql-connector-java //安装连接mysql服务软件
  5. [root@web33 ~]#cp /usr/share/java/mysql-connector-java.jar /usr/local/tomcat/lib/ //拷贝连接命令
  6. [root@web33 ~]# rm -rf /usr/local/tomcat/webapps/ROOT/* //删除默认的网页文件
  7. [root@web33 ~]# yum -y install nfs-utils //安装提供showmount命令软件

步骤二:挂载共享

1)查看共享目录

  1. [root@web33 ~]# showmount -e 192.168.4.30 //查看共享
  2. Export list for 192.168.4.30:
  3. /sitedir *
  4. [root@web33 ~]#

2)挂载共享目录

  1. [root@web33 ~]# vim /etc/fstab //配置开机挂载
  2. 192.168.4.30:/sitedir /usr/local/tomcat/webapps/ROOT nfs defaults 0 0
  3. :wq
  4.  
  5. [root@web33 ~]# mount –a //加载所有未加载的设备

3)查看挂载

  1. [root@web33 ~]# mount | grep "sitedir" //查看挂载
  2. 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)
  3.  
  4. [root@web33 ~]# systemctl enable remote-fs.target //设置远程加载服务开机运行

步骤三:启动服务

1)启动服务

  1. [root@web33 ~]# /usr/local/tomcat/bin/startup.sh
  2. [root@web33 ~]# echo “/usr/local/tomcat/bin/startup.sh” >> /etc/rc.local //设置服务开机运行
  3. [root@web33 ~]# chmod +x /etc/rc.local

2)查看服务运行信息

  1. [root@web33 ~]# netstat -utnlp | grep java
  2. tcp6 0 0 127.0.0.1:8005 :::* LISTEN 83033/java
  3. tcp6 0 0 :::8009 :::* LISTEN 83033/java
  4. tcp6 0 0 :::8080 :::* LISTEN 83033/java

8 案例8:测试配置

8.1 问题

具体配置如下:

  1. 测试NFS服务
  2. 测试MySQL服务
  3. 测试Tomcat连接MySQL服务

8.2 步骤

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

步骤一:测试NFS服务

1)在nfs服务器创建网页 test.html

  1. [root@nfs30 ~]# echo "web test page" > /sitedir/test.html
  2. [root@nfs30 ~]#
  3. [root@nfs30 ~]# ls /sitedir/
  4. test.html
  5. [root@nfs30 ~]#

2)在客户端访问网站服务

  1. [root@room9pc17 ~]# curl http://192.168.4.33/test.html //访问网站33主机
  2. web test page
  3. [root@room9pc17 ~]#

步骤二:测试MySQL服务

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

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

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

  1. [root@mysql22 ~]# mysql -uroot -p123qqq...A
  2. mysql> show grants for yaya99@"%"; //查看授权用户
  3. +--------------------------------------------------------------------+
  4. | Grants for yaya99@% |
  5. +--------------------------------------------------------------------+
  6. | GRANT USAGE ON *.* TO 'yaya99'@'%' |
  7. | GRANT SELECT, INSERT, UPDATE, DELETE ON `gamedb`.* TO 'yaya99'@'%' |
  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. +-------+----------+------+-----+---------+-------+
  16. mysql> insert into gamedb.user values("xdd"); //用来验证数据读写分离
  17. mysql> select * from gamedb.user;
  18. +------+
  19. | name |
  20. +------+
  21. | xdd |
  22. +------+
  23. 1 row in set (0.00 sec)
  24.  
  25. mysql>

3)测试读写分离服务

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

  1. [root@web33 ~]# yum -y install mariadb //安装提供连接命令软件
  2. 已安装:
  3. mariadb.x86_64 1:5.5.56-2.el7
  4. 作为依赖被安装:
  5. mariadb-libs.x86_64 1:5.5.56-2.el7
  6. 完毕!
  7. [root@web33 ~]#
  8.  
  9. [root@web33 ~]# mysql -h192.168.4.77 -P4006 -uyaya99 -p123qqq...A //连接读写分离服务
  10. Welcome to the MariaDB monitor. Commands end with ; or \g.
  11. Your MySQL connection id is 23258
  12. Server version: 10.0.0 2.1.2-maxscale MySQL Community Server (GPL)
  13. Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
  14. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  15. MySQL [(none)]>

4)查询数据

  1. MySQL [(none)]> select * from gamedb.user; //显示的是从服务器上的数据
  2. +------+
  3. | name |
  4. +------+
  5. | xdd |
  6. +------+
  7. 1 row in set (0.00 sec)
  8. MySQL [(none)]>

5)存储数据

  1. MySQL [(none)]> insert into gamedb.user values("pmm"); //插入记录
  2. Query OK, 1 row affected (0.05 sec)
  3. MySQL [(none)]> select * from gamedb.user; //查看记录
  4. +------+
  5. | name |
  6. +------+
  7. | xdd |
  8. | pmm |
  9. +------+
  10. 2 rows in set (0.00 sec)
  11.  
  12. MySQL [(none)]>

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 ~]#

步骤三:Tomcat连接MySQL服务

1)在nfs服务共享目录编写网页文件

  1. [root@nfs30 ~]# vim /sitedir/linkdb.jsp
  2. <%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
  3. <%@ page import="java.naming.*" %>
  4. <%@ page import="java.sql.*" %>
  5. <html>
  6. <body>
  7. <%
  8. Class.forName("com.mysql.jdbc.Driver");
  9. Connection con=DriverManager.getConnection("jdbc:mysql://192.168.4.11:3306/gamedb","yaya99","123qqq...A");
  10. Statement state=con.createStatement();
  11.  
  12. String sql="insert into user values('TOM')";
  13. state.executeUpdate(sql);
  14. %>
  15. <h1>data save ok</h1>
  16. </body>
  17. </html>
  18. :wq

2)在浏览器地址栏里输入网站访问

  1. http://192.168.4.33:8080/linkdb.jsp

3)在据库服务器查看数据

  1. [root@mysql11 ~]# mysql -uadmin -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. | TOM |
  7. | xdd |
  8. | pmm |
  9. +------+
  10. [root@mysql11 ~]#