Nginx+PHP7+MySQL5.7(LNMP)環境配置
在前面幾篇文章中,我們已經介紹並演示安裝 Nginx 的幾種方式,在開始本篇文章的閱讀和實踐之前,建議先閱讀「Nginx安裝配置」:http://www.yiibai.com/nginx/nginx-install.html ,在上面文章的基礎之上,我們再添加 PHP7 的安裝配置以及MySQL的安裝配置,最後編寫一個簡單的PHP測試程序。
目錄:
- Nginx安裝配置
- PHP7安裝配置
- MySQL5.7安裝配置
1. Nginx安裝配置
如果需要一些特殊的功能,在包和端口不可用的情況下,也可以從源代碼編譯來安裝nginx。雖然源代碼編譯安裝更靈活,但這種方法對於初學者來說可能很複雜(建議初學者自己使用源代碼編譯安裝來安裝nginx)。有關更多信息,請參閱從源構建nginx。
在本文中,主要介紹從源代碼安裝nginx,這篇教程是基於CentOS7 64bit
系統來安裝的,非Centos系統不適用。現在我們就開始吧!
1.1 安裝前工作
首先更新系統軟件源,使用以下命令更新系統 -
[root@localhost ~]# yum update
有關兩個命令的一點解釋:
yum -y update
- 升級所有包,改變軟件設置和系統設置,系統版本內核都升級yum -y upgrade
- 升級所有包,不改變軟件設置和系統設置,系統版本升級,內核不改變
依賴包安裝
[root@localhost src]# yum -y install gcc gcc-c++ autoconf automake libtool make cmake
[root@localhost src]# yum -y install zlib zlib-devel openssl openssl-devel pcre-devel
1.2. 下載Nginx安裝源文件
源碼下載,可官網下載地址:http://nginx.org/en/download.html 下載並上傳到服務器(這裏選擇最新穩定版本:nginx-1.10.3
),如下圖所示 -
或直接在服務上執行以下命令下載 -
[root@localhost ~]# cd /usr/local/src
[root@localhost src]# wget -c http://nginx.org/download/nginx-1.10.3.tar.gz
解壓上面下載的文件 -
[root@localhost src]# tar zxvf nginx-1.10.3.tar.gz
在編譯之前還要做一些前期的準備工作,如:依懶包安裝,Nginx用戶和用戶組等。
1.3. 新建nginx用戶及用戶組
使用 root 用戶身份登錄系統,執行以下命令創建新的用戶。
[root@localhost src]# groupadd nginx
[root@localhost src]# useradd -g nginx -M nginx
useradd
命令的-M
參數用於不爲nginx
建立home
目錄
修改/etc/passwd
,使得nginx
用戶無法bash登陸(nginx用戶後面由/bin/bash
改爲/sbin/nologin
),
[root@localhost src]# vi /etc/passwd
然後找到有 nginx 那一行,把它修改爲(後面由/bin/bash
改爲/sbin/nologin
):
nginx:x:1002:1003::/home/nginx:/sbin/nologin
1.4. 編譯配置、編譯、安裝
下面我們進入解壓的nginx源碼目錄:/usr/local/src/
執行以下命令 -
[root@localhost ~]# cd /usr/local/src/nginx*
[root@localhost nginx-1.10.3]# pwd
/usr/local/src/nginx-1.10.3
[root@localhost nginx-1.10.3]#
[root@localhost nginx-1.10.3]# ./configure --prefix=/usr/local/nginx \
--pid-path=/usr/local/nginx/run/nginx.pid \
--with-http_ssl_module \
--user=nginx \
--group=nginx \
--with-pcre \
--without-mail_pop3_module \
--without-mail_imap_module \
--without-mail_smtp_module
注意:上面的反斜槓
\
表示換行繼續。
--prefix=/usr/local/nginx
指定安裝到 /usr/local/nginx
目錄下。
上面配置完成後,接下來執行編譯 -
[root@localhost nginx-1.10.3]# make
[root@localhost nginx-1.10.3]# make install
... ...
cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf.default'
test -d '/usr/local/nginx/run' \
|| mkdir -p '/usr/local/nginx/run'
test -d '/usr/local/nginx/logs' \
|| mkdir -p '/usr/local/nginx/logs'
test -d '/usr/local/nginx/html' \
|| cp -R html '/usr/local/nginx'
test -d '/usr/local/nginx/logs' \
|| mkdir -p '/usr/local/nginx/logs'
make[1]: Leaving directory `/usr/local/src/nginx-1.10.3'
[root@localhost nginx-1.10.3]#
上面編譯時間跟你的電腦配置相關,所以可能需要一些等待時間。
查看安裝後的程序版本:
[root@localhost nginx-1.10.3]# /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.10.3
修改Nginx默認端口(可選):
[root@localhost nginx-1.10.3]# vi /usr/local/nginx/conf/nginx.conf
找到 -
... ...
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
... ...
把上面的 80
修改爲你想要的端口,如:8080
。
修改配置後驗證配置是否合法:
[root@localhost nginx-1.10.3]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
啓動Nginx程序、查看進程 -
[root@localhost nginx-1.10.3]# /usr/local/nginx/sbin/nginx
[root@localhost nginx-1.10.3]# ps -ef | grep nginx
root 29151 1 0 22:01 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 29152 29151 0 22:01 ? 00:00:00 nginx: worker process
root 29154 2302 0 22:01 pts/0 00:00:00 grep --color=auto nginx
[root@localhost nginx-1.10.3]#
nginx停止、重啓
未添加nginx服務前對nginx的管理只能通過一下方式管理:
# nginx 管理的幾種方式 -
# 啓動Nginx
/usr/local/nginx/sbin/nginx
# 從容停止Nginx:
kill -QUIT 主進程號 # 如上一步中的 ps 命令輸出的 29151,就是 Nginx的主進程號
# 快速停止Nginx:
kill -TERM 主進程號
# 強制停止Nginx:
pkill -9 nginx
# 平滑重啓nginx
/usr/nginx/sbin/nginx -s reload
現在我們來看看安裝的Nginx的運行結果,可以簡單地使用curl
命令訪問localhost測試,結果如下 -
[root@localhost nginx-1.10.3]# curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@localhost nginx-1.10.3]#
或者也可以打開瀏覽訪問目標服務器的IP,在本示例中,服務器的IP地址是:192.168.0.195,所以打開瀏覽器訪問如下結果 -
提示: 如果沒有看到以上界面,在確保Nginx啓動的前提下,檢查SeLinux和防火牆是否已關閉。關閉防火牆命令:
systemctl stop firewalld.service
。
2. PHP7安裝配置
2.1 源碼下載
官網地址:php7下載
[root@localhost ~]# cd /usr/local/src
[root@localhost src]# wget -c http://cn2.php.net/distributions/php-7.1.3.tar.gz
解壓壓縮包:
[root@localhost src]# tar -xzvf php-7.*
[root@localhost src]# cd php-7*
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
或者常見大部分依懶包安裝 -
[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
2.3 源碼編譯、安裝
通過 ./configure –help
查看支持的編譯配置參數,如下所示 -
[root@localhost php-7.1.3]# ./configure --help
`configure' configures this package to adapt to many kinds of systems.
Usage: ./configure [OPTION]... [VAR=VALUE]...
To assign environment variables (e.g., CC, CFLAGS...), specify them as
VAR=VALUE. See below for descriptions of some of the useful variables.
Defaults for the options are specified in brackets.
Configuration:
-h, --helpdisplay this help and exit
--help=short display options specific to this package
--help=recursive display the short help of all the included packages
-V, --versiondisplayversion information and exit
-q, --quiet, --silent do not print `checking ...' messages
--cache-file=FILE cache test results inFILE [disabled]
-C, --config-cache alias for `--cache-file=config.cache'
-n, --no-create do not create output files
--srcdir=DIR find the sources inDIR [configure dir or `..']
Installation directories:
--prefix=PREFIX install architecture-independent files in PREFIX
[/usr/local]
--exec-prefix=EPREFIX install architecture-dependent files in EPREFIX
[PREFIX]
By default, `make install' will install all the files in
`/usr/local/bin', `/usr/local/lib' etc. You can specify
an installation prefix other than `/usr/local' using `--prefix',
for instance `--prefix=$HOME'.
For better control, use the options below.
PHP+Nginx組合的編譯配置命令 -
[root@localhost php-7.1.3]# ./configure --prefix=/usr/local/php7 \
--with-config-file-path=/usr/local/php7/etc \
--with-config-file-scan-dir=/usr/local/php7/etc/php.d \
--with-mcrypt=/usr/include \
--enable-mysqlnd \
--with-mysqli \
--with-pdo-mysql \
--enable-fpm \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--with-gd \
--with-iconv \
--with-zlib \
--enable-xml \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--enable-mbregex \
--enable-mbstring \
--enable-ftp \
--enable-gd-native-ttf \
--with-openssl \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-zip \
--enable-soap \
--without-pear \
--with-gettext \
--enable-session \
--with-curl \
--with-jpeg-dir \
--with-freetype-dir \
--enable-opcache
# 執行完成後的結果:
Generating files
configure: creating ./config.status
creating main/internal_functions.c
creating main/internal_functions_cli.c
+--------------------------------------------------------------------+
| License: |
| This software is subject to the PHP License, available in this |
| distribution in the file LICENSE. By continuing this installation |
| process, you are bound by the terms of this license agreement. |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point. |
+--------------------------------------------------------------------+
Thank you for using PHP.
config.status: creating php7.spec
config.status: creating main/build-defs.h
config.status: creating scripts/phpize
config.status: creating scripts/man1/phpize.1
config.status: creating scripts/php-config
config.status: creating scripts/man1/php-config.1
config.status: creating sapi/cli/php.1
config.status: creating sapi/fpm/php-fpm.conf
config.status: creating sapi/fpm/www.conf
config.status: creating sapi/fpm/init.d.php-fpm
config.status: creating sapi/fpm/php-fpm.service
config.status: creating sapi/fpm/php-fpm.8
config.status: creating sapi/fpm/status.html
config.status: creating sapi/cgi/php-cgi.1
config.status: creating ext/phar/phar.1
config.status: creating ext/phar/phar.phar.1
config.status: creating main/php_config.h
config.status: executing default commands
編譯 + 安裝,編譯源碼, 如下所示 -
$ make
Generating phar.php
Generating phar.phar
PEAR package PHP_Archive not installed: generated phar will require PHP's phar extension be enabled.
directorytreeiterator.inc
pharcommand.inc
directorygraphiterator.inc
invertedregexiterator.inc
clicommand.inc
phar.inc
Build complete.
Don't forget to run 'make test'.
## 對編譯結果進行測試:
[root@localhost php-7.1.3]# make test
## 很遺憾,我這裏make test報錯了,已反饋php test信息。
## 安裝程序至指定目錄:
[root@localhost php-7.1.3]# make install
Installing shared extensions: /usr/local/php7/lib/php/extensions/no-debug-non-zts-20160303/
Installing PHP CLI binary: /usr/local/php7/bin/
Installing PHP CLI man page: /usr/local/php7/php/man/man1/
Installing PHP FPM binary: /usr/local/php7/sbin/
Installing PHP FPM defconfig: /usr/local/php7/etc/
Installing PHP FPM man page: /usr/local/php7/php/man/man8/
Installing PHP FPM status page: /usr/local/php7/php/php/fpm/
Installing phpdbg binary: /usr/local/php7/bin/
Installing phpdbg man page: /usr/local/php7/php/man/man1/
Installing PHP CGI binary: /usr/local/php7/bin/
Installing PHP CGI man page: /usr/local/php7/php/man/man1/
Installing build environment: /usr/local/php7/lib/php/build/
Installing header files: /usr/local/php7/include/php/
Installing helper programs: /usr/local/php7/bin/
program: phpize
program: php-config
Installing man pages: /usr/local/php7/php/man/man1/
page: phpize.1
page: php-config.1
/usr/local/src/php-7.1.3/build/shtool install -c ext/phar/phar.phar /usr/local/php7/bin
ln -s -f phar.phar /usr/local/php7/bin/phar
Installing PDO headers: /usr/local/php7/include/php/ext/pdo/
[root@localhost php-7.1.3]#
查看安裝成功後的版本信息 -
[root@localhost local]# /usr/local/php7/bin/php -v
PHP 7.1.3 (cli) (built: Apr 13 2017 22:47:30) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
[root@localhost local]#
2.4. 修改配置
修改php
配置,查看php加載配置文件路徑:
[root@localhost local]# /usr/local/php7/bin/php -i | grep php.ini
Configuration File (php.ini) Path => /usr/local/php7/etc
[root@localhost local]#
php-7.1.3
源碼目錄下:
[root@localhost local]# ll /usr/local/src/php-7.1.3/ | grep ini
-rw-rw-r--. 1 yiibai yiibai 71063 Mar 14 09:17 php.ini-development
-rw-rw-r--. 1 yiibai yiibai 71095 Mar 14 09:17 php.ini-production
[root@localhost local]#
複製PHP的配置文件,使用以下命令 -
[root@localhost local]# cp /usr/local/src/php-7.1.3/php.ini-production /usr/local/php7/etc/php.ini
## 根據需要對`php.ini`配置進行配置修改,請自行參考官方文檔配置 。
[root@localhost local]# /usr/local/php7/bin/php -i | grep php.ini
Configuration File (php.ini) Path => /usr/local/php7/etc
Loaded Configuration File => /usr/local/php7/etc/php.ini
[root@localhost local]#
2.5 啓用php-fpm服務
上面我們在編譯php7
的時候,已經將fpm模塊編譯了,那麼接下來,我們要啓用php-fpm
。但是默認情況下它的配置文件和服務都沒有啓用,所以要我們自己來配置,先重命名並移動以下兩個文件:
[root@localhost local]# cd /usr/local/php7/etc
[root@localhost etc]# cp php-fpm.conf.default php-fpm.conf
[root@localhost etc]# cp php-fpm.d/www.conf.default php-fpm.d/www.conf
php-fpm
的具體配置這裏不做深入去詳解,因爲在編譯之前./configure
的時候,我們都已經確定了一些配置,比如運行fpm
的用戶和用戶組之類的,所以默認配置應該不會存在路徑問題和權限問題。
配置php-fpm的服務載入:
就像上面的nginx一樣,我們希望使用 service php-fpm start|stop|restart
這些操作來實現服務的重啓,但沒有像nginx那麼複雜,php編譯好之後,給我們提供了一個php-fpm
的程序。這個文件放在php編譯源碼目錄中:
[root@localhost local]# cd /usr/local/src/php-7.1.3/sapi/fpm/
## 或直接使用可執行文件: /usr/local/php7/sbin/php-fpm
[root@localhost local]# ls
[root@localhost local]# cp init.d.php-fpm /etc/init.d/php-fpm
[root@localhost local]# chmod +x /etc/init.d/php-fpm
[root@localhost local]# chkconfig --add php-fpm
[root@localhost local]# chkconfig php-fpm on
通過上面這個操作,我們就可以使用 service php-fpm start
來啓用php-fpm
了。用 ps -ef | grep php-fpm
看看進程吧。
[root@localhost fpm]# ps -ef | grep php-fpm
root 108421 1 0 23:19 ? 00:00:00 php-fpm: master process (/usr/local/php7/etc/php-fpm.conf)
nginx 108422 108421 0 23:19 ? 00:00:00 php-fpm: pool www
nginx 108423 108421 0 23:19 ? 00:00:00 php-fpm: pool www
root 108507 2285 0 23:23 pts/0 00:00:00 grep --color=auto php-fpm
[root@localhost fpm]#
這樣,PHP環境就安裝完成了,接下來我們通過Nginx代理集成PHP7,來實現Nginx+PHP服務。
3. Nginx代理集成PHP7配置
通過上面的操作,nginx
和php-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
如果你大致瞭解過nginx的配置,應該能夠很快分辨出這個配置文件裏面的結構,並且知道server
塊代表一個虛擬主機,要增加虛擬主機就再增加一個server
塊,而且這個conf
文件中也給出了例子。那麼怎麼代理php-fpm
呢?找到:
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /script$fastcgi_script_name;
# include fastcgi_params;
#}
把前面的#
註釋符號去掉,把script
改爲$document_root
最終如下:
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html/$fastcgi_script_name;
include fastcgi_params;
}
這樣就可以了,重新載入nginx
配置即可,使用以下命令 -
/usr/local/nginx/sbin/nginx -s reload
然後到/usr/local/nginx/html
去寫一個php文件:index.php
進行測試,文件:index.php
的代碼如下 -
<?php
phpinfo();
?>
現在訪問目錄IP,應該能看到結果如下 -
提示:如果無法打開,可能需要關閉防火牆,使用命令:
systemctl stop firewalld
附完整的Nginx配置(/usr/local/nginx/conf/nginx.conf
)文件內容:
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.html;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.html;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.html;
# }
#}
}
MySQL5.7安裝配置
MySQL5.7在Linux安裝有很多種方式,這裏爲了節省時間和減少文章的篇幅,我們基於系統集成環境安裝。當然如果想以編譯源代碼方式安裝的話,可以參考MySQL的官方文檔。
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選擇Red,download-yum選擇Red) Hat Enterprise Linux 7 / Oracle Linux 7 (Architecture Independent), RPM Package。
也可以直接進入CentOS系統下載安裝包:
wget http://repo.mysql.com//mysql57-community-release-el7-7.noarch.rpm
# 或者
wget http://cdn.mysql.com//Downloads/MySQL-5.7/mysql-community-server-5.7.18-1.el7.x86_64.rpm
如果新的系統還沒有wget
命令的話可以先:yum install wget
,一般都會有安裝了wget
命令工具。
添加選擇yum源 -
[root@localhost src]# yum localinstall mysql57-community-release-el7-7.noarch.rpm [root@localhost src]# yum repolist all | grep mysql mysql-connectors-community/x86_64 MySQL Connectors Community enabled: 30 mysql-connectors-community-source MySQL Connectors Community - So disabled mysql-tools-community/x86_64 MySQL Tools Community enabled: 47 mysql-tools-community-source MySQL Tools Community - Source disabled mysql55-community/x86_64 MySQL 5.5 Community Server disabled mysql55-community-source MySQL 5.5 Community Server - So disabled mysql56-community/x86_64 MySQL 5.6 Community Server disabled mysql56-community-source MySQL 5.6 Community Server - So disabled mysql57-community/x86_64 MySQL 5.7 Community Server enabled: 187 mysql57-community-source MySQL 5.7 Community Server - So disabled
把需要安裝的啓用,其他的禁用。
- 安裝MySQL:
[root@localhost src]# yum install mysql-community-server
.....
Installing : mysql-community-server-5.7.18-1.el7.x86_64 4/6
Installing : mysql-community-libs-compat-5.7.18-1.el7.x86_64 5/6
Erasing : 1:mariadb-libs-5.5.52-1.el7.x86_64 6/6
Verifying : mysql-community-server-5.7.18-1.el7.x86_64 1/6
Verifying : mysql-community-common-5.7.18-1.el7.x86_64 2/6
Verifying : mysql-community-client-5.7.18-1.el7.x86_64 3/6
Verifying : mysql-community-libs-compat-5.7.18-1.el7.x86_64 4/6
Verifying : mysql-community-libs-5.7.18-1.el7.x86_64 5/6
Verifying : 1:mariadb-libs-5.5.52-1.el7.x86_64 6/6
Installed:
mysql-community-libs.x86_64 0:5.7.18-1.el7
mysql-community-libs-compat.x86_64 0:5.7.18-1.el7
mysql-community-server.x86_64 0:5.7.18-1.el7
Dependency Installed:
mysql-community-client.x86_64 0:5.7.18-1.el7
mysql-community-common.x86_64 0:5.7.18-1.el7
Replaced:
mariadb-libs.x86_64 1:5.5.52-1.el7
Complete!
[root@localhost src]#
- 安裝完成之後會自動在log中生成連接的密碼。
啓動MySQL:
[root@localhost src]# service mysqld start
Redirecting to /bin/systemctl start mysqld.service
[root@localhost src]# ps -axu|grep mysqld
mysql 2952 15.1 18.2 1127664 182008 ? Sl 05:15 0:01 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
root 2982 0.0 0.0 112648 964 pts/0 R+ 05:15 0:00 grep --color=auto mysqld
[root@localhost src]#
查看root
用戶的密碼:
[root@localhost src]# grep password /var/log/mysqld.log
2017-04-16T09:15:17.046285Z 1 [Note] A temporary password is generated for root@localhost: afWrxaqQi0!M
[root@localhost src]#
如上面所示,root
用戶的密碼爲:afWrxaqQi0!M
。現在我們使用上面的密碼連接到MySQL數據。
[root@localhost src]# mysql -uroot -p
password:
[root@localhost src]# show databases;
#ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> ALTER USER root@localhost IDENTIFIED BY 'Pass@123456';
mysql> flush privileges;
創建一個簡單的表:tb_user -
create database test;
use test;
create table tb_user(
id int(10) not null auto_increment primary key,
username varchar(64) default ''
);
insert into tb_user (id,username) values(1, 'maxsu');
insert into tb_user (id,username) values(2, 'minsu');
好了已經可以成功連接了,默認不能遠程連接,在使用數據庫之前,MySQL服務器要求你必須先修改原密碼。另外如果需要開機啓動的話,可以自行搜索解決。
PHP7連接MySQL
PHP5中可以使用 mysql extension
,mysqli
和 PDO_MYSQL
。但是在PHP7中移除了mysql extension
,只剩下後面兩種選擇。
PHP 提供了三種不同的API去連接mysql數據庫。下面的示例代碼展示了3種不同連接mysql數據庫的方式。
連接方式-1
文件:mysqli.php 代碼如下 -
<?php
/*
* mysqli
* 數據庫地址,登陸賬號,密碼,數據庫名稱
*/
$mysqli = new mysqli("127.0.0.1", "root", "Pass@123456", "test");
if($mysqli){
echo 'Connected to MySQL Success.';
}else{
echo 'Connected to MySQL Fail.';
}
echo '<hr/>';
$sql = "SELECT * FROM tb_user";
$result = $mysqli->query($sql);
if ($result) {
while ($row = $result->fetch_assoc()) {
echo 'Username: '.$row['username']. '<br/>';
}
}
/* free result set */
$result->free();
/* close connection */
$mysqli->close();
?>
將文件:mysqli.php 放到 /usr/local/nginx/html
目錄下,打開瀏覽器訪問測試結果如下 -
連接方式-2
文件:pdo.php 代碼如下 -
<?php
/*
* 第一個參數是mysql:host,第二是dbname,第三個賬戶名,第四個密碼
*/
try {
$pdo = new PDO("mysql:host=127.0.0.1;dbname=test", "root", "Pass@123456");
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
$sql = "select * from tb_user";
echo $sql . "<hr/>";
$pdo->query('set names utf8;');
$result = $pdo->query($sql);
if($result){
$rows = $result->fetchAll();
foreach ($rows as $row) {
$username = $row[1];
echo 'Username: '.$username.'<br/>';
}
}
將文件:pdo.php 放到 /usr/local/nginx/html
目錄下,打開瀏覽器訪問測試結果如下 -