安装MySQL数据库

 

安装步骤介绍

本例采用MySQL二进制安装包进行安装演示

(1) 创建mysql用户的账号

 
  1. [root@mysql ~]# groupadd mysql
  2. [root@mysql ~]# useradd -s /sbin/nologin -g mysql -M mysql
  3. [root@localhost ~]# tail -1 /etc/passwd
  4. mysql:x:500:501::/home/mysql:/sbin/nologin
  5. [root@mysql ~]# id mysql
  6. uid=500(mysql) gid=501(mysql) groups=501(mysql)

(2)获取MySQL二进制软件包

百度云盘:http://pan.baidu.com/s/1hrBCzsC 
提取码:4yjf

image_1ct4mm2th1rn64vh1ea8kjc9b12p.png-57.9kB
(3) 采用二进制方式安装MySQL

  1. [root@mysql ~]# tar xf mysql-5.5.32-linux2.6-x86_64.tar.gz -C /usr/local/
  2. [root@mysql ~]# cd /usr/local/
  3. [root@mysql local]# mv mysql-5.5.32-linux2.6-x86_64 mysql-5.5.32
  4. [root@mysql local]# ln -s mysql-5.5.32 mysql
  5. [root@mysql local]# ls
  6. bin etc games include lib lib64 libexec mysql mysql-5.5.32 sbin share src
  7. [root@mysql local]# cd /usr/local/mysql
  8. [root@mysql mysql]# ls
  9. bin data include lib mysql-test scripts sql-bench
  10. COPYING docs INSTALL-BINARY man README share support-files
  11. #提示:
  12. 二进制安装包,仅需要解压就可以了,不需要执行cmake/configure,make,make install等过程

(4)初始化MySQL配置文件my.cnf

 
  1. [root@mysql mysql]# ls -l support-files/*.cnf
  2. -rw-r--r--. 1 7161 wheel 4691 Jun 19 2013 support-files/my-huge.cnf
  3. -rw-r--r--. 1 7161 wheel 19759 Jun 19 2013 support-files/my-innodb-heavy-4G.cnf
  4. -rw-r--r--. 1 7161 wheel 4665 Jun 19 2013 support-files/my-large.cnf
  5. -rw-r--r--. 1 7161 wheel 4676 Jun 19 2013 support-files/my-medium.cnf
  6. -rw-r--r--. 1 7161 wheel 2840 Jun 19 2013 support-files/my-small.cnf
  7. [root@mysql mysql]# /bin/cp support-files/my-small.cnf /etc/my.cnf

提示: 
- support-files下有my.cnf的各种配置样例。 
- 使用cp全路径/bin/cp,可实现拷贝而不出现替换提示,即如果有重名文件会直接覆盖 
- 本例为测试安装环境,因此选择参数配置小的my-small.cnf配置模版,如果是生产环境可以根据硬件选择更高级的配置文件,上述配置文件模版对硬件的要求从低到高依次为

 
  1. my-medium.cnf (最低)
  2. my-small.cnf
  3. my-large.cnf
  4. my-huge.cnf
  5. my-innodb-heavy-4G.cnf(最高)

(5)初始化MySQL数据库文件 

初始化命令如下

 
  1. [root@mysql mysql]# mkdir -p /usr/local/mysql/data
  2. [root@mysql mysql]# chown -R mysql.mysql /usr/local/mysql
  3. [root@mysql mysql]# yum -y install libaio
  4. [root@mysql mysql]# /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql
  5. #初始化MySQL数据库文件,会有很多信息提示,如果没有ERROR级别的错误,会有两个OK的字样,表示初始化成功,否则就要解决初始化的问题
 

配置并启动MySQL数据库

(1)设置MySQL启动脚本,命令如下

 
  1. [root@mysql mysql]# cp support-files/mysql.server /etc/init.d/mysqld
  2. #拷贝MySQL启动脚本到MySQL的命令路径
  3. [root@mysql mysql]# chmod +x /etc/init.d/mysqld
  4. #使脚本可执行

