LNMP应用环境介绍之MySQL的安装

2021/7/2 19:23:50

本文主要是介绍LNMP应用环境介绍之MySQL的安装,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

LNMP是一种web服务环境组合,是由Linux、Nginx、MySQL、PHP等组合搭建的,当其工作时,首先是用户通过浏览器输入域名请求Nginx Web 服务,如果请求的是静态资源,则由Nginx解析返回给用户,如果是动态请求(以.php结尾),那么Nginx就会把它通过FastCGI接口(生产常用方法)发送给PHP 引擎服务(FastCGI进程php-fpm)进行解析,如果这个动态请求要读取数据库数据,那么PHP会继续向后请求MySQL数据库,以读取需要的数据,并最终通过Nginx服务把获取的数据返回给用户,这就是LNMP环境的基本请求顺序流程。

我们在部署LNMP之前首先安装软件MySQL,这里需要注意的是,MySQL安装要与Nginx安装在同一台机器上

首先创建MySQL用户的账户

[root@web1 ~]# useradd -s /sbin/nologin -g mysql -M mysql
useradd:“mysql”组不存在

[root@web1 ~]# groupadd mysql    #添加组
[root@web1 ~]# useradd -M -s /sbin/nologin mysql -g mysql     

[root@web1 ~]# id mysql      #创建成功
uid=8890(mysql) gid=10000(mysql) 组=10000(mysql)

建立存放所有软件的固定目录

[root@web1 ~]# mkdir -p /home/wangju/tools
[root@web1 ~]# cd /home/wangju/tools

获取MySQL软件包,从官网上下载二进制软件包,网址:https://downloads.mysql.com/archives/community/,然后将文件传输到Linux系统中的指定位置,并检查是否传输完成。

[root@web1 tools]# rz -E
rz waiting to receive.
[root@web1 tools]# ls
mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz nginx-1.16.0 nginx-1.16.0.tar.gz
[root@web1 tools]# echo $?    #出现0表示传输完成
0

[root@web1 tools]# tar xf mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz     #解压压缩包
[root@web1 tools]# echo $?  #解压成功
0
[root@web1 tools]# ls
mysql-5.7.26-linux-glibc2.12-x86_64 nginx-1.16.0
mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz nginx-1.16.0.tar.gz
[root@web1 tools]# mv mysql-5.7.26-linux-glibc2.12-x86_64 /application/mysql-5.7.26       #将解压后的文件移动并改名
[root@web1 tools]# ln -s /application/mysql-5.7.26/ /application/mysql      #将改名后的文件设置软连接
[root@web1 tools]# ll /application
总用量 0
lrwxrwxrwx 1 root root 26 6月 21 10:57 mysql -> /application/mysql-5.7.26/
drwxr-xr-x 9 root root 129 6月 21 10:52 mysql-5.7.26
lrwxrwxrwx. 1 root root 25 6月 2 19:43 nginx -> /application/nginx-1.16.0
drwxr-xr-x. 11 root root 151 6月 2 20:02 nginx-1.16.0

初始化MySQL的配置文件

[root@web1 tools]# cd /application/mysql
[root@web1 mysql]# find ./ -name "*.conf"    #查找带有.conf的配置文件,没有找到

[root@web1 mysql]# ls
bin COPYING docs include lib man README share support-files

[root@web1 mysql]# ll support-files   
总用量 24
-rw-r--r-- 1 7161 31415 773 4月 13 2019 magic
-rwxr-xr-x 1 7161 31415 1061 4月 13 2019 mysqld_multi.server
-rwxr-xr-x 1 7161 31415 894 4月 13 2019 mysql-log-rotate
-rwxr-xr-x 1 7161 31415 10576 4月 13 2019 mysql.server
[root@web1 mysql]# rpm -e --nodeps mariadb-libs     #卸载掉系统自动安装的mariadb库,防止冲突
[root@web1 mysql]# echo $?    #卸载成功
0

[root@web1 mysql]# vim /etc/my.cnf     #手动编写配置文件/etc/my.cnf

