@数据库迁移

1|0数据库迁移

1|1一、数据库拆分

为什么要拆分?

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

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

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

1|3三、数据库环境搭建

1|01.环境准备

主机搭建服务外网地址内网地址
web01nginx+php10.0.0.7172.16.1.7
db01mariadb10.0.0.51172.16.1.51

1|02.部署全新的服务器(建新房子)

[root@db01 ~]# yum install -y mariadb-server

1|03.启动新的数据库(装修)

[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.

1|04.创建设置数据库密码(换锁)

[root@db01 ~]# mysqladmin -uroot password '123456'

1|05.数据库测试连接(能不能住)

[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

1|06.检查并授权远程连接数据库(想办法进去住)

[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)]>

1|07.授权数据库后并测试连接(再试试能不能住)

[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)]>

1|08.导出旧数据库内数据(把家具搬出来)

[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.导出的文件后缀无所谓

1|09.将导出的数据传到新数据库机器(把东西运到新房子门口)

[root@web01 ~]# scp /tmp/wordpress.sql 172.16.1.51:/tmp/ [root@web01 ~]# scp /tmp/*.sql 172.16.1.51:/tmp

1|010.把数据导入新的数据库(把家具搬进新房子)

#方式一:在房子外面往里搬 [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;

1|011.检查数据库状态(查看摆放)

[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)]>

1|012.修改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');

1|013.修改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', );

1|014.修改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/*

1|015.测试访问页面

1|016.停掉旧的数据库(旧房子拆迁,卖掉)

[root@web01 ~]# systemctl stop mariadb

__EOF__

本文作者ଲ小何才露煎煎饺
本文链接https://www.cnblogs.com/zeny/p/15121568.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   ଲ小何才露煎煎饺  阅读(40)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
点击右上角即可分享
微信分享提示