拆分数据库至独立服务器
为什么要拆分数据库
由于单台服务器运行LNMP架构会导致网站访问缓慢,当内存被占满时,很容易导致系统出现oom从而kill掉MySQL数据库,所以要将web和数据库进行独立部署。
注意:这种的功能是一个整体,属于集群,不属于微服务。
拆分服务器可以解决如下问题:
1.缓解web网站的压力
2.增强数据库读写性能
3.提高用户访问速度
实现流程
1、克隆一台服务器51
2、安装mariadb-server
【省略,上面有流程】
3、导出10.0.0.7上面的所有库文件,并关闭MySQL服务(如果一开始使用的51就省略该步骤了)
1. [root@Web01 ~]# mysqldump -uroot -p666666 -A > all.sql 2. [root@Web01 ~]# ll 3. total 2508 4. -rw-r--r-- 1 root root 2563044 Apr 6 09:06 all.sql 5. -rw-------. 1 root root 1519 Mar 29 18:41 anaconda-ks.cfg 6. [root@Web01 ~]# scp all.sql 10.0.0.51:/root/ #将all.sql发送到10.0.0.51的root下 7. The authenticity of host '10.0.0.51 (10.0.0.51)' can't be established. 8. ECDSA key fingerprint is SHA256:zQvI/tCFYssR7l6cr90EtaIA93FXJp8FmUhGtkZshlA. 9. ECDSA key fingerprint is MD5:0b:a1:ee:d2:75:92:1a:62:05:63:5e:d1:e8:42:13:84. 10. Are you sure you want to continue connecting (yes/no)? yes 11. Warning: Permanently added '10.0.0.51' (ECDSA) to the list of known hosts. 12. root@10.0.0.51's password: 13. all.sql 100% 2503KB 35.0MB/s 00:00 14. [root@Web01 ~]# systemctl stop mariadb 15. [root@Web01 ~]# systemctl disable mariadb 16. Removed symlink /etc/systemd/system/multi-user.target.wants/mariadb.service.
4、导入到10.0.0.51新数据、检查数据库是否导入成功
1. [root@MySQL ~]# mysql -uroot -p666666 < all.sql 2. [root@MySQL ~]# mysql -uroot -p666666 -e "show databases" 3. +--------------------+ 4. | Database | 5. +--------------------+ 6. | information_schema | 7. | mysql | 8. | performance_schema | 9. | phpshe | 10. | test | 11. | wecenter | 12. | wordpress | 13. +--------------------+ 14. [root@MySQL ~]#
5、修改代码中连接数据库的信息(服务需要挨个修改)
浏览器访问网址
1. #授权普通用户,允许远程连接51 2. 3. [root@MySQL ~]# mysql -uroot -p666666 4. Welcome to the MariaDB monitor. Commands end with ; or \g. 5. Your MariaDB connection id is 10 6. Server version: 5.5.68-MariaDB MariaDB Server 7. 8. Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. 9. 10. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 11. 12. MariaDB [(none)]> grant all on *.* to koten@'%' identified by '666666'; 13. Query OK, 0 rows affected (0.00 sec) 14. 15. MariaDB [(none)]> quit 16. Bye 17. 18. 19. [root@Web01 ~]# vim /code/wordpress/wp-config.php 20. /** The name of the database for WordPress */ 21. define( 'DB_NAME', 'wordpress' ); #数据库名称 22. define( 'DB_USER', 'koten' ); #数据库用户 23. 24. /** Database password */ 25. define( 'DB_PASSWORD', '666666' ); #数据库密码 26. 27. /** Database hostname */ 28. define( 'DB_HOST', '10.0.0.51' ); #修改远程连接IP地址
修改后刷新浏览器,发现恢复(其他服务同理)
拆分静态资源至独立服务器
1、安装NFS
[root@NFS ~]# yum -y install nfs-utils
2、创建匿名压缩用户www
1. [root@NFS ~]# groupadd -g666 www 2. [root@NFS ~]# useradd -u666 -g666 -M -s /sbin/nologin www
3、修改配置文件
1. [root@NFS ~]# cat /etc/exports 2. /data/wordpress 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666) 3. /data/wecenter 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
4、创建必要数据文件
1. [root@NFS ~]# mkdir -p /data/wordpress 2. [root@NFS ~]# mkdir -p /data/wecenter 3. [root@NFS ~]# chown www.www /data/* 4. [root@NFS ~]# ll /data/ 5. total 0 6. drwxr-xr-x 2 www www 6 Apr 6 10:08 wecenter 7. drwxr-xr-x 2 www www 6 Apr 6 10:08 wordpress
5、启动NFS
1. [root@NFS ~]# systemctl start nfs 2. [root@NFS ~]# systemctl enable nfs 3. Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
6、找出业务上传图片的目录并挂载
右键复制图片连接地址:找到上传目录uploads
http://blog.koten.com/wp-content/uploads/2023/04/1.jpg
1)将2023目录拷贝到31的/data/wordpress/
1. [root@Web01 ~]# scp -r /code/wordpress/wp-content/uploads/* 172.16.1.31:/data/wordpress/ 2. The authenticity of host '172.16.1.31 (172.16.1.31)' can't be established. 3. ECDSA key fingerprint is SHA256:zQvI/tCFYssR7l6cr90EtaIA93FXJp8FmUhGtkZshlA. 4. ECDSA key fingerprint is MD5:0b:a1:ee:d2:75:92:1a:62:05:63:5e:d1:e8:42:13:84. 5. Are you sure you want to continue connecting (yes/no)? yes 6. Warning: Permanently added '172.16.1.31' (ECDSA) to the list of known hosts. 7. root@172.16.1.31's password: 8. music-music-1.6.2.zip 100% 67KB 6.5MB/s 00:00 9. HTML5中国象棋.zip 100% 1571KB 30.9MB/s 00:00 10. 小霸王-FC怀旧游 100% 7718KB 48.9MB/s 00:00 11. [root@Web01 ~]#
NFS重新授权下面的目录权限
1. [root@NFS wordpress]# chown -R www.www /data/wordpress/ 2. [root@NFS wordpress]# ll /data/wordpress/ 3. total 0 4. drwxr-xr-x 3 www www 16 Apr 6 15:22 2023
2)将31/data/wordpress挂载到uploads目录
1. web1安装nfs-utils 2. [root@Web01 ~]# yum -y install nfs-utils 3. 4. 查看共享目录 5. [root@Web01 ~]# showmount -e 172.16.1.31 6. Export list for 172.16.1.31: 7. /data/wecenter 172.16.1.0/24 8. /data/wordpress 172.16.1.0/24 9. 10. 挂载NFS共享目录/data/wordpress并查看挂载信息 11. [root@Web01 ~]# mount -t nfs 172.16.1.31:/data/wordpress /code/wordpress/wp-content/uploads 12. [root@Web01 ~]# df -h 13. Filesystem Size Used Avail Use% Mounted on 14. devtmpfs 476M 0 476M 0% /dev 15. tmpfs 487M 0 487M 0% /dev/shm 16. tmpfs 487M 7.7M 479M 2% /run 17. tmpfs 487M 0 487M 0% /sys/fs/cgroup 18. /dev/sda3 19G 2.4G 17G 13% / 19. /dev/sda1 197M 110M 88M 56% /boot 20. tmpfs 98M 0 98M 0% /run/user/0 21. 172.16.1.31:/data/wordpress 19G 2.0G 17G 11% /code/wordpress/wp-content/uploads
扩展一个web节点(web2服务器)
可以直接克隆,也可以创建新服务器自己安装配置服务,同步文件
1、安装Nginx+PHP+NFS
1. Nginx 2. 3. [root@Web02 ~]# scp 172.16.1.7:/etc/yum.repos.d/nginx.repo /etc/yum.repos.d/ 4. The authenticity of host '172.16.1.7 (172.16.1.7)' can't be established. 5. ECDSA key fingerprint is SHA256:zQvI/tCFYssR7l6cr90EtaIA93FXJp8FmUhGtkZshlA. 6. ECDSA key fingerprint is MD5:0b:a1:ee:d2:75:92:1a:62:05:63:5e:d1:e8:42:13:84. 7. Are you sure you want to continue connecting (yes/no)? yes 8. Warning: Permanently added '172.16.1.7' (ECDSA) to the list of known hosts. 9. root@172.16.1.7's password: 10. nginx.repo 100% 192 110.1KB/s 00:00 11. [root@Web02 ~]# yum -y install nginx 12. 13. 14. PHP 15. 安装PHP软件【文末可以下载】 16. [root@Web02 ~]# rz -E 17. rz waiting to receive. 18. [root@Web02 ~]# ls 19. anaconda-ks.cfg php71.tar.gz 20. [root@Web02 ~]# tar xf php71.tar.gz 21. [root@Web02 ~]# yum -y localinstall *.rpm 22. 23. 24. 扩展:批量删除PHP软件 25. [root@Web02 ~]# rpm -qa|grep php|xargs yum -y remove 26. 27. 28. NFS 29. 安装nfs-utils,但不用启动 30. [root@Web02 ~]# yum -y install nfs-utils 31.
2、创建虚拟用户www
1. [root@Web02 ~]# groupadd -g666 www 2. [root@Web02 ~]# useradd -u666 -g666 -M -s /sbin/nologin www
3、同步配置文件和WEB01相同(/etc/nginx/,/etc/php-fpm.d/www.conf)
1. #同步Nginx配置 2. [root@Web02 ~]# rsync -avz --delete 172.16.1.7:/etc/nginx/ /etc/nginx 3. root@172.16.1.7's password: 4. receiving incremental file list 5. ./ 6. nginx.conf 7. conf.d/ 8. conf.d/default.conf 9. conf.d/phpshe.conf 10. conf.d/wecenter.conf 11. conf.d/wordpress.conf 12. 13. sent 155 bytes received 1,701 bytes 1,237.33 bytes/sec 14. total size is 9,775 speedup is 5.27 15. 16. #同步PHP配置 17. [root@Web02 conf.d]# rsync -avz --delete 172.16.1.7:/etc/php-fpm.d/www.conf /etc/php-fpm.d/www.conf 18. root@172.16.1.7's password: 19. receiving incremental file list 20. www.conf 21. 22. sent 199 bytes received 425 bytes 113.45 bytes/sec 23. total size is 17,962 speedup is 28.79
4、同步代码文件到WEB02
打包code然后拷贝到web02,进行解压到/目录
1. [root@Web01 04]# tar zcvf code.tar.gz /code/ 2. 3. [root@Web02 ~]# scp 172.16.1.7:/code/wordpress/wp-content/uploads/2023/04/code.tar.gz / 4. root@172.16.1.7's password: 5. code.tar.gz 100% 168MB 40.6MB/s 00:04 6. [root@Web02 /]# tar xf code.tar.gz 7. [root@Web02 /]# ll /code 8. total 8 9. drwxr-xr-x 8 www www 300 Apr 6 09:56 phpshe 10. drwxr-xr-x 14 www www 4096 Apr 4 17:13 wecenter 11. drwxr-xr-x 5 www www 4096 Apr 6 09:21 wordpress
5、启动服务、挂载NFS到本地上传目录
1. #启动服务 2. [root@Web02 code]# systemctl start nginx php-fpm 3. [root@Web02 code]# systemctl enable nginx php-fpm 4. Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service. 5. Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service. 6. 7. #挂载NFS到本地上传目录 8. [root@Web02 code]# mount -t nfs 172.16.1.31:/data/wordpress /code/wordpress/wp-content/uploads/ 9. [root@Web02 code]# df -h 10. Filesystem Size Used Avail Use% Mounted on 11. devtmpfs 476M 0 476M 0% /dev 12. tmpfs 487M 0 487M 0% /dev/shm 13. tmpfs 487M 7.7M 479M 2% /run 14. tmpfs 487M 0 487M 0% /sys/fs/cgroup 15. /dev/sda3 19G 2.4G 17G 13% / 16. /dev/sda1 197M 110M 88M 56% /boot 17. tmpfs 98M 0 98M 0% /run/user/0 18. 172.16.1.31:/data/wordpress 19G 2.2G 17G 12% /code/wordpress/wp-content/uploads
Linux系统PHP软件下载链接:https://pan.baidu.com/s/1E9QfpakeZA1zkA3B7-urjw?pwd=r843
我是koten,10年运维经验,持续分享运维干货,感谢大家的阅读和关注!