day040-搭建lnmp服务与数据库迁移

文章目录

  • [0. 老男孩思想-企业会如何对待价值观一般的员工?](#0. 老男孩思想-企业会如何对待价值观一般的员工?)
  • [1. location 规则优先级](#1. location 规则优先级)
  • [2. 动态网站架构](#2. 动态网站架构)
    • [2.1 动态、静态网站](#2.1 动态、静态网站)
    • [2.2 常见动态网站架构](#2.2 常见动态网站架构)
    • [2.3 LNMP架构](#2.3 LNMP架构)
  • [3. 部署LNMP环境](#3. 部署LNMP环境)
    • [3.1 数据库mariadb](#3.1 数据库mariadb)
      • [3.1.1 安装mariadb](#3.1.1 安装mariadb)
      • [3.1.2 安全配置](#3.1.2 安全配置)
      • [3.1.3 mariadb基础命令](#3.1.3 mariadb基础命令)
      • [3.1.4 添加用户及设置权限](#3.1.4 添加用户及设置权限)
      • [3.1.5 删除用户](#3.1.5 删除用户)
    • [3.2 php](#3.2 php)
      • [3.2.1 安装php](#3.2.1 安装php)
      • [3.2.2 修改php配置文件](#3.2.2 修改php配置文件)
      • [3.2.3 检查语法并重启](#3.2.3 检查语法并重启)
    • [3.3 nginx配置](#3.3 nginx配置)
      • [3.3.1 添加站点配置文件](#3.3.1 添加站点配置文件)
      • [3.3.2 在站点跟目录添加php文件](#3.3.2 在站点跟目录添加php文件)
    • [3.4 部署代码与设置权限](#3.4 部署代码与设置权限)
    • [3.5 配置本地hosts解析](#3.5 配置本地hosts解析)
  • [4. 数据库迁移](#4. 数据库迁移)
    • [4.1 备份旧数据库](#4.1 备份旧数据库)
    • [4.2 导入备份数据](#4.2 导入备份数据)
    • [4.3 修改代码连接的数据库](#4.3 修改代码连接的数据库)
  • [5. 思维导图](#5. 思维导图)

0. 老男孩思想-企业会如何对待价值观一般的员工?

  • 不会刻意培养价值观差的人
  • 重要的业务不会安排给价值观差的做
  • 重要的岗位不会安排给价值观差的担任
  • 企业会想着如何清理/替代价值观差的员工

1. location 规则优先级

  1. 精确匹配:location =
  2. 非正则匹配:location ^~
  3. 正则匹配:location ~或location ~*
  4. 路径匹配:location /URI
  5. 默认匹配:location /

2. 动态网站架构

2.1 动态、静态网站

网站架构 特点 说明
动态资源 服务端处理与加工,设计动态语言 php、java、Python、golang、rust...... 一般需要数据库,服务端处理,将结果响应给用户; 网站处理较慢
静态资源 服务端发送响应,客户端解析(html、css、js) 前端 客户端解析,服务端只负责发送;网站处理更快
混合 静态(前端开发),动态(后端开发) 全栈开发

2.2 常见动态网站架构

常见动态网站架构 说明
lnmp Linux系统、nginx、mysql、php环境
lnmt Linux系统、nginx、mysql、tomcat(java)
lnm? Linux系统、nginx、mysql、其他语言(python、golang)

2.3 LNMP架构

3. 部署LNMP环境

3.1 数据库mariadb

3.1.1 安装mariadb

shell 复制代码
# 查看是否有mysql的安装包
[root@web01 ~]# rpm -qa |grep -E 'mysql|mariadb'
mariadb-connector-c-3.0.6-9.ky10.x86_64
# 安装mariadb-server
[root@web01 ~]# yum install -y mariadb-server
上次元数据过期检查:1:15:23 前,执行于 2025年06月23日 星期一 18时21分42秒。
软件包 mariadb-server-3:10.3.39-1.p01.ky10.x86_64 已安装。
依赖关系解决。
无需任何处理。
完毕!
# 设置开机自启动
[root@web01 ~]# systemctl enable --now mariadb-server
[root@web01 ~]# systemctl is-active mariadb.service 
active
[root@web01 ~]# systemctl is-enabled mariadb.service 
enabled
# 检查
[root@web01 ~]# ss -lntup |grep 3306
tcp     LISTEN   0        80                     *:3306                *:*       users:(("mysqld",pid=2496,fd=21))                            
[root@web01 ~]# ps -ef |grep [m]ysql
mysql       2496       1  0 17:37 ?        00:00:03 /usr/libexec/mysqld

3.1.2 安全配置

  • 仅在初始安装时执行即可
  • 命令:mysql_secure_installation
shell 复制代码
# 初始化数据库安全设置
[root@web01 ~]# mysql_secure_installation 

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):   # 初始root密码为空,直接回车
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] Y # 设置root用户密码
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] Y # 移除匿名用户
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] Y # 禁用root远程登录
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] Y # 移除测试数据库
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] Y # 重新加载权限信息表
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

3.1.3 mariadb基础命令

  • 进入数据库
shell 复制代码
[root@web01 ~]# mysql -uroot -p1
......
[root@web01 ~]# mysql -uroot -p
Enter password:  # 输入密码
  • 显示存在的数据库
shell 复制代码
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.001 sec)
  • 查看某个数据库中的表
shell 复制代码
MariaDB [(none)]> show tables from mysql;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| column_stats              |
| columns_priv              |
| db                        |
| event                     |
| func                      |
| general_log               |
| gtid_slave_pos            |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| host                      |
| index_stats               |
| innodb_index_stats        |
| innodb_table_stats        |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| roles_mapping             |
| servers                   |
| slow_log                  |
| table_stats               |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| transaction_registry      |
| user                      |
+---------------------------+
31 rows in set (0.000 sec)
  • 查看用户信息
    • user:用户名
    • host:允许该用户登录的节点(网段)
shell 复制代码
MariaDB [(none)]> select user,host from mysql.user;
+------+-----------+
| user | host      |
+------+-----------+
| root | 127.0.0.1 |
| root | ::1       |
| root | localhost |
+------+-----------+
3 rows in set (0.000 sec)
  • 查看当前用户信息
    • 用户名@登录的节点
shell 复制代码
MariaDB [(none)]> select user();
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.000 sec)

3.1.4 添加用户及设置权限

  • 命令:grant all on blog.* to 'blog'@'localhost' identified by '1';
  • 配置远程登录权限
shell 复制代码
MariaDB [(none)]> grant all on blog.* to 'blog'@'172.16.1.%' identified by '1';
Query OK, 0 rows affected (0.000 sec)

MariaDB [(none)]> select user,host from mysql.user;
+------+------------+
| user | host       |
+------+------------+
| root | 127.0.0.1  |
| blog | 172.16.1.% |
| root | ::1        |
| blog | localhost  |
| root | localhost  |
+------+------------+
5 rows in set (0.000 sec)
  • 检查
shell 复制代码
# 远程登录:-h,指定mysql服务器地址
[root@db01 ~]# mysql -ublog -p1 -h 172.16.1.7
......
MariaDB [(none)]> select  user();
+-----------+
| user()    |
+-----------+
| blog@db01 |
+-----------+
1 row in set (0.001 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| blog               |
| information_schema |
+--------------------+
2 rows in set (0.001 sec)

3.1.5 删除用户

  • 格式:DROP USER [IF EXISTS] '用户名'@'主机';
shell 复制代码
MariaDB [(none)]> select user,host from mysql.user;
+------+------------+
| user | host       |
+------+------------+
| root | 127.0.0.1  |
| blog | 172.16.1.% |
| root | ::1        |
| blog | localhost  |
| root | localhost  |
+------+------------+
5 rows in set (0.000 sec)

# 禁用blog远程登录
MariaDB [(none)]> drop user 'blog'@'172.16.1.%';
Query OK, 0 rows affected (0.000 sec)
MariaDB [(none)]> select user,host from mysql.user;
+------+-----------+
| user | host      |
+------+-----------+
| root | 127.0.0.1 |
| root | ::1       |
| blog | localhost |
| root | localhost |
+------+-----------+
4 rows in set (0.000 sec)
  • 删除或更改用户信息后,要刷新用户权限信息
  • 命令:flush privileges;
shell 复制代码
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.000 sec)

3.2 php

3.2.1 安装php

shell 复制代码
yum -y install php php-bcmath php-cli php-common php-devel php-embedded php-fpm php-gd php-intl php-mbstring php-mysqlnd php-opcache php-pdo   php-process php-xml php-json
  • 设置开启自启动
shell 复制代码
[root@web01 ~]# systemctl enable --now php-fpm

3.2.2 修改php配置文件

  • 修改php子配置文件:/etc/php-fpm.d/www.conf
  • 修改后参数:
shell 复制代码
[root@web01 ~]# grep -En '^(user|group|listen) ' /etc/php-fpm.d/www.conf
24:user = nginx
26:group = nginx
38:listen = 127.0.0.1:9000

3.2.3 检查语法并重启

shell 复制代码
[root@web01 ~]# php-fpm -t
[23-Jun-2025 20:42:42] NOTICE: configuration file /etc/php-fpm.conf test is successful
[root@web01 ~]# systemctl reload php-fpm.service 

3.3 nginx配置

3.3.1 添加站点配置文件

shell 复制代码
[root@web01 ~]# cat /etc/nginx/conf.d/blog.oldboy.cn.conf 
server {
	listen 80;
	server_name blog.oldboy.cn;
	root /app/code/blog;
	error_log /var/log/nginx/blog.oldboy.cn-error.log notice;
	access_log /var/log/nginx/blog.oldboy.cn-access.log main;

	location / {
		index index.php;
	}
	location ~ \.php$ {
		fastcgi_pass 127.0.0.1:9000;
		fastcgi_index index.php;
		fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
		include fastcgi_params;
	}
}

3.3.2 在站点跟目录添加php文件

shell 复制代码
[root@web01 /app/code/blog]# ll chk_*
-rw-r--r-- 1 nginx nginx 274  6月 23 15:16 chk_db.php
-rw-r--r-- 1 nginx nginx   7  6月 23 15:11 chk_ngx.html
-rw-r--r-- 1 nginx nginx  20  6月 23 15:12 chk_ngx.php
[root@web01 /app/code/blog]# cat chk_db.php 
<?php
//数据库地址
$db_host='localhost'
$db_user='blog'
$db_pass='1'
//数据库名字
$db_name='blog'

$link_id=mysqli_connect($db_host,$db_user,$db_pass,$db_name);
if($link_id){
	echo "mysql successflu by oldboy lidao996!\n";
}else{
	echo "connection failed!\n"
}
?>
[root@web01 /app/code/blog]# cat chk_ngx.html 
oldboy
[root@web01 /app/code/blog]# cat chk_ngx.php 
<?php
phpinfo();
?>

3.4 部署代码与设置权限

shell 复制代码
wget https://cn.wordpress.org/latest-zh_CN.zip
unzip *zh_CN.zip
mv wordpress/* /app/code/blog/
chown -R nginx.nginx /app/code/blog/
ll -d /app/code/blog/

3.5 配置本地hosts解析

4. 数据库迁移

4.1 备份旧数据库

  • mysqldump:mysql备份工具
    • --all-databases/-A:备份所有数据库
  • 管道+gzip:实时压缩大型数据
shell 复制代码
[root@web01 ~]# mysqldump -uroot -p1 --all-databases |gzip >web-db-all.sql.gz
[root@web01 ~]# ll web-db-all.sql.gz 
-rw-r--r-- 1 root root 442243  6月 23 20:57 web-db-all.sql.gz

4.2 导入备份数据

  • 先将备份文件传输到目标节点
shell 复制代码
scp web-db-all.ssql.gz root@db01:~
  • 将备份数据导入数据库
  • zcat:直接查看或处理 gzip 压缩文件(.gz)内容
shell 复制代码
zcat web-db-all.sql.gz |mysql -uroot -p
  • 进入数据库,更新用户权限表
shell 复制代码
flush privileges;

4.3 修改代码连接的数据库

shell 复制代码
# wordprass-数据库配置文件
/wp-config.php

[root@web01 /app/code/blog]# grep DB_ wp-config.php
define( 'DB_NAME','blog' );
define( 'DB_USER','blog' );
define( 'DB_PASSWORD','blog' );
define( 'DB_HOST','172.16.1.51' ); # 修改成目标数据服务器
define( 'DB_CHARSET','utf8mb4' );
define( 'DB_COLLATE','' )

5. 思维导图

https://kdocs.cn/join/gpuxq6r?f=101\r\n邀请你加入共享群「老男孩教育Linux运维99期-孙克旭」一起进行文档协作

相关推荐
yzx99101321 分钟前
服务器生成图片
运维·服务器
db_murphy1 小时前
Oracle数据块8KB、OS默认认块管理4KB,是否需调整大小为一致?
linux
小阳睡不醒4 小时前
小白成长之路-部署Zabbix7(二)
android·运维
mCell4 小时前
从删库到跑路?这50个Linux命令能保你职业生涯
linux·windows·macos
杰克逊的日记4 小时前
GPU运维常见问题处理
linux·运维·gpu
caolib5 小时前
无需云服务器的内网穿透方案 -- cloudflare tunnel
运维·服务器·内网穿透·tunnel·cloudflared
奇舞精选5 小时前
k8s基本概念初探
运维
誰能久伴不乏6 小时前
Linux系统调用概述与实现:深入浅出的解析
linux·运维·服务器
程序员学习随笔6 小时前
Linux进程深度解析(2):fork/exec写时拷贝性能优化与exit资源回收机制(进程创建和销毁)
linux·运维·服务器
mmoyula6 小时前
【RK3568 PWM 子系统(SG90)驱动开发详解】
android·linux·驱动开发