(2)MySQL二进制默认安装路径是/usr/local/mysql,启动脚本里是/usr/local/mysql。如果安装路径不同,那么脚本里路径等都需要替换 
(3)启动MySQL数据库,命令如下:

 
  1. [root@mysql mysql]# /etc/init.d/mysqld start
  2. Starting MySQL... SUCCESS!
  3. [root@mysql mysql]# netstat -antup | grep mysql
  4. tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 1347/mysqld

如果发现3306端口没起来,请tail -100 /usr/local/mysql/data/主机名.err查看日志信息,看是否有报错信息,然后根据相关错误提示进行调试。经常查看服务运行日志是个很好的习惯,也是高手的习惯。

(4)设置MySQL开机自启动,命令如下:

  1. [root@mysql mysql]# chkconfig --add mysqld
  2. [root@mysql mysql]# chkconfig mysqld on
  3. [root@mysql mysql]# chkconfig --list mysqld
  4. mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off

(5)配置mysql命令的全局使用路径,命令如下:

 
  1. [root@mysql mysql]# ln -s /usr/local/mysql/bin/* /usr/local/bin/
  2. [root@mysql mysql]# which mysqladmin
  3. /usr/local/bin/mysqladmin

(6)登陆MySQL测试,命令如下:

 
  1. [root@mysql mysql]# mysql
  2. Welcome to the MySQL monitor. Commands end with ; or \g.
  3. Your MySQL connection id is 1
  4. Server version: 5.5.32 MySQL Community Server (GPL)
  5. Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
  6. Oracle is a registered trademark of Oracle Corporation and/or its
  7. affiliates. Other names may be trademarks of their respective
  8. owners.
  9. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 

MySQL安全配置

(1)为MySQL的root用户设置密码,命令如下:

  1. [root@mysql mysql]# mysqladmin -uroot password '666666'
  2. [root@mysql mysql]# mysql -uroot -p666666
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 4
  5. Server version: 5.5.32 MySQL Community Server (GPL)
  6. Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
  7. Oracle is a registered trademark of Oracle Corporation and/or its
  8. affiliates. Other names may be trademarks of their respective
  9. owners.
  10. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

(2)清理无用的MySQL用户及库,命令如下:

  1. mysql> select user,host from mysql.user;
  2. +------+-----------+
  3. | user | host |
  4. +------+-----------+
  5. | root | 127.0.0.1 |
  6. | root | ::1 |
  7. | | localhost |
  8. | root | localhost |
  9. | | mysql |
  10. | root | mysql |
  11. +------+-----------+
  12. 6 rows in set (0.00 sec)
  13. mysql> drop user "root"@"::1";
  14. Query OK, 0 rows affected (0.00 sec)
  15. mysql> drop user ""@"localhost";
  16. Query OK, 0 rows affected (0.05 sec)
  17. mysql> select user,host from mysql.user;
  18. +------+-----------+
  19. | user | host |
  20. +------+-----------+
  21. | root | 127.0.0.1 |
  22. | root | localhost |
  23. | | mysql |
  24. | root | mysql |
  25. +------+-----------+
  26. 4 rows in set (0.00 sec)
 

安装nginx

 

nginx的编译安装部署

  1. [root@nginx ~]# mount /dev/sr0 /media/cdrom/
  2. mount: block device /dev/sr0 is write-protected, mounting read-only
  3. [root@nginx ~]# yum install -y pcre-devel openssl-devel
  4. #wget -q http://nginx.org/download/nginx-1.10.2.tar.gz
  5. [root@nginx ~]# useradd -s /sbin/nologin -M www #创建程序用户
  6. [root@nginx ~]# tar xf nginx-1.10.2.tar.gz -C /usr/src/ #解压缩
  7. [root@nginx ~]# cd /usr/src/nginx-1.10.2
  8. [root@nginx nginx-1.10.2]# ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module #预配置
  9. [root@nginx nginx-1.10.2]# make && make install #编译和安装
  10. [root@nginx nginx-1.10.2]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/ #给命令做软连接,以便PATH能找到
  11. [root@nginx nginx-1.10.2]# /usr/local/nginx/sbin/nginx #启动nginx
 

