2016年9月

设置nginx连接php-fpm方式为socket

修改下面几项,用户和用户组的设置需要对应nginx的用户,不然会没有权限,我的nginx执行用户是 www

listen = /var/run/php-7.0.11-fpm.sock
listen.owner = www
listen.group = www

nginx 修改配置文件,这里的路径就是上面listen选项的路径

#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php-7.0.11-fpm.sock;

重启nginx和php-fpm,ok

编译安装nginx,php

安装nginx

安装pcre

为了支持rewrite功能,我们需要安装pcre

yum install -y pcre* //如过你已经装了,请跳过这一步

需要ssl的支持,如果不需要ssl支持,请跳过这一步

yum install -y openssl*

编译安装

./configure --prefix=/usr/local/nginx-1.8.1 \
--with-http_spdy_module \
--with-http_stub_status_module \
--with-pcre

--with-http_stub_status_module:支持nginx状态查询
--with-http_spdy_module:支持google的spdy,想了解请百度spdy,这个必须有ssl的支持
--with-pcre:为了支持rewrite重写功能,必须制定pcre

报错

checking for OS
 + Linux 2.6.32-642.4.2.el6.x86_64 x86_64
checking for C compiler ... not found

./configure: error: C compiler cc is not found

安装所需的软件

yum -y install gcc gcc-c++

再 configure

报错

./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=<path> option.

安装所需软件

yum install -y zlib-devel

再 configure

Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using builtin md5 code
  + sha1 library is not found
  + using system zlib library

  nginx path prefix: "/usr/local/nginx-1.8.1"
  nginx binary file: "/usr/local/nginx-1.8.1/sbin/nginx"
  nginx configuration prefix: "/usr/local/nginx-1.8.1/conf"
  nginx configuration file: "/usr/local/nginx-1.8.1/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx-1.8.1/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx-1.8.1/logs/error.log"
  nginx http access log file: "/usr/local/nginx-1.8.1/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

安装

make //确定你的服务器有安装make,如果没有安装请执行yum install make
make install

添加启动脚本

这个网页可以 https://www.nginx.com/resources/wiki/start/topics/examples/redhatnginxinit/ 下载启动脚本

#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig:   - 85 15
# description:  NGINX is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /etc/nginx/nginx.conf
# config:      /etc/sysconfig/nginx
# pidfile:     /var/run/nginx.pid

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

nginx="/usr/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/etc/nginx/nginx.conf"

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

lockfile=/var/lock/subsys/nginx

