Day 53 LNMP拆分数据库
1.1 拆分数据库
缓解网站的压力
1.1.1 准备一台服务器
打开虚拟机->克隆->完成地址修改 172.16.1.51
1.1.2 安装好MySQL数据库
# 1.下载MySQL官方扩展源
[root@nginx ~]# rpm -ivh http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql57-community-release-el7-10.noarch.rpm
#2.安装mysql5.7, 文件过大可能会导致下载缓慢
[root@nginx ~]# yum install mysql-community-server -y
#3.启动数据库, 并加入开机自启动
[root@nginx ~]# systemctl start mysqld
[root@nginx ~]# systemctl enable mysqld
#4.由于mysql5.7默认配置了默认密码, 需要过滤temporary password关键字查看对应登陆数据库密码
[root@nginx ~]# grep 'temporary password' /var/log/mysqld.log
#5.登陆mysql数据库[password中填写上一步过滤的密码]
[root@web02 ~]# mysql -uroot -p$(awk '/temporary password/{print $NF}' /var/log/mysqld.log)
#6.重新修改数据库密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Bgx123.com';
1.1.3 备份原数据库中的数据
#1.指定导出对应的数据库文件。(Bgx123.com是数据库密码)
[root@web02 ~]# mysqldump -uroot -p'Bgx123.com' --all-databases --single-transaction > `date +%F%H`-mysql-all.sql
1.1.4 拷贝原数据库备份文件至新的服务器
[root@web02 zh]# scp 2018-09-2109-mysql-all.sql [email protected]:~
1.1.5 将数据导入进新的数据库环境中
[root@db01 ~]# mysql -uroot -pBgx123.com <2018-09-2109-mysql-all.sql
#验证是否导入成功
[root@db01 ~]# mysql -uroot -pBgx123.com
#登录数据库后,查看当前有多少个库
mysql> show databases;
1.1.6 使用客户端测试连接远端的mysql
[root@web01 ~]# mysql -h172.16.1.51 -uroot -pBgx123.com
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1130 (HY000): Host '172.16.1.7' is not allowed to connect to this MySQL server
1.1.7 新的数据库,配置允许远程用户连接
#授权所有权限 grant all privileges
#授权所有库所有表 *.*
#将授权赋予给哪个用户,这个用户只能通过哪个网段过来(%所有) 'all'@'%'
#授权该用户登录的密码 identified by
mysql> grant all privileges on *.* to 'all'@'%' identified by 'Bgx123.com';
1.1.8 再次使用客户端测试连接远端的mysql
[root@web01 ~]# mysql -h172.16.1.51 -uall -pBgx123.com
#如果其他机器想测试能否连接数据库,可以先安装mysql的客户端软件
[root@web02 ~]# yum install mariadb -y
[root@web02 ~]# mysql -h172.16.1.51 -uall -pBgx123.com
1.1.9 停止web本地的数据库,刷新网页看看是否down机
[root@web01 ~]# systemctl stop mysqld
[root@web01 ~]# systemctl disable mysqld
1.1.10 修改Wordpress产品代码连接数据库的配置文件
[root@web01 ~]# vim /code/wordpress/wp-config.php
# 数据库名称
define('DB_NAME', 'wordpress');
# 数据库用户
define('DB_USER', 'all');
# 数据库密码
define('DB_PASSWORD', 'Bgx123.com');
# 数据库地址
define('DB_HOST', '172.16.1.51');
1.1.11 修改wecenter产品代码连接数据库的配置文件
[root@web01 zh]# grep -iR "Bgx123.com"|grep -v cache
system/config/database.php: 'password' => 'Bgx123.com',
[root@web01 zh]# vim /code/zh/system/config/database.php
'host' => '172.16.1.51',
'username' => 'all',
'password' => 'Bgx123.com',
'dbname' => 'zh',
1.1.12 修改edusogho产品代码连接数据库的配置文件
[root@web01 edu]# grep -iR "Bgx123.com"|grep -v cache
app/config/parameters.yml: database_password: 'Bgx123.com'
[root@web01 edu]# vim /code/edu/app/config/parameters.yml
parameters:
database_driver: pdo_mysql
database_host: 172.16.1.51
database_port: 3306
database_name: edu
database_user: all
database_password: 'Bgx123.com'
#必须清理缓存
[root@web01 edu]# rm -rf /code/edu/app/cache/*
2.1 Web挂载NFS
2.1.1 准备一台新的服务器安装NFS
[root@nfs ~]# groupadd -g666 www
[root@nfs ~]# useradd -u666 -g666 www
[root@nfs ~]# yum install nfs-utils -y
[root@nfs ~]# cat /etc/exports
/data/blog 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
/data/edu 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
/data/zh 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
创建对应的共享目录,并授权为www
[root@nfs ~]# mkdir /data/{blog,zh,edu} -p
[root@nfs ~]# chown -R www.www /data/
重启NFS
[root@nfs ~]# systemctl restart nfs-server
2.1.2 WEB客户端验证NFS是否安装成功
[root@web01 ~]# yum install nfs-utils -y
[root@web01 ~]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/data/zh 172.16.1.0/24
/data/edu 172.16.1.0/24
/data/blog 172.16.1.0/24
2.1.3 获取Wordpress产品的附件和图片存放的位置
浏览器->右键->检查->Network->选择按钮->点击一下图片
2.1.4 备份web服务器上的Wordpress图片和附件
[root@web01 ~]# cd /code/wordpress/
[root@web01 wordpress]# cp -rp wp-content/ wp-content_back
2.1.5 客户端执行挂载操作【Wordpress】
[root@web01 ~]# mount -t nfs 172.16.1.31:/data/blog /code/wordpress/wp-content
2.1.6 恢复原web上存储的(图片、附件、语言、主题)
[root@web01 wordpress]# cp -rp wp-content_back/* wp-content/
3.1 拆分web图片放入nfs共享目录中
3.1.1 获取Edu产品的附件和图片存放的位置
浏览器->右键->检查->Network->选择按钮->点击一下图片
3.1.2 备份edu产品的视频和图片
视频:/code/edu/app/data/udisk/
图片:/code/edu/web/files/
3.1.3 备份原有的视频和图片
[root@web01 edu]# cp -rp web/files/ web/files_bak
[root@web01 edu]# cp -rp app/data/ app/data_bak
3.1.4 执行挂载操作【我仅挂载视频,其他自行解决】
[root@web01 edu]# mount -t nfs 172.16.1.31:/data/edu /oldboy_code4/edu/app/data
3.1.5 恢复数据至NFS存储
[root@web01 edu]# \cp -rp app/data_bak/* app/data/
4.1 快速的扩展一台相同的web服务器【数据库一模一样,图片、附件都一样】
先准备一台服务器10.0.0.8
4.1.1 创建www用户
[root@web02 ~]# groupadd -g666 www
[root@web02 ~]# useradd -u666 -g666 www
4.1.2 安装LNP
[root@web02 ~]# scp -rp [email protected]:/etc/yum.repos.d/* /etc/yum.repos.d/
[root@web02 ~]# scp -rp [email protected]:/etc/pki/rpm-gpg/* /etc/pki/rpm-gpg/
4.1.3 安装Nginx
[root@web02 ~]# yum install nginx -y
4.1.4 安装php
[root@web02 ~]# yum -y install php71w php71w-cli php71w-common php71w-devel \
php71w-embedded php71w-gd php71w-mcrypt php71w-mbstring php71w-pdo php71w-xml php71w-fpm \
php71w-mysqlnd php71w-opcache php71w-pecl-memcached php71w-pecl-redis php71w-pecl-mongodb
4.1.5 将web01的nginx配置文件导入到web02
[root@web02 ~]# scp -rp [email protected]:/etc/nginx/* /etc/nginx/
4.1.6 将web01的php配置文件导入到web02
[root@web02 ~]# scp -rp [email protected]:/etc/php-fpm.d/* /etc/php-fpm.d/
4.1.7 将web01的 产品代码 导到web02
在web1上线进行打包操作
[root@web01 ~]# tar czf code.tar.gz /oldboy_code4/
在web2上面拉取web1打包好的内容
[root@web02 ~]# scp [email protected]:/root/code.tar.gz ~
在web2上面解压即可
[root@web02 ~]# tar xf code.tar.gz -C /
4.1.8 启动相关的服务
[root@web02 ~]# systemctl start nginx php-fpm
[root@web02 ~]# systemctl enable nginx php-fpm
4.1.9 在web02上进行挂载
[root@web02 ~]# mount -t nfs 172.16.1.31:/data/blog /code/wordpress/wp-content
4.1.10 所有的挂载都应该加入开机自启动