配置nginx配置文件

  1. [root@nginx conf]# cat nginx.conf.default | egrep -v "#|^$" > nginx.conf
  2. [root@nginx conf]# vim nginx.conf
  3. [root@nginx conf]# cat nginx.conf
  4. worker_processes 1;
  5. events {
  6. worker_connections 1024;
  7. }
  8. http {
  9. include mime.types;
  10. default_type application/octet-stream;
  11. sendfile on;
  12. keepalive_timeout 65;
  13. server {
  14. listen 80;
  15. server_name www.yunjisuan.com;
  16. root /www;
  17. location / {
  18. index index.html index.htm;
  19. }
  20. error_page 500 502 503 504 /50x.html;
  21. location = /50x.html {
  22. root html;
  23. }
  24. }
  25. }
  26. # 检查配置文件
  27. [root@nginx conf]# /usr/local/nginx/sbin/nginx -t
  28. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  29. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  30. #加入映射文件
  31. [root@nginx conf]# echo "`hostname -I` www.yunjisuan.com" >> /etc/hosts
  32. [root@nginx conf]# cat /etc/hosts
  33. 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
  34. ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
  35. 192.168.200.20 www.yunjisuan.com
 

添加网页

  1. [root@nginx conf]# mkdir /www
  2. [root@nginx conf]# echo "`hostname -I` www.yunjisuan.com" >> /www/index.html
  3. [root@nginx conf]# chown -R www.www /www/ #PHP的程序用户也要弄成www
  4. [root@nginx conf]# curl www.yunjisuan.com
  5. 192.168.200.20 www.yunjisuan.com
 

安装PHP

 

检查安装PHP所需的lib库

PHP程序在开发及运行时会调用一些诸如zlib,gd等函数库,因此需要确认lib库是否已经安装,执行过程如下:

 
  1. [root@PHP ~]# rpm -qa zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel
  2. [root@PHP ~]# rpm -qa freetype-devel libpng-devel gd libcurl-devel libxslt-devel

提示:

1)每个lib一般都会存在对应的以“*-devel”命名的包,安装lib对应的-devel包后,对应的lib包就会自动安装好,例如安装gd-devel时就会安装gd。 
2)这些lib库不是必须安装的,但是目前的企业环境下一般都需要安装。否则,PHP程序运行时会出现问题,例如验证码无法显示等。

执行下面命令安装相关的lib软件包

 
  1. [root@PHP ~]# yum -y install zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel
  2. [root@PHP ~]# yum -y install freetype-devel libpng-devel gd libcurl-devel libxslt-devel

安装后的结果如下:

  1. [root@PHP ~]# rpm -qa zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel
  2. libxml2-devel-2.7.6-14.el6.x86_64
  3. zlib-devel-1.2.3-29.el6.x86_64
  4. libjpeg-turbo-devel-1.2.1-1.el6.x86_64
  5. #这里仅缺少libiconv-devel包
  6. [root@PHP ~]# rpm -qa freetype-devel libpng-devel gd libcurl-devel libxslt-devel
  7. libxslt-devel-1.1.26-2.el6_3.1.x86_64
  8. libcurl-devel-7.19.7-37.el6_4.x86_64
  9. libpng-devel-1.2.49-1.el6_2.x86_64
  10. gd-2.0.35-11.el6.x86_64
  11. freetype-devel-2.3.11-14.el6_3.1.x86_64

从以上结果看出,仅有libiconv-devel这个包没有安装,因为默认的yum源没有此包,后面会编译安装。

 

安装yum无法安装的libiconv库

 
  1. [root@PHP ~]# yum -y install wget
  2. [root@PHP ~]# wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
  3. [root@PHP ~]# ls
  4. anaconda-ks.cfg install.log install.log.syslog libiconv-1.14.tar.gz
  5. [root@PHP ~]# tar xf libiconv-1.14.tar.gz -C /usr/src/
  6. [root@PHP ~]# cd /usr/src/libiconv-1.14/
  7. [root@PHP libiconv-1.14]# ./configure --prefix=/usr/local/libiconv && make && make install
 

