为什么要拆分?
由于单台服务器运行`LNMP`架构会导致网站访问缓慢,当内存被占满时,很容易导致系统出现`oom`,从而kill掉MySQL数据库,所以要将web和数据库进行独立部署。(一般数据占用服务器内存70%-80%)
1、缓解web网站的压力
2、增强数据库读写性能
3、提高用户访问的速度
主机 | 搭建服务 | 外网地址 | 内网地址 |
---|---|---|---|
web01 | nginx+php | 10.0.0.7 | 172.16.1.7 |
db01 | mariadb | 10.0.0.51 | 172.16.1.51 |
[root@db01 ~]# yum install -y mariadb-server
[root@db01 ~]# systemctl start mariadb
[root@db01 ~]# systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
[root@db01 ~]# mysqladmin -uroot password '123456'
[root@web01 ~]# mysql -uroot -pLin123.com -h172.16.1.51
ERROR 1130 (HY000): Host '172.16.1.7' is not allowed to connect to this MariaDB server
[root@db01 ~]# mysql -uroot -p123456
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 5
Server version: 5.5.64-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.
#授权 172.16.1.% 这个网段 通过 root用户 密码123456 连接之后 可以管理我得所有库所有表,所有命令
MariaDB [(none)]> grant all on *.* to root@'172.16.1.%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
#查看授权用户
MariaDB [(none)]> select user,host from mysql.user;
+------+------------+
| user | host |
+------+------------+
| root | 127.0.0.1 |
| root | 172.16.1.% | #如果有这一条说明授权成功
| root | ::1 |
| | db01 |
| root | db01 |
| | localhost |
| root | localhost |
+------+------------+
7 rows in set (0.00 sec)
MariaDB [(none)]>
[root@web01 ~]# mysql -uroot -p123456 -h172.16.1.51
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 5.5.64-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)]>
[root@web01 ~]# mysqldump -uroot -pLin123.com -B wordpress > /tmp/wordpress.sql
[root@web01 ~]# mysqldump -uroot -pLin123.com -B zh > /tmp/zh.sql
[root@web01 ~]# mysqldump -uroot -pLin123.com -B edusoho > /tmp/edu.sql
#注意:
1.导出的文件名字与数据库名无关
2.导出的文件后缀无所谓
[root@web01 ~]# scp /tmp/wordpress.sql 172.16.1.51:/tmp/
[root@web01 ~]# scp /tmp/*.sql 172.16.1.51:/tmp
#方式一:在房子外面往里搬
[root@db01 ~]# mysql -uroot -p123456 < /tmp/wordpress.sql
[root@db01 ~]# mysql -uroot -p123456 < /tmp/zh.sql
[root@db01 ~]# mysql -uroot -p123456 < /tmp/edu.sql
#方式二:在房子里面往里搬
MariaDB [wordpress]> source /tmp/wordpress.sql;
#方式三:传送门方式
[root@web01 tmp]# mysql -uroot -p123456 -h172.16.1.51 < /tmp/wordpress.sql
[root@web01 tmp]# mysql -uroot -p123456 -h172.16.1.51
MariaDB [wordpress]> source /tmp/wordpress.sql;
[root@db01 ~]# mysql -uroot -p123456
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 12
Server version: 5.5.64-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)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| wordpress | #看到这个库就带表导入成功
+--------------------+
5 rows in set (0.00 sec)
MariaDB [(none)]>
[root@db01 ~]# mysql -uroot -p123456
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 5.5.64-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)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| edusoho |
| mysql |
| performance_schema |
| test |
| wordpress |
| zh |
+--------------------+
7 rows in set (0.01 sec)
MariaDB [(none)]>
[root@web01 ~]# vim /code/wordpress/wp-config.php
/** WordPress数据库的名称 */
define('DB_NAME', 'wordpress');
/** MySQL数据库用户名 */
define('DB_USER', 'root');
/** MySQL数据库密码 */
define('DB_PASSWORD', '123456');
/** MySQL主机 */
define('DB_HOST', '172.16.1.51');
[root@web01 ~]# vim /code/wecenter/system/config/database.php
$config['master'] = array (
'charset' => 'utf8',
'host' => '172.16.1.51',
'username' => 'root',
'password' => '123456',
'dbname' => 'zh',
);
[root@web01 ~]# vim /code/edusoho/app/config/parameters.yml
database_host: 172.16.1.51
database_port: 3306
database_name: edusoho
database_user: root
database_password: '123456'
[root@web01 ~]# rm -rf /code/edusoho/app/cache/*
[root@web01 ~]# systemctl stop mariadb