[mysqld] #服务器模块名称
basedir = /application/mysql/ #MySQL安装目录
datadir = /application/mysql/data #MySQL数据文件目录
socket = /tmp/mysql.sock #MySQL服务器sock文件目录
server_id = 1 #MySQL实例ID
port = 3306 #MySQL默认端口
log_error = /application/mysql/data/wangju_mysql.err #MySQL错误日志路径

[mysql] #MySQL客户端模块名
socket = /tmp/mysql.sock #MySQL客户端sock文件目录
prompt = wangju [\\d]> #MySQL登录提示符

初始化MySQL数据库文件

首先安装依赖的包

[root@web1 mysql]# yum install libaio-devel -y
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
base

-------------------------------------------------------------------------------

省略部分输出

-------------------------------------------------------------------------------

创建数据文件目录并授权

[root@web1 mysql]# mkdir /application/mysql/data  #建立数据文件目录
[root@web1 application]# chown -R mysql.mysql /application/mysql/   #给MySQL授权于用户mysql
[root@web1 application]# ls -ld /application/mysql/  
drwxr-xr-x 10 mysql mysql 141 6月 21 11:40 /application/mysql/
[root@web1 application]# /application/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/application/mysql/ --datadir=/application/mysql/data    #初始化数据库(如果未出现error就表示授权成功)
2021-06-21T03:47:35.198943Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-06-21T03:47:36.937244Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-06-21T03:47:37.182665Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-06-21T03:47:37.533723Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 6ba1bfd0-d243-11eb-bcb9-000c296b7730.
2021-06-21T03:47:37.591100Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-06-21T03:47:37.609538Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.

  --user=mysql    #MySQL的用户

  --basedir=/application/mysql/      #MySQL的根目录

  --datadir=/application/mysql/data       #MySQL数据文件目录

  --initialize-insecure         #关闭MySQL的安全策略,这里我们选择关闭

  --initialize      #开启MySQL的安全策略,高安全环境采用

配置并启动MySQL数据库

首先,设置启动脚本,在centos7 中使用systemctl 来管理任务

[root@web1 mysql]# vim /etc/systemd/system/mysqld.service
[root@web1 mysql]# cat /etc/systemd/system/mysqld.service
[Unit]
Description=MySQL Server by wangju
Documentation=man:mysql(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/application/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000

启动MySQL数据库

[root@web1 mysql]# systemctl start mysqld

[root@web1 mysql]# systemctl enable mysqld

[root@web1 mysql]# systemctl status mysqld     #这边出现报错

解决此错误的方法:首先检查环境变量,

[root@web1 etc]# vim /etc/profile #如果有如下的配置,则说明环境变量配置正确

 

 其次检查配置文件/etc/my.cnf中服务器的模块名称是否合适,注意一定是[mysqld],不能写错

 

最后检查是否 /application/mysql/data/support-files/mysql.server 文件的配置错误

经过如上的排查,我们的错误就已经解决

[root@web1 mysql]# systemctl status mysqld
● mysqld.service - MySQL Server by wangju
Loaded: loaded (/etc/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since 五 2021-07-02 14:50:33 CST; 57min ago
Docs: man:mysql(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Main PID: 1188 (mysqld)
Tasks: 28
CGroup: /system.slice/mysqld.service
└─1188 /application/mysql/bin/mysqld --defaults-file=/etc/my.cnf

7月 02 14:50:33 web1 systemd[1]: Started MySQL Server by wangju.

登录到数据库

[root@web1 mysql]# mysql   #登录到数据库
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.26 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

wangju [(none)]>show databases;       #查看当前的数据库
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.17 sec)

wangju [(none)]>select user();   #查看当前的登录用户
+--------+
| user() |
+--------+
| root@ |
+--------+
1 row in set (0.09 sec)

wangju [(none)]>select user,authentication_string,host from mysql.user;    #查看用户列表命令
+---------------+-------------------------------------------+-----------+
| user | authentication_string | host |
+---------------+-------------------------------------------+-----------+
| root | *5368F3DD951CE4035DA1A7228D7D796D9DF958BD | localhost |
| mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | localhost |
| mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | localhost |
+---------------+-------------------------------------------+-----------+
3 rows in set (0.30 sec)

wangju [(none)]>quit     #退出数据库
Bye

到这里,数据库就安装好了!



这篇关于LNMP应用环境介绍之MySQL的安装的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程