程序笔记   发布时间:2022-07-19  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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 mysqluseradd:“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 -Erz waiTing to receive.[root@web1 tools]# ls@H_242_31@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@H_242_31@mysql-5.7.26-linux-glibc2.12-x86_64 nginx-1.16.0@H_242_31@mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz nginx-1.16.0.tar.gz[root@web1 tools]# @H_728_7@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总用量 0lrwxrwxrwx 1 root root 26 6月 21 10:57 @H_242_31@mysql -> /application/mysql-5.7.26/drwxr-xr-x 9 root root 129 6月 21 10:52 mysql-5.7.26lrwxrwxrwx. 1 root root 25 6月 2 19:43 nginx -> /application/nginx-1.16.0drwxr-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]# lsbin 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

[@H_735_71@mysqld] #服务器模块名称basedir = /application/mysql/ #MySQL安装目录datadir = /application/mysql/data #MySQL数据文件目录socket = /tmp/mysql.sock #MySQL服务器sock文件目录server_id = 1 #MySQL实例IDport = 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, langpacksLoading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.combase

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

省略部分输出

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

创建数据文件目录并授权

[root@web1 mysql]# @H_242_31@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    #@R_675_10922@据库(如果未出现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=457902021-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 wangjuDocumentation=man:mysql(8)Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.htmlAfter=network.targetAfter=syslog.target[Install]WantedBy=multi-user.target[service]User=mysqlGroup=mysqlExecStart=/application/mysql/bin/mysqld --defaults-file=/etc/my.cnfLimitNOFILE = 5000

启动MySQL数据库

[root@web1 mysql]# systemctl start mysqld

[root@web1 mysql]# systemctl enable mysqld

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

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

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

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

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

 

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

 

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

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

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

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

[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]# @H_728_7@mysql   #登录到数据库Welcome to the MySQL monitor. Commands end with ; or g.Your MySQL connection id is 2Server 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 itsaffiliates. Other names may be trademarks of their respectiveowners.

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

wangju [(nonE)]>show databases;       #查看当前的数据库+--------------------+| Database |+--------------------+| information_scheR_798_11845@a || mysql || perfoRMANce_scheR_798_11845@a || 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的安装全部内容,希望文章能够帮你解决LNMP应用环境介绍之MySQL的安装所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。