安装libmcrypt库

 
  1. 推荐使用简单的在线yum的方式安装:wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
  2. [root@PHP libiconv-1.14]# yum -y install libmcrypt-devel
 

安装mhash加密扩展库

 
  1. 推荐使用简单的在线yum的方式安装:wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
  2. [root@PHP ~]# yum -y install mhash
 

安装mcrvpt加密扩展库

  1. 推荐使用简单的在线yum的方式安装:wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
  2. [root@PHP ~]# yum -y install mcrypt
 

开始安装PHP(FastCGI方式)服务

 

获取PHP软件包

 
  1. [root@PHP ~]# wget http://cn2.php.net/get/php-5.3.28.tar.gz/from/this/mirror
 

解压配置PHP

 
  1. [root@PHP ~]# useradd -s /sbin/nologin -M www
  2. [root@PHP ~]# id www
  3. uid=500(www) gid=500(www) groups=500(www)
  4. [root@PHP ~]# tar xf php-5.3.28.tar.gz -C /usr/src/
  5. [root@PHP ~]# cd /usr/src/php-5.3.28/
  6. [root@PHP php-5.3.28]# ./configure --prefix=/usr/local/php5.3.28 --with-mysql=mysqlnd --with-iconv-dir=/usr/local/libiconv --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fpm --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --enable-short-tags --enable-zend-multibyte --enable-static --with-xsl --with-fpm-user=www --with-fpm-group=www --enable-ftp

-devel是安装前面命令有关的所有包 
yum install openssl openssl-devel 
执行上述命令后,最后的正确输出提示为下图 
image_1ct4rr0nc6ga48mokt164m1tg246.png-16.4kB

编译PHP

正确执行前文配置PHP软件的./configure系列命令后,就可以编译PHP软件了,具体操作过程如下:

 
  1. [root@PHP php-5.3.28]# ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64/ #分离式不用这步
  2. [root@PHP php-5.3.28]# touch ext/phar/phar.phar #分离式不用这步
  3. [root@PHP php-5.3.28]# make # 直接编译
  4. #make最后的正确提示
  5. Build complete.
  6. Don't forget to run 'make test'.
 

安装PHP生成文件到系统

 
  1. [root@PHP php-5.3.28]# make install
 

配置PHP引擎配置文件php.ini

(1)设置软链接以方便访问,命令如下

 
  1. [root@PHP php-5.3.28]# ln -s /usr/local/php5.3.28/ /usr/local/php
  2. [root@PHP php-5.3.28]# ls -l /usr/local/php
  3. lrwxrwxrwx. 1 root root 21 Nov 25 23:30 /usr/local/php -> /usr/local/php5.3.28/

(2)查看PHP配置默认模版文件,命令如下

 
  1. [root@PHP php-5.3.28]# ls php.ini*
  2. php.ini-development php.ini-production

(3)拷贝PHP配置文件到PHP默认目录,并更改文件名称为php.ini,命令如下

 
  1. [root@PHP php-5.3.28]# cp php.ini-production /usr/local/php/lib/php.ini
  2. [root@PHP php-5.3.28]# ls -l /usr/local/php/lib/php.ini
  3. -rw-r--r--. 1 root root 69627 Nov 25 23:32 /usr/local/php/lib/php.ini
 

配置PHP(FastCGI方式)的配置文件php-fpm.conf

  1. [root@PHP php-5.3.28]# cd /usr/local/php/etc/
  2. [root@PHP etc]# ls
  3. pear.conf php-fpm.conf.default
  4. [root@PHP etc]# cp php-fpm.conf.default php-fpm.conf
  5. #修改配置文件就是修改一下监听端口

image_1ct4t1b6t11cu9lels7145518r84j.png-21kB

 

启动PHP服务(FastCGI方式)

(1)启动PHP服务php-fpm,命令如下

 
  1. [root@PHP etc]# /usr/local/php/sbin/php-fpm