make_dirs() {
   # make required directories
   user=`$nginx -V 2>&1 | grep "configure arguments:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
   if [ -n "$user" ]; then
      if [ -z "`grep $user /etc/passwd`" ]; then
         useradd -M -s /bin/nologin $user
      fi
      options=`$nginx -V 2>&1 | grep 'configure arguments:'`
      for opt in $options; do
          if [ `echo $opt | grep '.*-temp-path'` ]; then
              value=`echo $opt | cut -d "=" -f 2`
              if [ ! -d "$value" ]; then
                  # echo "creating" $value
                  mkdir -p $value && chown -R $user $value
              fi
          fi
       done
    fi
}

start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    make_dirs
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    configtest || return $?
    stop
    sleep 1
    start
}

reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
}

force_reload() {
    restart
}

configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}

rh_status() {
    status $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac

修改下面脚本中两项为实际的路径

#nginx="/usr/sbin/nginx"
nginx="/usr/local/nginx-1.8.1/sbin/nginx"
#NGINX_CONF_FILE="/etc/nginx/nginx.conf"
NGINX_CONF_FILE="/usr/local/nginx-1.8.1/conf/nginx.conf"

把文件写入到 /etc/init.d/nginx

给权限,开启服务器

chmod +x /etc/init.d/nginx
service nginx start

php 编译安装,版本 php-7.0.11

取官方下载程序包

安装依赖包

yum install gcc make gd-devel libjpeg-devel libpng-devel libxml2-devel bzip2-devel libcurl-devel -y

编译安装(64位系统需要加上最后一个 --with-libdir=lib64)

./configure --prefix=/usr/local/php-7.0.11 \
--with-config-file-path=/usr/local/php-7.0.11/etc \
--with-bz2 \
--with-curl \
--enable-ftp \
--enable-sockets \
--disable-ipv6 \
--with-gd \
--with-jpeg-dir=/usr/local \
--with-png-dir=/usr/local \
--with-freetype-dir=/usr/local \
--enable-gd-native-ttf \
--with-iconv-dir=/usr/local \
--enable-mbstring \
--enable-calendar \
--with-gettext \
--with-libxml-dir=/usr/local \
--with-zlib \
--with-pdo-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-mysql=mysqlnd \
--enable-dom \
--enable-xml \
--enable-fpm \
--with-libdir=lib64

安装

make 
make test 
make install

复制 php 配置文件

cp php.ini-production /usr/local/php-7.0.11/etc/php.ini

复制 php-fpm 配置文件

cp /usr/local/php-7.0.11/etc/php-fpm.d/www.conf.default /usr/local/php-7.0.11/etc/php-fpm.d/www.conf

加入 service 管理脚本,修改到实际的路径即可

vim /etc/init.d/php7.0.11-fpm
#!/bin/bash
#
# Startup script for the PHP-FPM server.
#
# chkconfig: 345 85 15
# description: PHP is an HTML-embedded scripting language
# processname: php-7.0.11-fpm
# config: /usr/local/php/etc/php.ini
# config: /usr/local/php-7.0.11/etc/php.ini

# Source function library.
. /etc/rc.d/init.d/functions
# php 编译安装的目录
PHP_DIRECTORY=php-7.0.11
PHP_PATH=/usr/local/
DESC="php7.0.11-fpm daemon"
NAME=php-fpm
# php-fpm路径
DAEMON=$PHP_PATH/$PHP_DIRECTORY/sbin/$NAME
# 配置文件路径
CONFIGFILE=$PHP_PATH$PHP_DIRECTORY/etc/php-fpm.d/www.conf
# PID文件路径(在php-fpm.conf设置)
PIDFILE=$PHP_PATH/$PHP_DIRECTORY/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$PHP_DIRECORY

# Gracefully exit if the package has been removed.
test -x $DAEMON || exit 0

rh_start() {
  $DAEMON -y $CONFIGFILE || echo -n " already running"
}
rh_stop() {
  kill -QUIT `cat $PIDFILE` || echo -n " not running"
}

rh_reload() {
  kill -HUP `cat $PIDFILE` || echo -n " can't reload"
}

case "$1" in
  start)
        echo -n "Starting $DESC: $NAME"
        rh_start
        echo "."
        ;;
  stop)
        echo -n "Stopping $DESC: $NAME"
        rh_stop
        echo "."
        ;;
  reload)
        echo -n "Reloading $DESC configuration..."
        rh_reload
        echo "reloaded."
  ;;
  restart)
        echo -n "Restarting $DESC: $NAME"
        rh_stop
        sleep 1
        rh_start
        echo "."
        ;;
  *)
         echo "Usage: $SCRIPTNAME {start|stop|restart|reload}" >&2
         exit 3
        ;;
esac
exit 0

给执行权限
chmod +x php-7.0.11-fpm

设置 php-fpm pid 文件
在 php-fpm 配置文件的 [global] 部分加入 pid 设置项

vim /usr/local/php-7.0.11/etc/php-fpm.d/www.conf
#添加
[global]
pid = run/php-fpm.pid

nginx 配置

server {
        server_name www.jintest1.com;
        root /www/www.jintest1.com;
        index index.html index.php;
        location ~ .*\.(php)?$
        {
                expires -1s;
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                include fastcgi_params;
                fastcgi_param PATH_INFO $fastcgi_path_info;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_pass 127.0.0.1:9000;

        }
}

ok

mysql数据类型学习

整型
整型用来存放整数,根据可以存放的数字大小又分为TINYINT,SMALLINT,MEDIUMINT,INT,BIGINT,每种类型又分为无符号(UNSIGNED)和有符号,
创建一个表,并插入数据,int后面括号里面的数字并不是限制了可存放的数字的大小,而是在查询结果输出的时候会改变输出的格式,方便查看,并且在设置了zerofill的时候才会生效

create table `int_table` (`int1` int,`int2` int(3),`int3` int(3) zerofill)engine=MYISAM;
insert into int_table values (1,1,1),(2,2,2),(8,8,8),(11,11,11);

结果

+------+------+------+
| int1 | int2 | int3 |
+------+------+------+
|    1 |    1 |  001 |
|    2 |    2 |  002 |
|    8 |    8 |  008 |
|   11 |   11 |  011 |
+------+------+------+

浮点型
用来存放小数,根据数据范围的大小可分为FLOAT、DOUBLE 和 DECIMAL,和整型一样 UNSIGNED 和 ZEROFILL 也可以作用于浮点型
FLOAT 数值类型用于表示单精度浮点数值,而 DOUBLE 数值类型用于表示双精度浮点数值。
与整数一样,这些类型也带有附加参数:一个显示宽度指示器和一个小数点指示器。比如语句 FLOAT(7,3) 规定显示的值不会超过 7 位数字,小数点后面带有 3 位数字。
对于小数点后面的位数超过允许范围的值,MySQL 会自动将它四舍五入为最接近它的值,再插入它

create table `float_t` (`f1` float,`f2` float(5,2),`f3` float(7,2) zerofill)engine=MYISAM;
insert into `float_t` values (1.1,1.1,1.1),(2.222,2.222,2.222),(5.555,5.555,5.555),(1.16666,1.16666,1.166666);
select * from float_t;
+---------+------+---------+
| f1      | f2   | f3      |
+---------+------+---------+
|     1.1 | 1.10 | 0001.10 |
|   2.222 | 2.22 | 0002.22 |
|   5.555 | 5.55 | 0005.55 |
| 1.16666 | 1.17 | 0001.17 |
+---------+------+---------+

日期类型
year 类型
插入数据时,注意带上引号的数据有什么不同

create table `year_t` (`y1` year(4),`y2` year(2));
insert into year_t values (16,16),(2016,2016),(0,0),('0','0');
select * from year_t;
+------+------+
| y1   | y2   |
+------+------+
| 2016 | 2016 |
| 2016 | 2016 |
| 0000 | 0000 |
| 2000 | 2000 |
+------+------+

time 类型
空格前的数字代表天数

create table `time_t` (`t1` time);
insert into `time_t` values ('01:01:01'),(020202),('030303'),(0404),('0505'),('6 06:06'),('7 07');
select * from time_t;
+-----------+
| t1        |
+-----------+
| 01:01:01  |
| 02:02:02  |
| 03:03:03  |
| 00:04:04  |
| 00:05:05  |
| 150:06:00 |
| 175:00:00 |
+-----------+

date类型

create table `date_t` (`d1` date);
insert into `date_t` values ('2016-08-08'),('16-09-09');
select * from `date_t`;
+------------+
| d1         |
+------------+
| 2016-08-08 |
| 2016-09-09 |
+------------+

datetime 类型

create table `datatime_t` (`d1` datetime);
insert into `datatime_t` values ('2016-01-01'),('2016-02-02 02:02:02'),('16-03-03 03:03:03'),('16*04*04 04:04:04');
select * from `datatime_t`;
+---------------------+
| d1                  |
+---------------------+
| 2016-01-01 00:00:00 |
| 2016-02-02 02:02:02 |
| 2016-03-03 03:03:03 |
| 2016-04-04 04:04:04 |
+---------------------+

timestamp 类型
会随着系统时区的变化而变化

create table `timestamp_t` (`t1` timestamp);
insert into `timestamp_t` values (now());
show variables like 'time_zone';
+---------------+--------+
| Variable_name | Value  |
+---------------+--------+
| time_zone     | SYSTEM |
+---------------+--------+
select * from `timestamp_t`;
+---------------------+
| t1                  |
+---------------------+
| 2016-09-21 15:21:33 |
+---------------------+
set time_zone='+10:00';
show variables like 'time_zone';
+---------------+--------+
| Variable_name | Value  |
+---------------+--------+
| time_zone     | +10:00 |
+---------------+--------+
select * from `timestamp_t`;
+---------------------+
| t1                  |
+---------------------+
| 2016-09-21 17:21:33 |
+---------------------+

字符串类型
字符串里面类型很多,就记录下其中的几个
char : 固定长度
varchar : 可变长度
从以下的sql看区别

create table `char_t` (`c1` char(6),`c2` varchar(6));
insert into char_t values ('abc   ','abc   ');
select *,concat(c1,'!'),concat(c2,'!'),length(c1),length(c2) from char_t;
+------+--------+----------------+----------------+------------+------------+
| c1   | c2     | concat(c1,'!') | concat(c2,'!') | length(c1) | length(c2) |
+------+--------+----------------+----------------+------------+------------+
| abc  | abc    | abc!           | abc   !        |          3 |          6 |
+------+--------+----------------+----------------+------------+------------+

mysql本机登录不上

报这个错
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

在 my.cnf 最后加上一行,这个是在启动时不启用授权表

skip-grant-tables

重启 mysql ,登录进mysql,再修改相关的权限信息,使之可以登录,在去掉这个参数就可以,重启mysql就行了

mysql架构学习

服务层:为客户端请求做连接处理,授权认证,安全等
完成线程的管理(线程缓冲池)

核心层:查询解析,分析,优化,缓存,所有内建函数,存储过程,触发器,视图等

查询 -> 缓存 -> 有缓存 -> 返回结果
-> 没有缓存 -> 解析查询 -> 优化 -> 执行查询 -> 返回结果

存储引擎层:存储和提取数据以及事务处理