文章目录
前言
现在需要实现拆分数据库和多台web服务器共享文件。也就是多台服务器搭建LNMP环境。
一、拆分数据库
1.为什么要拆分
由于单台服务器运行LNMP架构会导致网站访问缓慢,当内存被占满时,很容易导致系统出现oom从而kill掉MySQL数据库,所以要将web和数据库进行独立部署。
2.数据库拆分解决的问题
1.缓解网站压力
2.增强数据库读写性能
3.提高用户访问速度
3.环境准备
主机 | IP | 服务 |
---|---|---|
web01 | 10.0.0.7、172.16.1.7 | nginx、php |
db01 | 172.16.1.51 | mariadb |
4.在新的服务器上搭建数据库
#安装mysql服务
[root@db01 ~]# yum install -y mariadb-server
#启动并设置自启
[root@db01 ~]# systemctl start mariadb
[root@db01 ~]# systemctl enable mariadb
#设置数据库密码
[root@db01 ~]# mysqladmin -uroot password "zzc"
5.web01远程连接测试
[root@web01 ~]# mysql -uroot -pzzc -h172.16.1.51
ERROR 1130 (HY000): Host '172.16.1.7' is not allowed to connect to this MariaDB server
#错误,不允许主机172.16.1.7连接
#命令拆分
mysql #命令
-u #指定用户
root #数据库用户
-p #指定密码
Linhd@123 #数据库用户的密码
-h #指定主机
172.16.1.51 #远端数据库的主机地址
6.授权远程连接数据库
[root@db01 ~]# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 5.5.65-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> grant all on *.* to root@'172.16.1.%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
#命令拆分
grant #授权命令
all #所有权限
on #在...上
*.* #所有库 .所有表
to #给...
root@'172.16.1.%' #用户 root@'172.16.1.%' root用户通过172.16.1.0-255连接都允许
identified #指定密码
by #是
'123456'; #密码