(2)检查PHP服务php-fpm的进程及启动端口的情况,命令如下:

 
  1. [root@PHP etc]# ps -ef | grep php-fpm
  2. root 30699 1 0 23:42 ? 00:00:00 php-fpm: master process (/usr/local/php5.3.28/etc/php-fpm.conf)
  3. www 30700 30699 0 23:42 ? 00:00:00 php-fpm: pool www
  4. www 30701 30699 0 23:42 ? 00:00:00 php-fpm: pool www
  5. root 30716 1046 0 23:43 pts/0 00:00:00 grep php-fpm
  6. [root@PHP etc]# lsof -i:9000
  7. COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
  8. php-fpm 30699 root 7u IPv4 146919 0t0 TCP 192.168.200.30:cslistener (LISTEN)
  9. php-fpm 30700 www 0u IPv4 146919 0t0 TCP 192.168.200.30:cslistener (LISTEN)
  10. php-fpm 30701 www 0u IPv4 146919 0t0 TCP 192.168.200.30:cslistener (LISTEN)
 

配置Nginx支持PHP程序请求访问

 

修改Nginx配置文件

  1. [root@nginx conf]# cat nginx.conf
  2. worker_processes 1;
  3. events {
  4. worker_connections 1024;
  5. }
  6. http {
  7. include mime.types;
  8. default_type application/octet-stream;
  9. sendfile on;
  10. keepalive_timeout 65;
  11. server {
  12. listen 80;
  13. server_name www.yunjisuan.com;
  14. root /www;
  15. location / {
  16. index index.html index.htm;
  17. }
  18. location ~ .*\.(php|php5)?$ {
  19. fastcgi_pass 192.168.200.30:9000;
  20. fastcgi_index index.php;
  21. include fastcgi.conf;
  22. }
  23. error_page 500 502 503 504 /50x.html;
  24. location = /50x.html {
  25. root html;
  26. }
  27. }
  28. }
  29. [root@nginx conf]# /usr/local/sbin/nginx -t
  30. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  31. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  32. [root@nginx conf]# /usr/local/sbin/nginx -s reload
 

测试

映射IP 
image_1ct4ueki4eods5irp964vb7650.png-14.8kB

image_1ct4uf6npbg910l9pq7d7218c55d.png-57.2kB

 

测试动态

 
  1. [root@nginx conf]# cd /www/
  2. [root@nginx www]# ls
  3. index.html
  4. [root@nginx www]# echo "huahua" > index.php
  5. [root@nginx www]# ls
  6. index.html index.php
  7. [root@PHP etc]# mkdir /www #php服务器里的目录必须和nginx服务器的一致
  8. [root@PHP www]# touch index.php
  9. [root@PHP www]# echo "123" >> index.php
  10. [root@PHP www]# cat index.php
  11. 123

image_1ct4v04fp18rb1b0e1rno1t2i1rr78a.png-49.8kB

加入数据库

 
  1. [root@mysql mysql]# mysql -uroot -p666666
  2. Welcome to the MySQL monitor. Commands end with ; or \g.
  3. Your MySQL connection id is 5
  4. Server version: 5.5.32 MySQL Community Server (GPL)
  5. Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
  6. Oracle is a registered trademark of Oracle Corporation and/or its
  7. affiliates. Other names may be trademarks of their respective
  8. owners.
  9. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  10. mysql> grant all on *.* to 'yunjisuan'@'%' identified by '666666';
  11. Query OK, 0 rows affected (0.00 sec)
 

创建网页

 
  1. #php上创建
  2. [root@PHP www]# cat test_mysql.php
  3. <?php
  4. //$link_id=mysql_connect('主机名','用户','密码');
  5. $link_id=mysql_connect('192.168.200.31','yunjisuan','666666');
  6. if($link_id){
  7. echo "mysql successful by hua !";
  8. }else{
  9. echo mysql_error();
  10. }
  11. ?>
  12. #nginx也要创建
 

测试

image_1ct51765g1j15apkhng9o31a6d8n.png-49.1kB