@数据库迁移

数据库迁移

一、数据库拆分

为什么要拆分?

由于单台服务器运行`LNMP`架构会导致网站访问缓慢,当内存被占满时,很容易导致系统出现`oom`,从而kill掉MySQL数据库,所以要将web和数据库进行独立部署。(一般数据占用服务器内存70%-80%)

二、拆分数据库解决什么问题

1、缓解web网站的压力
2、增强数据库读写性能
3、提高用户访问的速度

三、数据库环境搭建

1.环境准备
主机搭建服务外网地址内网地址
web01nginx+php10.0.0.7172.16.1.7
db01mariadb10.0.0.51172.16.1.51
2.部署全新的服务器(建新房子)
[root@db01 ~]# yum install -y mariadb-server
3.启动新的数据库(装修)
[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.
4.创建设置数据库密码(换锁)
[root@db01 ~]# mysqladmin -uroot password '123456'
5.数据库测试连接(能不能住)
[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
6.检查并授权远程连接数据库(想办法进去住)
[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)]>
7.授权数据库后并测试连接(再试试能不能住)
[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)]>
8.导出旧数据库内数据(把家具搬出来)
[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.导出的文件后缀无所谓
9.将导出的数据传到新数据库机器(把东西运到新房子门口)
[root@web01 ~]# scp /tmp/wordpress.sql 172.16.1.51:/tmp/
[root@web01 ~]# scp /tmp/*.sql 172.16.1.51:/tmp
10.把数据导入新的数据库(把家具搬进新房子)
#方式一:在房子外面往里搬
[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;
11.检查数据库状态(查看摆放)
[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)]> 
12.修改wordpress重新连接新的数据库(告诉亲戚新的住址)
[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');
13.修改zhihu连接数据库配置重新链接
[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',
);
14.修改edusoho连接数据库配置文件
[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/*
15.测试访问页面
16.停掉旧的数据库(旧房子拆迁,卖掉)
[root@web01 ~]# systemctl stop mariadb
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值