当前位置: 代码迷 >> 综合 >> php7+mysql
  详细解决方案

php7+mysql

热度:59   发布时间:2023-09-07 21:23:46.0

lnmp配置 php7+mysql5.7

好文章爱分享:https://www.yiibai.com/nginx/lnmp.html

作者:初生不惑

尊重原创特此标明作者

1. Nginx安装配置

如果需要一些特殊的功能,在包和端口不可用的情况下,也可以从源代码编译来安装nginx。虽然源代码编译安装更灵活,但这种方法对于初学者来说可能很复杂(建议初学者自己使用源代码编译安装来安装nginx)。有关更多信息,请参阅从源构建nginx。

在本文中,主要介绍从源代码安装nginx,这篇教程是基于CentOS7 64bit系统来安装的,非Centos系统不适用。现在我们就开始吧!

1.1 安装前工作

首先更新系统软件源,使用以下命令更新系统 -

[root@localhost ~]# yum update

      
Shell

有关两个命令的一点解释:
yum -y update - 升级所有包,改变软件设置和系统设置,系统版本内核都升级
yum -y upgrade - 升级所有包,不改变软件设置和系统设置,系统版本升级,内核不改变

依赖包安装


      
  1. [root@localhost src]# yum -y install gcc gcc-c++ autoconf automake libtool make cmake
  2. [root@localhost src]# yum -y install zlib zlib-devel openssl openssl-devel pcre-devel
Shell

1.2. 下载Nginx安装源文件

源码下载,可官网下载地址:http://nginx.org/en/download.html 下载并上传到服务器(这里选择最新稳定版本:nginx-1.10.3),如下图所示 -

或直接在服务上执行以下命令下载 -


      
  1. [root@localhost ~]# cd /usr/local/src
  2. [root@localhost src]# wget -c http://nginx.org/download/nginx-1.10.3.tar.gz
Shell

解压上面下载的文件 -

[root@localhost src]# tar zxvf nginx-1.10.3.tar.gz

      
Shell

在编译之前还要做一些前期的准备工作,如:依懒包安装,Nginx用户和用户组等。

1.3. 新建nginx用户及用户组

使用 root 用户身份登录系统,执行以下命令创建新的用户。


      
  1. [root@localhost src]# groupadd nginx
  2. [root@localhost src]# useradd -g nginx -M nginx
Shell

useradd命令的-M参数用于不为nginx建立home目录
修改/etc/passwd,使得nginx用户无法bash登陆(nginx用户后面由/bin/bash改为/sbin/nologin),

[root@localhost src]# vi /etc/passwd

      
Shell

然后找到有 nginx 那一行,把它修改为(后面由/bin/bash改为/sbin/nologin):

nginx:x:1002:1003::/home/nginx:/sbin/nologin

      

1.4. 编译配置、编译、安装

下面我们进入解压的nginx源码目录:/usr/local/src/ 执行以下命令 -


      
  1. [root@localhost ~] # cd /usr/local/src/nginx*
  2. [root@localhost nginx- 1.10. 3] # pwd
  3. /usr/ local/src/nginx- 1.10. 3
  4. [root@localhost nginx- 1.10. 3] #
  5. [root@localhost nginx- 1.10. 3] # ./configure --prefix=/usr/local/nginx \
  6. --pid-path= /usr/local /nginx/run /nginx.pid \
  7. --with-http_ssl_module \
  8. --user=nginx \
  9. --group=nginx \
  10. --with-pcre \
  11. --without-mail_pop3_module \
  12. --without-mail_imap_module \
  13. --without-mail_smtp_module

注意:上面的反斜杠\ 表示换行继续。

--prefix=/usr/local/nginx 指定安装到 /usr/local/nginx 目录下。

