项目最终效果:
给网站服务搭建2套数据存储框架 分别是内存存储数据框架 和 硬盘存储数据框架
把网站的热点数据 存储在内存存储服务器里(Redis)
把需要永久保存的数据存储在硬盘存储服务器里 (Mysql)
案例1:配置逻辑卷
把数据库服务器的数据存储到逻辑卷(LV)里,目的是可以动态扩展数据库服务器
硬盘的存储空间
具体步骤如下:
1) 给2台数据库服务器mysql11 和 mysql22 分别添加2块5G的硬盘
2) 划分物理分区 (/dev/vdb1 /dev/vdc1)
3) 创建物理卷
4) 创建卷组vg0
5) 创建逻辑卷lv0
6) 格式化lv0 (文件系统类型是xfs)
操作命令如下:
systemctl stop mysqld
yum -y install lvm2
lsblk
pvcreate /dev/vdb /dev/vdc
vgcreate vg0 /dev/vdb /dev/vdc
vgs
lvcreate -n lv0 -L 5.99g vg0
lvscan
mkfs.xfs /dev/vg0/lv0
blkid /dev/vg0/lv0
案例2:配置数据库服务器
具体步骤如下:(2台数据库服务器主机分别做如下操作)
1)?安装?mysql服务软件
2)开机挂载逻辑卷lv0?到数据库目录下
3)启动mysql服务并设置开机启动
4)??查看初始密码并使用初始密码登录
5)?修改数据库管理员密码?并断开连接
6)?使用修改后的密码登录
]# rm -rf /var/lib/mysql/*
]# vim /etc/fstab
/dev/vg0/lv0 /var/lib/mysql xfs defaults 0 0
:wq
]# mount -a
]# mount | grep "/var/lib/mysql"
]# systemctl start mysqld
]# grep password /var/log/mysqld.log | tail -1
]# mysqladmin -uroot -p'Tih:Zehtr57s' password "123qqq...A"
]# mysql -uroot -p123qqq...A
案例3:配置主从同步:(读写分离存储数据时?能让用户存储的数据和查看的是一致)
1)?配置主数据库服务器?mysql11(192.168.4.11)
具体操作如下:
1)?启用binlog日志文件
2)????用户授权
3)??查看正在使用的binlog日志名和偏移量?(准备从服务器使用)
mysql11]# vim /etc/my.cnf
[mysqld]
server_id=11
log_bin=master11
:wq
mysql11]# systemctl restart mysqld
mysql11]# mysql -uroot -p123qqq...A
mysql> grant replication slave on *.* to repluser@"%" identified by "123qqq...A";
mysql>
mysql> show master status;
+-----------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-----------------+----------+--------------+------------------+-------------------+
| master11.000001 | 1026 | | | |
+-----------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
2)?配置从数据库服务器?mysql11(192.168.4.22)
具体操作如下:
1)指定server_id????(修改配置设置server_id?所以要重启服务)
2)管理员登录数据库服务,指定主服务器信息,指定的信息
包括(ip????用户????密码????日志名??偏移量)
3)启动slave进程
4)查看IO线程和SQL线程的状态(都是Yes?就配置对了
mysql22]# vim /etc/my.cnf
[mysqld]
server_id=22
:wq
mysql11]# systemctl restart mysqld
mysql11]# mysql -uroot -p123qqq...A
mysql> change master to master_host="192.168.4.11" , master_user="repluser" , master_password="123qqq...A",
master_log_file="master11.000001" , master_log_pos=441;
mysql> start slave;
mysql> show slave status \G
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
案例4:配置读写分离服务:
ip?地址?是?192.168.4.77???主机名maxscal77
把客户端连接后的查询请求给slave?数据库服务器,?写请求master数据库服务器
具体配置步骤如下:
1)安装软件
yum?-y?install?maxscale-2.1.2-1.rhel.7.x86_64.rpm
2)修改配置文件
vim /etc/maxscale.cnf
[maxscale]
threads=auto
[server1]
type=server
address=192.168.4.11
port=3306
protocol=MySQLBackend
[server2]
type=server
address=192.168.4.22
port=3306
protocol=MySQLBackend
[MySQL Monitor]
type=monitor
module=mysqlmon
servers=server1,server2
user=mysqla
passwd=123qqq...A
monitor_interval=10000
#[Read-Only Service]
#type=service
#router=readconnroute
#servers=server1
#user=myuser
#passwd=mypwd
#router_options=slave
[Read-Write Service]
type=service
router=readwritesplit
servers=server1,server2
user=mysqlb
passwd=123qqq...A
max_slave_connections=100%
[MaxAdmin Service]
type=service
router=cli
#[Read-Only Listener]
#type=listener
#service=Read-Only Service
#protocol=MySQLClient
#port=4008
[Read-Write Listener]
type=listener
service=Read-Write Service
protocol=MySQLClient
port=4006
[MaxAdmin Listener]
type=listener
service=MaxAdmin Service
protocol=maxscaled
socket=default
port=4016
:wq
3)配置数据库服务器?
只需要在主数据库服务器mysql11?用户?从服务器会自动同步用户
replication?slave,??查看主从角色?
replication?client??查看mysql数据库服务运行状态
[root@mysql11 ~]# mysql -uroot -p123qqq...A
mysql> grant replication slave , replication client on *.* to mysqla@"%" identified by "123qqq...A"; 监控用户
mysql> grant select on *.* to mysqlb@"%" identified by "123qqq...A"; 路由用户
在从数据库服务器查看是否同步授权用户名
[root@mysql22 ~]# mysql -uroot -p123qqq...A
mysql> select user from mysql.user where user="mysqla";
mysql> select user from mysql.user where user="mysqlb";
4)??启动maxscale服务?(在maxscale77?主机启动服务)
[root@maxscale77 ~]# maxscale /etc/maxscale.cnf
5)?查看maxscale服务状态
[root@maxscale77 ~]# netstat -utnlp | grep 4016
tcp6 0 0 :::4016 :::* LISTEN 1125/maxscale
[root@maxscale77 ~]# netstat -utnlp | grep 4006
tcp6 0 0 :::4006 :::* LISTEN 1125/maxscale
[root@maxscale77 ~]#
6)查看监控信息(在maxscale77?访问自己的管理服务,查看监控信息)
[root@maxscale77 ~]# maxadmin -uadmin -pmariadb -P4016
MaxScale> list servers
Servers.
-------------------+-----------------+-------+-------------+--------------------
Server | Address | Port | Connections | Status
-------------------+-----------------+-------+-------------+--------------------
server1 | 192.168.4.11 | 3306 | 0 | Master, Running
server2 | 192.168.4.22 | 3306 | 0 | Slave, Running
-------------------+-----------------+-------+-------------+--------------------
MaxScale> exit
[root@maxscale77 ~]#
案例5:准备NFS服务存储磁盘?
IP?192.168.4.30主机提供nfs服务?,存储网站web33的网页文件
步骤一?:?
1)添加1块3G?磁盘
2)??磁盘分区?(分一个区??/dev/vdb1)
3)??格式化
[root@NFS30 ~]# lsblk 分区前查看
[root@NFS30 ~]# fdisk /dev/vdb -> n -> p -> 三个回车-> w
[root@NFS30 ~]# lsblk 分区后查看
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda 253:0 0 30G 0 disk
└─vda1 253:1 0 30G 0 part /
vdb 253:16 0 3G 0 disk
└─vdb1 253:17 0 3G 0 part
[root@NFS30 ~]# mkfs.xfs /dev/vdb1
步骤二:挂载磁盘
[root@NFS30 ~]# mkdir /sitedir
[root@NFS30 ~]# vim /etc/fstab
/dev/vdb1 /sitedir xfs defaults 0 0
:wq
[root@NFS30 ~]# mount -a
[root@NFS30 ~]# mount | grep -i "/sitedir"
/dev/vdb1 on /sitedir type xfs (rw,relatime,attr2,inode64,noquota)
[root@NFS30 ~]#
案例6:配置NFS服务?(192.168.4.30?把目录共享给客户端?)
?1)安装软件
?2)修改配置文件
?3)?启动服务?(先启动rpcbind服务?再启动nfs服务)
?4)查看共享信息
[root@NFS30 ~]# yum -y install nfs-utils rpcbind
[root@NFS30 ~]# vim /etc/exports
/sitedir *(rw)
:wq
[root@NFS30 ~]# chmod o+w /sitedir/
[root@NFS30 ~]# systemctl restart rpcbind
[root@NFS30 ~]# systemctl enable rpcbind
[root@NFS30 ~]# systemctl restart nfs
[root@NFS30 ~]# systemctl enable nfs
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
[root@NFS30 ~]# showmount -e localhost
Export list for localhost:
/sitedir *
[root@NFS30 ~]#
案例7:配置网站服务192.168.4.33?主机名web33
1)安装软件apache-tomcat-8.0.30.tar.gz??(使用tomcatr软件提高的网站服务)
2)挂载nfs服务的共享目录(把网站的网页存放的nfs30主机里)
3)查看挂载
4)?启动tomcat服务
5)?查看tomcat服务运行信息
yum -y install java-1.8.0-openjdk
tar -xf apache-tomcat-8.0.30.tar.gz
mv apache-tomcat-8.0.30 /usr/local/tomcat
rm -rf /usr/local/tomcat/webapps/ROOT/*
which showmount || yum -y install nfs-utils
[root@web33 ~]# showmount -e 192.168.4.30
Export list for 192.168.4.30:
/sitedir *
[root@web33 ~]#
[root@web33 ~]# vim /etc/fstab
192.168.4.30:/sitedir /usr/local/tomcat/webapps/ROOT nfs defaults 0 0
:wq
[root@web33 ~]# mount -a
[root@web33 ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/vda1 30G 1.4G 29G 5% /
devtmpfs 697M 0 697M 0% /dev
tmpfs 707M 0 707M 0% /dev/shm
tmpfs 707M 8.5M 699M 2% /run
tmpfs 707M 0 707M 0% /sys/fs/cgroup
tmpfs 142M 0 142M 0% /run/user/0
192.168.4.30:/sitedir 3.0G 32M 3.0G 2% /usr/local/tomcat/webapps/ROOT 已经挂载
[root@web33 ~]#
[root@web33 ~]# /usr/local/tomcat/bin/startup.sh ^C
[root@web33 ~]#
[root@web33 ~]# netstat -utnlp | grep :8080
tcp6 0 0 :::8080 :::* LISTEN 1371/java
[root@web33 ~]#
?案例8:测试配置
一?、测试NFS服务
[root@web33 ~]# netstat -utnlp | grep 8080
tcp6 0 0 :::8080 :::* LISTEN 1829/java
[root@web33 ~]# vim /usr/local/tomcat/webapps/ROOT/test.html
[root@web33 ~]# cat /usr/local/tomcat/webapps/ROOT/test.html
web test page
[root@web33 ~]#
[root@web33 ~]#
[root@web33 ~]# curl http://localhost:8080/test.html
web test page
[root@web33 ~]#
二、测试MySQL服务
2.1、?在主数据库服务器mysql11?做如下配置
[root@mysql11 ~]# mysql -uroot -p123qqq...A
create database gamedb;
create table gamedb.user ( name char(10) , password char(6));
grant select, insert on gamedb.* to yaya@"%" identified by "123qqq...A";
2.2、在从服务器?检查是否同步库表?和??授权用户?
[root@mysql22 ~]# mysql -uroot -p123qqq...A -e 'desc gamedb.user'
[root@mysql22 ~]# mysql -uroot -p123qqq...A -e 'select user from mysql.user where user="yaya"'
2.3?在网站服务器命令行?连接?maxscale77
web33]# yum -y install mariadb
web33]# mysql -h192.168.4.77 -P4006 -uyaya -p123qqq...A
mysql>exit ;
三、测试Tomcat连接MySQL服务(在网页目录下编写网站脚本文件?访问数据库服务)
1)在网站服务器做如下配置
[root@web33 ~]# yum -y install mysql-connector-java
[root@web33 ~]# cp /usr/share/java/mysql-connector-java.jar /usr/local/tomcat/lib/
[root@web33 ~]# /usr/local/tomcat/bin/shutdown.sh
[root@web33 ~]# /usr/local/tomcat/bin/startup.sh
2)编写网站脚本(在nfs服务共享目录编写网页文件)
[root@nfs30 ~]# vim /sitedir/linkdb.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@ page import="java.naming.*" %>
<%@ page import="java.sql.*" %>
<%
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://192.168.4.77:4006","yaya","123qqq...A");
Statement state=con.createStatement();
String sql="insert into gamedb.user values('bbb','654321')";
state.executeUpdate(sql);
%>
data save ok
3)在浏览器地址栏里输入网站访问?或?命令行连接访问?都可以
[root@NFS30 ~]# curl http://192.168.4.33:8080/linkdb.jsp
data save ok
4)在据库服务器查看数据?(可以查看到linkdb.jsp?脚本里?Insert?into?命令添加的记录?)
[root@mysql11 ~]# mysql -uroot -p123qqq...A -e 'select * from gamedb.user'
mysql: [Warning] Using a password on the command line interface can be insecure.
+------+----------+
| name | password |
+------+----------+
| bbb | 654321 |
+------+----------+
[root@mysql11 ~]#
[root@mysql22 ~]# mysql -uroot -p123qqq...A -e 'select * from gamedb.user'
mysql: [Warning] Using a password on the command line interface can be insecure.
+------+----------+
| name | password |
+------+----------+
| bbb | 654321 |
+------+----------+
[root@mysql22 ~]#
|