微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

LNMP是一种web服务环境组合,是由Linux、NginxMysqLPHP等组合搭建的,当其工作时,首先是用户通过浏览器输入域名请求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 |
+--------------------+
| @R_415_4045@ion_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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