nginx架构演进-拆分数据库及nfs

时间:2022-06-29 00:54:24

1.为什么要进行数据库的拆分

由于单台服务器运行LNMP架构会导致网站访问缓慢,当内存吃满时,容易导致系统出现oom,从而kill掉MySQL数据库,所以将web和数据库进行独立部署。

2.数据库拆分后解决了什么问题

1.缓解web网站的压力

2.增强数据库的读写性能

3.提高用户访问的速度

3.数据库拆分架构

nginx架构演进-拆分数据库及nfs

4.如何将LNMP拆分为LNP MySQL

4.1备份172.16.1.7上的数据库信息

[[email protected] ~]# mysqldump -uroot -p‘oldxu.com‘ --all-databases > mysql-all.sql

4.2将172.16.1.7上的数据推送至172.16.1.51

[[email protected] ~]# scp mysql-all.sql [email protected]:/tmp

4.3登录172.16.1.51恢复数据

[[email protected] ~]# yum install mariadb mariadb-server -y
[[email protected] ~]# systemctl enable mariadb
[[email protected] ~]# systemctl start mariadb

读取sql文件至数据库中

[[email protected] ~]# mysql -uroot < /tmp/mysql-all.sql
[[email protected] ~]# systemctl restart mariadb

配置一个远程用户,允许其他服务器能通过远程的方式连接

mysql -u数据库名 -p数据库密码

MariaDB [(none)]> grant all privileges on *.* to ‘all‘@‘%‘ identified by ‘oldxu.com‘;
MariaDB [(none)]> flush privileges;

4.4将172.16.1.7程序连接至本地的数据库,修改为远程的数据库(应用割接)

[[email protected] ~]# systemctl disable mariadb
[[email protected] ~]# systemctl stop mariadb

4.5修改对应的数据的配置文件

[[email protected] wordpress]# find ./ -type f  | xargs grep "oldxu.com"
    ./wp-config.php:define( ‘DB_PASSWORD‘, ‘oldxu.com‘ );

    /** WordPress数据库的名称 */
    define( ‘DB_NAME‘, ‘wordpress‘ );

    /** MySQL数据库用户名 */
    define( ‘DB_USER‘, ‘all‘ );

    /** MySQL数据库密码 */
    define( ‘DB_PASSWORD‘, ‘oldxu.com‘ );

    /** MySQL主机 */
    define( ‘DB_HOST‘, ‘172.16.1.51‘ );

    wecenter
    [[email protected] zh]# find ./ -type f  | xargs grep "oldxu.com"
    ./system/config/database.php:  ‘password‘ => ‘oldxu.com‘,

    edusoho
    [[email protected] edusoho]# find ./ -type f  | xargs grep "oldxu.com"
    ./app/config/parameters.yml
    
    清理缓存
    [[email protected] edusoho]# rm -rf /code/edusoho/app/cache/*

5.扩展多台web节点,简称web集群

5.1准备一台172.16.1.18的服务器

5.2确保172.16.1.8上安装Nginx PHP

yum -y install nginx 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

5.3确保172.16.1.8 nginx配置 代码和172.16.1.7一致

5.4.1创建用户和用户组

[[email protected] ~]# groupadd -g 666 www
[[email protected] ~]# useradd -u666 -g666 www

5.4.2切到172.16.1.7上执行如下的操作

[[email protected] ~]# rsync -avz --delete  /etc/nginx [email protected]:/etc/
[[email protected] ~]# rsync -avz --delete  /etc/php.ini [email protected]:/etc/
[[email protected] ~]# rsync -avz --delete  /etc/php-fpm.d [email protected]:/etc/

5.4.3打包代码

[[email protected] ~]# tar czf code.tar.gz /code

5.4.4拷贝代码

[[email protected] ~]# scp code.tar.gz [email protected]:/tmp
5.5回到172.16.1.8  然后解包  授权  重启服务,并加入开机自启

[[email protected] ~]# tar xf /tmp/code.tar.gz -C /
[[email protected] ~]# systemctl restart nginx php-fpm
[[email protected] ~]# systemctl enable nginx php-fpm

6.如何将多台节点的静态资源共享至NFS

6.1准备172.16.1.31nfs存储服务器

6.1.1安装

[[email protected] ~]# yum install nfs-utils -y

6.1.2配置

[[email protected] ~]# 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)
6.1.3初始化环境

[[email protected] ~]# mkdir -p /data/{blog,zh,edu}
[[email protected] ~]# groupadd -g 666 www
[[email protected] ~]# useradd -u666 -g666 www
[[email protected] ~]# chown -R www.www /data/
6.1.4启动

[[email protected] ~]# systemctl enable nfs
[[email protected] ~]# systemctl restart nfs

6.2找到web存储的图片所在的路径 http://blog.oldxu.com/wp-content/uploads/2019/09/tt.jpeg

[[email protected] wp-content]# mv uploads/ uploads_bak
[[email protected] wp-content]# scp -rp uploads_bak/* [email protected]:/data/blog/
[[email protected] wp-content]# mkdir uploads

6.3在 172.16.1.7 172.16.1.8 ....  应用服务器上进行挂载

[[email protected] wp-content]# mount -t nfs 172.16.1.31:/data/blog /code/wordpress/wp-content/uploads

S: 注意权限问题
    [[email protected] ~]# chown -R www.www /data/
 6.4.访问网站 测试