上面配置完成后,接下来执行编译 -


      
  1. [root@localhost nginx-1.10.3]# make
  2. [root@localhost nginx-1.10.3]# make install
  3. ... ...
  4. cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf.default'
  5. test -d '/usr/local/nginx/run' \
  6. || mkdir -p '/usr/local/nginx/run'
  7. test -d '/usr/local/nginx/logs' \
  8. || mkdir -p '/usr/local/nginx/logs'
  9. test -d '/usr/local/nginx/html' \
  10. || cp -R html '/usr/local/nginx'
  11. test -d '/usr/local/nginx/logs' \
  12. || mkdir -p '/usr/local/nginx/logs'
  13. make[1]: Leaving directory `/usr/local/src/nginx-1.10.3'
  14. [root@localhost nginx-1.10.3]#
Shell

上面编译时间跟你的电脑配置相关,所以可能需要一些等待时间。

查看安装后的程序版本:


      
  1. [root@localhost nginx-1.10.3]# /usr/local/nginx/sbin/nginx -v
  2. nginx version: nginx/1.10.3
Shell

修改Nginx默认端口(可选):

[root@localhost nginx-1.10.3]# vi /usr/local/nginx/conf/nginx.conf

      

找到 -


      
  1. ... ...
  2. #gzip on;
  3. server {
  4. listen 80;
  5. server_name localhost;
  6. #charset koi8-r;
  7. ... ...
Shell

把上面的 80 修改为你想要的端口,如:8080 。
修改配置后验证配置是否合法:


      
  1. [root@localhost nginx-1.10.3]# /usr/local/nginx/sbin/nginx -t
  2. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  3. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
Shell

启动Nginx程序、查看进程 -


      
  1. [root@localhost nginx-1.10.3]# /usr/local/nginx/sbin/nginx
  2. [root@localhost nginx-1.10.3]# ps -ef | grep nginx
  3. root 29151 1 0 22:01 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
  4. nginx 29152 29151 0 22:01 ? 00:00:00 nginx: worker process
  5. root 29154 2302 0 22:01 pts/0 00:00:00 grep --color=auto nginx
  6. [root@localhost nginx-1.10.3]#
Shell

nginx停止、重启
未添加nginx服务前对nginx的管理只能通过一下方式管理:


      
  1. # nginx 管理的几种方式 -
  2. # 启动Nginx
  3. /usr/ local/nginx/sbin/nginx
  4. # 从容停止Nginx:
  5. kill -QUIT 主进程号 # 如上一步中的 ps 命令输出的 29151,就是 Nginx的主进程号
  6. # 快速停止Nginx:
  7. kill -TERM 主进程号
  8. # 强制停止Nginx:
  9. pkill - 9 nginx
  10. # 平滑重启nginx
  11. /usr/nginx/sbin/nginx - s reload

现在我们来看看安装的Nginx的运行结果,可以简单地使用curl命令访问localhost测试,结果如下 -


      
  1. [root@localhost nginx-1.10.3]# curl localhost
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <title>Welcome to nginx! </title>
  6. <style>
  7. body {
  8. width: 35em;
  9. margin: 0 auto;
  10. font-family: Tahoma, Verdana, Arial, sans-serif;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <h1>Welcome to nginx! </h1>
  16. <p>If you see this page, the nginx web server is successfully installed and
  17. working. Further configuration is required. </p>
  18. <p>For online documentation and support please refer to
  19. <a href="http://nginx.org/">nginx.org </a>. <br/>
  20. Commercial support is available at
  21. <a href="http://nginx.com/">nginx.com </a>. </p>
  22. <p> <em>Thank you for using nginx. </em> </p>
  23. </body>
  24. </html>
  25. [root@localhost nginx-1.10.3]#

或者也可以打开浏览访问目标服务器的IP,在本示例中,服务器的IP地址是:192.168.0.195,所以打开浏览器访问如下结果 -

提示: 如果没有看到以上界面,在确保Nginx启动的前提下,检查SeLinux和防火墙是否已关闭。关闭防火墙命令:systemctl stop firewalld.service

2. PHP7安装配置

2.1 源码下载

官网地址:php7下载


      
  1. [root@localhost ~]# cd /usr/local/src
  2. [root@localhost src]# wget -c http://cn2.php.net/distributions/php-7.1.3.tar.gz
Shell

解压压缩包:


      
  1. [root@localhost src]# tar -xzvf php-7.*
  2. [root@localhost src]# cd php-7*
Shell

2.2 安装编译所需依赖包

[root@localhost php-7.1.3]# yum -y install libxml2 libxml2-devel openssl openssl-devel curl-devel libjpeg-devel libpng-devel freetype-devel libmcrypt-devel

      
Shell

或者常见大部分依懒包安装 -

[root@localhost php-7.1.3]# yum install -y wget gcc gcc-c++ autoconf libjpeg libjpeg-devel perl perl* perl-CPAN libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers png jpeg autoconf gcc cmake make gcc-c++ gcc ladp ldap* ncurses ncurses-devel zlib zlib-devel zlib-static pcre pcre-devel pcre-static openssl openssl-devel perl libtoolt openldap-devel libxml2-devel ntpdate cmake gd* gd2 ImageMagick-devel jpeg jpeg* pcre-dev* fontconfig libpng libxml2 zip unzip gzip

      
Shell

2.3 源码编译、安装

通过 ./configure –help 查看支持的编译配置参数,如下所示 -


      
  1. [root@localhost php-7.1.3]# ./configure --help
  2. `configure' configures this package to adapt to many kinds of systems.
  3. Usage: ./configure [OPTION]... [VAR=VALUE]...
  4. To assign environment variables (e.g., CC, CFLAGS...), specify them as
  5. VAR=VALUE. See below for descriptions of some of the useful variables.
  6. Defaults for the options are specified in brackets.
  7. Configuration:
  8. -h, --helpdisplay this help and exit
  9. --help=short display options specific to this package
  10. --help=recursive display the short help of all the included packages
  11. -V, --versiondisplayversion information and exit
  12. -q, --quiet, --silent do not print `checking ...' messages
  13. --cache-file=FILE cache test results inFILE [disabled]
  14. -C, --config-cache alias for `--cache-file=config.cache'
  15. -n, --no-create do not create output files
  16. --srcdir=DIR find the sources inDIR [configure dir or `..']
  17. Installation directories:
  18. --prefix=PREFIX install architecture-independent files in PREFIX
  19. [/usr/local]
  20. --exec-prefix=EPREFIX install architecture-dependent files in EPREFIX
  21. [PREFIX]
  22. By default, `make install' will install all the files in
  23. `/usr/local/bin', `/usr/local/lib' etc. You can specify
  24. an installation prefix other than `/usr/local' using `--prefix',
  25. for instance `--prefix=$HOME'.
  26. For better control, use the options below.
Shell

PHP+Nginx组合的编译配置命令 -


      
  1. [root@localhost php-7.1.3]# ./configure --prefix=/usr/local/php7 \
  2. --with-config-file-path=/usr/local/php7/etc \
  3. --with-config-file-scan-dir=/usr/local/php7/etc/php.d \
  4. --with-mcrypt=/usr/include \
  5. --enable-mysqlnd \
  6. --with-mysqli \
  7. --with-pdo-mysql \
  8. --enable-fpm \
  9. --with-fpm-user=nginx \
  10. --with-fpm-group=nginx \
  11. --with-gd \
  12. --with-iconv \
  13. --with-zlib \
  14. --enable-xml \
  15. --enable-shmop \
  16. --enable-sysvsem \
  17. --enable-inline-optimization \
  18. --enable-mbregex \
  19. --enable-mbstring \
  20. --enable-ftp \
  21. --enable-gd-native-ttf \
  22. --with-openssl \
  23. --enable-pcntl \
  24. --enable-sockets \
  25. --with-xmlrpc \
  26. --enable-zip \
  27. --enable-soap \
  28. --without-pear \
  29. --with-gettext \
  30. --enable-session \
  31. --with-curl \
  32. --with-jpeg-dir \
  33. --with-freetype-dir \
  34. --enable-opcache
  35. # 执行完成后的结果:
  36. Generating files
  37. configure: creating ./config.status
  38. creating main/internal_functions.c
  39. creating main/internal_functions_cli.c
  40. +--------------------------------------------------------------------+
  41. | License: |
  42. | This software is subject to the PHP License, available in this |
  43. | distribution in the file LICENSE. By continuing this installation |
  44. | process, you are bound by the terms of this license agreement. |
  45. | If you do not agree with the terms of this license, you must abort |
  46. | the installation process at this point. |
  47. +--------------------------------------------------------------------+
  48. Thank you for using PHP.
  49. config.status: creating php7.spec
  50. config.status: creating main/build-defs.h
  51. config.status: creating scripts/phpize
  52. config.status: creating scripts/man1/phpize.1
  53. config.status: creating scripts/php-config
  54. config.status: creating scripts/man1/php-config.1
  55. config.status: creating sapi/cli/php.1
  56. config.status: creating sapi/fpm/php-fpm.conf
  57. config.status: creating sapi/fpm/www.conf
  58. config.status: creating sapi/fpm/init.d.php-fpm
  59. config.status: creating sapi/fpm/php-fpm.service
  60. config.status: creating sapi/fpm/php-fpm.8
  61. config.status: creating sapi/fpm/status.html
  62. config.status: creating sapi/cgi/php-cgi.1
  63. config.status: creating ext/phar/phar.1
  64. config.status: creating ext/phar/phar.phar.1
  65. config.status: creating main/php_config.h
  66. config.status: executing default commands
Shell

编译 + 安装,编译源码, 如下所示 -


      
  1. $ make
  2. Generating phar.php
  3. Generating phar.phar
  4. PEAR package PHP_Archive not installed: generated phar will require PHP's phar extension be enabled.
  5. directorytreeiterator.inc
  6. pharcommand.inc
  7. directorygraphiterator.inc
  8. invertedregexiterator.inc
  9. clicommand.inc
  10. phar.inc
  11. Build complete.
  12. Don't forget to run 'make test'.
  13. ## 对编译结果进行测试:
  14. [root@localhost php-7.1.3]# make test
  15. ## 很遗憾,我这里make test报错了,已反馈php test信息。
  16. ## 安装程序至指定目录:
  17. [root@localhost php-7.1.3]# make install
  18. Installing shared extensions: /usr/local/php7/lib/php/extensions/no-debug-non-zts-20160303/
  19. Installing PHP CLI binary: /usr/local/php7/bin/
  20. Installing PHP CLI man page: /usr/local/php7/php/man/man1/
  21. Installing PHP FPM binary: /usr/local/php7/sbin/
  22. Installing PHP FPM defconfig: /usr/local/php7/etc/
  23. Installing PHP FPM man page: /usr/local/php7/php/man/man8/
  24. Installing PHP FPM status page: /usr/local/php7/php/php/fpm/
  25. Installing phpdbg binary: /usr/local/php7/bin/
  26. Installing phpdbg man page: /usr/local/php7/php/man/man1/
  27. Installing PHP CGI binary: /usr/local/php7/bin/
  28. Installing PHP CGI man page: /usr/local/php7/php/man/man1/
  29. Installing build environment: /usr/local/php7/lib/php/build/
  30. Installing header files: /usr/local/php7/include/php/
  31. Installing helper programs: /usr/local/php7/bin/
  32. program: phpize
  33. program: php-config
  34. Installing man pages: /usr/local/php7/php/man/man1/
  35. page: phpize.1
  36. page: php-config.1
  37. /usr/local/src/php-7.1.3/build/shtool install -c ext/phar/phar.phar /usr/local/php7/bin
  38. ln -s -f phar.phar /usr/local/php7/bin/phar
  39. Installing PDO headers: /usr/local/php7/include/php/ext/pdo/
  40. [root@localhost php-7.1.3]#
Shell

查看安装成功后的版本信息 -


      
  1. [root@localhost local]# /usr/local/php7/bin/php -v
  2. PHP 7.1.3 (cli) (built: Apr 13 2017 22:47:30) ( NTS )
  3. Copyright (c) 1997-2017 The PHP Group
  4. Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
  5. [root@localhost local]#
Shell

2.4. 修改配置

修改php配置,查看php加载配置文件路径:


      
  1. [root@localhost local]# /usr/local/php7/bin/php -i | grep php.ini
  2. Configuration File (php.ini) Path => /usr/local/php7/etc
  3. [root@localhost local]#
Shell

php-7.1.3源码目录下:


      
  1. [root@localhost local]# ll /usr/local/src/php-7.1.3/ | grep ini
  2. -rw-rw-r--. 1 yiibai yiibai 71063 Mar 14 09:17 php.ini-development
  3. -rw-rw-r--. 1 yiibai yiibai 71095 Mar 14 09:17 php.ini-production
  4. [root@localhost local]#
Shell

复制PHP的配置文件,使用以下命令 -


      
  1. [root@localhost local]# cp /usr/local/src/php-7.1.3/php.ini-production /usr/local/php7/etc/php.ini
  2. ## 根据需要对`php.ini`配置进行配置修改,请自行参考官方文档配置 。
  3. [root@localhost local]# /usr/local/php7/bin/php -i | grep php.ini
  4. Configuration File (php.ini) Path => /usr/local/php7/etc
  5. Loaded Configuration File => /usr/local/php7/etc/php.ini
  6. [root@localhost local]#
Shell

2.5 启用php-fpm服务

上面我们在编译php7的时候,已经将fpm模块编译了,那么接下来,我们要启用php-fpm。但是默认情况下它的配置文件和服务都没有启用,所以要我们自己来配置,先重命名并移动以下两个文件:


      
  1. [root@localhost local]# cd /usr/local/php7/etc
  2. [root@localhost etc]# cp php-fpm.conf.default php-fpm.conf
  3. [root@localhost etc]# cp php-fpm.d/www.conf.default php-fpm.d/www.conf
Shell

php-fpm的具体配置这里不做深入去详解,因为在编译之前./configure的时候,我们都已经确定了一些配置,比如运行fpm的用户和用户组之类的,所以默认配置应该不会存在路径问题和权限问题。

配置php-fpm的服务载入:
就像上面的nginx一样,我们希望使用 service php-fpm start|stop|restart 这些操作来实现服务的重启,但没有像nginx那么复杂,php编译好之后,给我们提供了一个php-fpm的程序。这个文件放在php编译源码目录中:


      
  1. [root@localhost local]# cd /usr/local/src/php-7.1.3/sapi/fpm/
  2. ## 或直接使用可执行文件: /usr/local/php7/sbin/php-fpm
  3. [root@localhost local]# ls
  4. [root@localhost local]# cp init.d.php-fpm /etc/init.d/php-fpm
  5. [root@localhost local]# chmod +x /etc/init.d/php-fpm
  6. [root@localhost local]# chkconfig --add php-fpm
  7. [root@localhost local]# chkconfig php-fpm on
Shell

通过上面这个操作,我们就可以使用 service php-fpm start 来启用php-fpm了。用 ps -ef | grep php-fpm看看进程吧。


      
  1. [root@localhost fpm]# ps -ef | grep php-fpm
  2. root 108421 1 0 23:19 ? 00:00:00 php-fpm: master process (/usr/local/php7/etc/php-fpm.conf)
  3. nginx 108422 108421 0 23:19 ? 00:00:00 php-fpm: pool www
  4. nginx 108423 108421 0 23:19 ? 00:00:00 php-fpm: pool www
  5. root 108507 2285 0 23:23 pts/0 00:00:00 grep --color=auto php-fpm
  6. [root@localhost fpm]#
Shell

这样,PHP环境就安装完成了,接下来我们通过Nginx代理集成PHP7,来实现Nginx+PHP服务。

3. Nginx代理集成PHP7配置

通过上面的操作,nginxphp-fpm服务都已经正常运行起来了,但是php-fpm只是在127.0.0.1:9000上提供服务,外网是无法访问的,而且也不可能直接通过php-fpm给外网提供服务,因此需要使用nginx去代理9000端口执行php
实际上这个过程只需要对nginx进行配置即可,php-fpm已经在后台运行了,我们需要在nginx的配置文件中增加代理的规则,即可让用户在访问80端口,请求php的时候,交由后端的php-fpm去执行,并返回结果。现在编辑Nginx的配置文件 -

[root@localhost local]# vi /usr/local/nginx/conf/nginx.conf

      
Shell

如果你大致了解过nginx的配置,应该能够很快分辨出这个配置文件里面的结构,并且知道server块代表一个虚拟主机,要增加虚拟主机就再增加一个server块,而且这个conf文件中也给出了例子。那么怎么代理php-fpm呢?找到:


      
  1. #location ~ \.php$ {
  2. # root html;
  3. # fastcgi_pass 127.0.0.1:9000;
  4. # fastcgi_index index.php;
  5. # fastcgi_param SCRIPT_FILENAME /script$fastcgi_script_name;
  6. # include fastcgi_params;
  7. #}
Shell

把前面的#注释符号去掉,把script改为$document_root最终如下:


      
  1. location ~ \.php$ {
  2. root html;
  3. fastcgi_pass 127.0.0.1:9000;
  4. fastcgi_index index.php;
  5. fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html/$fastcgi_script_name;
  6. include fastcgi_params;
  7. }
Shell

这样就可以了,重新载入nginx配置即可,使用以下命令 -

/usr/local/nginx/sbin/nginx -s reload

      
Shell

然后到/usr/local/nginx/html去写一个php文件:index.php进行测试,文件:index.php的代码如下 -


      
  1. <?php
  2. phpinfo();
  3. ?>
PHP

现在访问目录IP,应该能看到结果如下 -

提示:如果无法打开,可能需要关闭防火墙,使用命令:systemctl stop firewalld

附完整的Nginx配置(/usr/local/nginx/conf/nginx.conf)文件内容:


      
  1. #user nobody;
  2. worker_processes 1;
  3. #error_log logs/error.log;
  4. #error_log logs/error.log notice;
  5. #error_log logs/error.log info;
  6. #pid logs/nginx.pid;
  7. events {
  8. worker_connections 1024;
  9. }
  10. http {
  11. include mime.types;
  12. default_type application/octet-stream;
  13. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  14. # '$status $body_bytes_sent "$http_referer" '
  15. # '"$http_user_agent" "$http_x_forwarded_for"';
  16. #access_log logs/access.log main;
  17. sendfile on;
  18. #tcp_nopush on;
  19. #keepalive_timeout 0;
  20. keepalive_timeout 65;
  21. #gzip on;
  22. server {
  23. listen 80;
  24. server_name localhost;
  25. #charset koi8-r;
  26. #access_log logs/host.access.log main;
  27. location / {
  28. root html;
  29. index index.html index.html;
  30. }
  31. #error_page 404 /404.html;
  32. # redirect server error pages to the static page /50x.html
  33. #
  34. error_page 500 502 503 504 /50x.html;
  35. location = /50x.html {
  36. root html;
  37. }
  38. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  39. #
  40. #location ~ \.php$ {
  41. # proxy_pass http://127.0.0.1;
  42. #}
  43. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  44. #
  45. #location ~ \.php$ {
  46. # root html;
  47. # fastcgi_pass 127.0.0.1:9000;
  48. # fastcgi_index index.php;
  49. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  50. # include fastcgi_params;
  51. #}
  52. # deny access to .htaccess files, if Apache's document root
  53. # concurs with nginx's one
  54. #
  55. #location ~ /\.ht {
  56. # deny all;
  57. #}
  58. }
  59. # another virtual host using mix of IP-, name-, and port-based configuration
  60. #
  61. #server {
  62. # listen 8000;
  63. # listen somename:8080;
  64. # server_name somename alias another.alias;
  65. # location / {
  66. # root html;
  67. # index index.html index.html;
  68. # }
  69. #}
  70. # HTTPS server
  71. #
  72. #server {
  73. # listen 443 ssl;
  74. # server_name localhost;
  75. # ssl_certificate cert.pem;
  76. # ssl_certificate_key cert.key;
  77. # ssl_session_cache shared:SSL:1m;
  78. # ssl_session_timeout 5m;
  79. # ssl_ciphers HIGH:!aNULL:!MD5;
  80. # ssl_prefer_server_ciphers on;
  81. # location / {
  82. # root html;
  83. # index index.html index.html;
  84. # }
  85. #}
  86. }
Shell

MySQL5.7安装配置

MySQL5.7在Linux安装有很多种方式,这里为了节省时间和减少文章的篇幅,我们基于系统集成环境安装。当然如果想以编译源代码方式安装的话,可以参考MySQL的官方文档。

  • http://dev.mysql.com/doc/refman/5.7/en/source-installation.html

Centos7.0安装Mysql5.7.11

在网上找了很多关于Centos7.0安装MySQL5.7.11的教程,找到靠谱的还得看运气,嘿嘿。

  • 检测下系统有没有自带的MySQL:yum list installed | grep mysql
    如果已经有的话执行命令yum -y remove mysql-libs.x86_64卸载已经安装的MySQL。

  • 先到MySQL官网下载5.7.11的安装包(http://dev.mysql.com/downloads/mysql/),download-yum选择RedHat Enterprise Linux 7 / Oracle Linux 7 (Architecture Independent), RPM Package。

也可以直接进入CentOS系统下载安装包:


      
  1. wget http://repo.mysql.com//mysql57-community-release-el7-7.noarch.rpm
  2. # 或者
  3. wget http://cdn.mysql.com//Downloads/MySQL-5.7/mysql-community-server-5.7.18-1.el7.x86_64.rpm
Shell

如果新的系统还没有wget命令的话可以先:yum install wget,一般都会有安装了wget命令工具。

  • 添加选择yum源 -
    
            
    1. [root@localhost src]# yum localinstall mysql57-community-release-el7-7.noarch.rpm
    2. [root@localhost src]# yum repolist all | grep mysql
    3. mysql-connectors-community/x86_64 MySQL Connectors Community enabled: 30
    4. mysql-connectors-community-source MySQL Connectors Community - So disabled
    5. mysql-tools-community/x86_64 MySQL Tools Community enabled: 47
    6. mysql-tools-community-source MySQL Tools Community - Source disabled
    7. mysql55-community/x86_64 MySQL 5.5 Community Server disabled
    8. mysql55-community-source MySQL 5.5 Community Server - So disabled
    9. mysql56-community/x86_64 MySQL 5.6 Community Server disabled
    10. mysql56-community-source MySQL 5.6 Community Server - So disabled
    11. mysql57-community/x86_64 MySQL 5.7 Community Server enabled: 187
    12. mysql57-community-source MySQL 5.7 Community Server - So disabled
    Shell

把需要安装的启用,其他的禁用。

  1. 安装MySQL:

      
  1. [root@localhost src]# yum install mysql-community-server
  2. .....
  3. Installing : mysql-community-server-5.7.18-1.el7.x86_64 4/6
  4. Installing : mysql-community-libs-compat-5.7.18-1.el7.x86_64 5/6
  5. Erasing : 1:mariadb-libs-5.5.52-1.el7.x86_64 6/6
  6. Verifying : mysql-community-server-5.7.18-1.el7.x86_64 1/6
  7. Verifying : mysql-community-common-5.7.18-1.el7.x86_64 2/6
  8. Verifying : mysql-community-client-5.7.18-1.el7.x86_64 3/6
  9. Verifying : mysql-community-libs-compat-5.7.18-1.el7.x86_64 4/6
  10. Verifying : mysql-community-libs-5.7.18-1.el7.x86_64 5/6
  11. Verifying : 1:mariadb-libs-5.5.52-1.el7.x86_64 6/6
  12. Installed:
  13. mysql-community-libs.x86_64 0:5.7.18-1.el7
  14. mysql-community-libs-compat.x86_64 0:5.7.18-1.el7
  15. mysql-community-server.x86_64 0:5.7.18-1.el7
  16. Dependency Installed:
  17. mysql-community-client.x86_64 0:5.7.18-1.el7
  18. mysql-community-common.x86_64 0:5.7.18-1.el7
  19. Replaced:
  20. mariadb-libs.x86_64 1:5.5.52-1.el7
  21. Complete!
  22. [root@localhost src]#
Shell
  • 安装完成之后会自动在log中生成连接的密码。

启动MySQL:


      
  1. [root@localhost src]# service mysqld start
  2. Redirecting to /bin/systemctl start mysqld.service
  3. [root@localhost src]# ps -axu|grep mysqld
  4. mysql 2952 15.1 18.2 1127664 182008 ? Sl 05:15 0:01 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
  5. root 2982 0.0 0.0 112648 964 pts/0 R+ 05:15 0:00 grep --color=auto mysqld
  6. [root@localhost src]#
Shell

查看root用户的密码:


      
  1. [root@localhost src]# grep password /var/log/mysqld.log
  2. 2017-04-16T09:15:17.046285Z 1 [Note] A temporary password is generated for root@localhost: afWrxaqQi0!M
  3. [root@localhost src]#
Shell

如上面所示,root用户的密码为:afWrxaqQi0!M。现在我们使用上面的密码连接到MySQL数据。


      
  1. [root@localhost src]# mysql -uroot -p
  2. password:
  3. [root@localhost src]# show databases;
  4. #ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
  5. mysql> ALTER USER root@localhost IDENTIFIED BY 'Pass@123456';
  6. mysql> flush privileges;
Shell

创建一个简单的表:tb_user -


      
  1. create database test;
  2. use test;
  3. create table tb_user(
  4. id int(10) not null auto_increment primary key,
  5. username varchar(64) default ''
  6. );
  7. insert into tb_user (id,username) values(1, 'maxsu');
  8. insert into tb_user (id,username) values(2, 'minsu');
SQL

好了已经可以成功连接了,默认不能远程连接,在使用数据库之前,MySQL服务器要求你必须先修改原密码。另外如果需要开机启动的话,可以自行搜索解决。

PHP7连接MySQL

PHP5中可以使用 mysql extensionmysqli 和 PDO_MYSQL。但是在PHP7中移除了mysql extension,只剩下后面两种选择。
PHP 提供了三种不同的API去连接mysql数据库。下面的示例代码展示了3种不同连接mysql数据库的方式。

连接方式-1

文件:mysqli.php 代码如下 -


      
  1. <?php
  2. /*
  3. * mysqli
  4. * 数据库地址,登陆账号,密码,数据库名称
  5. */
  6. $mysqli = new mysqli("127.0.0.1", "root", "Pass@123456", "test");
  7. if($mysqli){
  8. echo 'Connected to MySQL Success.';
  9. }else{
  10. echo 'Connected to MySQL Fail.';
  11. }
  12. echo '<hr/>';
  13. $sql = "SELECT * FROM tb_user";
  14. $result = $mysqli->query($sql);
  15. if ($result) {
  16. while ($row = $result->fetch_assoc()) {
  17. echo 'Username: '.$row['username']. '<br/>';
  18. }
  19. }
  20. /* free result set */
  21. $result->free();
  22. /* close connection */
  23. $mysqli->close();
  24. ?>
PHP

将文件:mysqli.php 放到 /usr/local/nginx/html 目录下,打开浏览器访问测试结果如下 -

连接方式-2

文件:pdo.php 代码如下 -


      
  1. <?php
  2. /*
  3. * 第一个参数是mysql:host,第二是dbname,第三个账户名,第四个密码
  4. */
  5. try {
  6. $pdo = new PDO("mysql:host=127.0.0.1;dbname=test", "root", "Pass@123456");
  7. } catch (PDOException $e) {
  8. echo 'Connection failed: ' . $e->getMessage();
  9. }
  10. $sql = "select * from tb_user";
  11. echo $sql . "<hr/>";
  12. $pdo->query('set names utf8;');
  13. $result = $pdo->query($sql);
  14. if($result){
  15. $rows = $result->fetchAll();
  16. foreach ($rows as $row) {
  17. $username = $row[1];
  18. echo 'Username: '.$username.'<br/>';
  19. }
  20. }
PHP

将文件:pdo.php 放到 /usr/local/nginx/html 目录下,打开浏览器访问测试结果如下 -




  相关解决方案