分类 centos6 下的文章

bash shell基本知识

bash shell执行命令前的动作
  1. 分割管道前后的命令
  2. 分割命令中的单词
  3. 替换别名命令
  4. 将括号扩展为指代字符串
  5. "~"符号扩展为实际路径
  6. 替换变量
  7. 替换"$()"和"``"形式的命令
  8. 替换运算符"$(())"为其结果
  9. 替换通配符"*"和"?"为指代本地文件或目录
  10. 根据"函数","内部命令","外部程序"的顺序搜索路径找到命令
  11. 运行替换后的整体程序
bash shell脚本程序变量
  • 参数位置变量
    $0-$9,${10}-${n}
  • 所有参数位置视为一个字符串
    $*
  • 所有参数位置视为一个串行多个字符串
    [email protected]
  • 参数个数
    $#
特殊的内置变量
  • 上一条命令执行结束后的返回值
    $?
  • 目前bash shell的进程号
    $$
  • 上一个后台程序的进程号
    $!
测试变量存在性及空值

${变量名称:-默认值}变量为空时回传默认值

echo ${S1:-is empty!!!}
echo ${S1}
echo 111

输出

[[email protected] test]# bash test.sh 
is empty!!!

111

${变量名称:+默认值}变量为空时设置成默认值

#!/bin/bash
echo ${S1:=is empty!!!}
echo ${S1}
echo 111

输出

[[email protected] test]# bash test.sh 
is empty!!!
is empty!!!
111

${变量名称:?提示信息}变量为空时停止显示程序显示提示信息

echo ${S1:?is empty!!!}
echo ${S1}
echo 111

输出

[[email protected] test]# bash test.sh 
test.sh: line 2: S1: is empty!!!
字符串切片,长度

${变量:位置起点},由指定的位置开始截取字符串到结尾
${变量:位置起点:长度},由指定的位置开始截取指定长度的子字符串
${#变量},传回子变量的长度
例子
逐个输出字符串

#!/bin/bash
STRING="Three words,eight letters."
echo ${STRING:0};
echo ${STRING:0:11};
echo ${#STRING};
# 逐字输出
for ((i=0;i<${#STRING};i++))
do 
        echo ${STRING:${i}:1};
done
对比样式

${变量#样式},从左开始对比变量,删除最短相符字串
${变量##样式},从左开始对比变量,删除最长相符字串
${变量%样式},从右开始对比变量,删除最短相符字串
${变量%%样式},从右开始对比变量,删除最长相符字串
${变量/样式/替换字串},如果变量中右匹配样式(取最长),则使用替代字串替换第一个匹配
${变量//样式/替换字串},如果变量中右匹配样式(取最长),则使用替代字串替换所有匹配
例子

#!/bin/bash
STRING="Three words,eight letters."
echo ${STRING#T*e};
echo ${STRING##T*e};
echo ${STRING%e*.};
echo ${STRING%%e*.};
echo ${STRING/s/ss};
echo ${STRING//s/ss};

输出

[[email protected] test]# bash test.sh 
e words,eight letters.
rs.
Three words,eight lett
Thr
Three wordss,eight letters.
Three wordss,eight letterss.
取变量名列表,数组索引列表

${!开头字串@}或者${!开头字串

},取当前匹配的变量名列表,各变量之前用$IFS第一个分割字符(默认为空格)隔开 ${!数组变量名[@]}或${!数组变量名[

]},取出数组索引列表,各索引之间用$IFS第一个分隔符(默认为空格)隔开

算术运算
  • 算数扩展 $((算数式))
  • 外部命令 expr 算数式
  • 内置命令 declare -i 变量=算数式
  • 内置命令 let 算数式
  • $[]方式 $[算数式]
  • 浮点运算用bc命令,最慢

例子

#!/bin/bash
echo $(( 1 + 1));
expr 1 + 1;
declare -i A=1+1;
echo A;
let B=1+1;
echo B;
echo $[ 1 + 1]
echo "scale=22;2.2+6.6" | bc

输出

[[email protected] test]# bash test.sh 
2
2
A
B
2
8.8
流程控制
 #!/bin/bash
STRING=1;
# if基本用法
if [ ${STRING} = "1" ];
then
        echo "\$STRING='1'"
elif [ ${STRING} -eq 2 ];
then
        echo "\$STRING=1"
fi

# case 基本用法
case $1 in
        1)
                echo "\$1=1";;
        2)
                echo "\$1=2";;
        *)
                echo "\$1i!=1 or 2";;

esac;
# for 基本使用
MAX_NUM=10;
# 使用 {1..n} 的形式时不能使用变量
#for i in {1..10}
# 下面两种方式能使用变量
#for i in `seq 0 ${MAX_NUM}`
for ((i=0;i<=${MAX_NUM};i++))
do
        echo ${i};
done;

# while 基本使用
while [ ${MAX_NUM} -gt 0 ] 
do
        # break使用
        break 1;
        echo ${MAX_NUM};
        MAX_NUM=$[MAX_NUM - 1]
done;
# 逐行读取
while read line
do
        # continue使用
        continue 1;
        sleep 1;
        echo ${line};
done < /etc/passwd;

# until 当条件表达式为假才会运行
until [ $MAX_NUM -gt 10 ]
do
        echo ${MAX_NUM};
        MAX_NUM=$((MAX_NUM+1));
done;
函数的基本使用
 #!/bin/bash
function fun1()
{
        # 使用local关键字避免和函数外的变量冲突
        local STRING=string
        echo "func1 \$1=${1}";
        # 最大返回255
        return 25;
}
fun2()
{
        echo fun2;
}
# 传参
fun1 aaa;
# 上个函数的返回值
echo $?;
# 包含其他文件脚本
#source ./test2.sh
. ./test2.sh
fun3;
文件的重定向
# 获取当前进程pid
[[email protected] test]# echo $$
8897
# 进入proc下对应pid的目录,里面有进程的相关信息
[[email protected] test]# cd /proc/8897/
[[email protected] 8897]# ls
attr       clear_refs       cpuset   fd      loginuid   mounts      numa_maps      pagemap      schedstat  stat     task
autogroup  cmdline          cwd      fdinfo  maps       mountstats  oom_adj        personality  sessionid  statm    wchan
auxv       comm             environ  io      mem        net         oom_score      root         smaps      status
cgroup     coredump_filter  exe      limits  mountinfo  ns          oom_score_adj  sched        stack      syscall
# fd目录下为文件的描述符
[[email protected] 8897]# cd fd
# 文件句柄,为正整数
[[email protected] fd]# ls -l
总用量 0
lrwx------ 1 root root 64 12月  9 15:19 0 -> /dev/pts/2
lrwx------ 1 root root 64 12月  9 15:19 1 -> /dev/pts/2
lrwx------ 1 root root 64 12月  9 15:19 2 -> /dev/pts/2
lrwx------ 1 root root 64 12月  9 15:19 255 -> /dev/pts/2

文件描述符
一个非负整数,用来索引和追踪文件,可以认为是打开文件的编号
在/proc/PID/fd中标识

# 创建文件
[[email protected] fd]# echo aaa > ~/test_file
# 打开文件句柄
[[email protected] fd]# exec 6<>~/test_file 
# 在fd目录下会生成一个文件,操作这个文件和直接操作那个文件一样
[[email protected] fd]# ls
0  1  2  255  6
[[email protected] fd]# cat 6
aaa
[[email protected] fd]# cat ~/test_file 
aaa
[[email protected] fd]# echo bbb > 6
[[email protected] fd]# cat ~/test_file 
bbb
[[email protected] fd]# cat 6
bbb
# 把文件删除,还可以把文件直接copy回去还原
[[email protected] fd]# \rm ~/test_file 
[[email protected] fd]# file ~/test_file
/root/test_file: cannot open `/root/test_file' (No such file or directory)
[[email protected] fd]# ls -l
总用量 0
lrwx------ 1 root root 64 12月  9 15:32 0 -> /dev/pts/2
lrwx------ 1 root root 64 12月  9 15:32 1 -> /dev/pts/2
lrwx------ 1 root root 64 12月  9 15:32 2 -> /dev/pts/2
lrwx------ 1 root root 64 12月  9 15:32 255 -> /dev/pts/2
lrwx------ 1 root root 64 12月  9 15:32 6 -> /root/test_file (deleted)
[[email protected] fd]# cp 6 ~/test_file
# 虽然文件还原了,但是文件其实已经不是之前的文件了
[[email protected] fd]# file ~/test_file
/root/test_file: ASCII text
[[email protected] fd]# echo ccc > 6
[[email protected] fd]# cat 6
ccc
[[email protected] fd]# cat ~/test_file 
bbb
# 关闭
exec 6<&-

打开文件

exec 6<>~/test_file

关闭文件
fd<&-或者fd>&-

exec 6<&-

作用
如下情况,逐行读取会跳过一行

#!/bin/bash
FILE_PATH=/etc/passwd
while read line
do
        sleep 1;
        read ;
        echo ${line};
done < ${FILE_PATH}

如下就可避免这种情况

 #!/bin/bash
FILE_PATH=/etc/passwd
exec 6<>${FILE_PATH}
while read -u 6 line
do
        sleep 1;
        read ;
        echo ${line};
done < ${FILE_PATH}
exec 6<&-;
限制程序指定进程数运行
  • 使用read -u文件描述符方式读取字符串内容
  • 设置文件描述符中的回车符号个数为预设的进程数
  • 通过循环完成进程的创建

例子

 #!/bin/bash
sub()
{
        echo "------${$}------begin------";
        SLEEP_TIME=$[ 1 % 7 ];
        sleep ${SLEEP_TIME};
        echo "------${$}------end------";
}
# 总执行次数
TOTAL_NUMBER=88;
# 同时最多执行次数
MAX_THREAD=8;
# 临时文件地址
TEMP_FILE="/tmp/.fifo-${$}";
# 创建管道文件,管道文件在内存中,更快
mkfifo ${TEMP_FILE};
# 打开文件句柄
exec 6<>${TEMP_FILE}
# 删除临时文件
rm -f ${TEMP_FILE};
# 填充回车到文件句柄使程序能执行
for((i=0;i<${MAX_THREAD};i++))
do
        echo
done >&6;

# 循环把程序执行完
for((i=0;i<${TOTAL_NUMBER};i++))
do
        # 从文件句柄中读取,遇到回车才会执行下一步
        read -u 6
        # 定义程序块,在程序块中执行要执行的操作,把程序块放到后台执行
        {   
                sub ${i};
                echo >&6;
        }&
        # 查看后台运行的程序
        jobs -r;
        jobs -r | wc -l;
done;


# 等到所有程序执行完
wait;
# 关闭文件句柄
exec 6<&-;
exit 0;

CentOS release 6.6 lvm扩容

服务器分区容量不够,需要扩容,公司使用的是虚拟机,直接调整了虚拟机的磁盘容量,然后嗨需要做如下的处理才能达到效果,记录下
参考网址 : http://blog.sina.com.cn/s/blog_967817f20102v2a6.html http://blog.sina.com.cn/s/blog_ac85574f01014wwc.html
分区

fdisk /dev/sda

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): n #新建分区
# 选择分区类型
Command action
   e   extended
   p   primary partition (1-4)
p # 主分区类型
#分区号,根据具体情况设置即可
Partition number (1-4): 3
# 开始扇区,默认即可
First cylinder (16579-39162, default 16579): 
# 结束扇区,默认的话就是使用全部
Last cylinder, +cylinders or +size{K,M,G} (16579-39162, default 39162): 
# 设置创建的分区的类型为 lvm
t
# 选择分区,就选择上面的分区
Partition number (1-4): 3
# 输入lvm的dai m8e, 
Hex code (type L to list codes): 8e
#保存退出
w

重启服务器
lvm 操作

# 查看 pv 
pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               VolGroup
  PV Size               126.51 GiB / not usable 2.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              32386
  Free PE               0
  Allocated PE          32386
  PV UUID               eFSO1Y-tyey-pa0K-0vea-uW7p-FmTh-TO0gEG
# 创建刚刚创建的分区为 pv 
pvcreate /dev/sda3
# 查看 pv 
pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               VolGroup
  PV Size               126.51 GiB / not usable 2.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              32386
  Free PE               0
  Allocated PE          32386
  PV UUID               eFSO1Y-tyey-pa0K-0vea-uW7p-FmTh-TO0gEG

  --- Physical volume ---
  PV Name               /dev/sda3
  VG Name               
  PV Size               173.00 GiB / not usable 4.06 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              44286
  Free PE               254
  Allocated PE          44032
  PV UUID               gSth8U-LXJl-3FA1-YGju-6J02-e4v0-ek8j2Y
# 把 pv 加入原有的 vg
vgextend VolGroup /dev/sda3
#查看 lv 
lvdisplay 
  --- Logical volume ---
  LV Path                /dev/VolGroup/lv_root
  LV Name                lv_root
  VG Name                VolGroup
  LV UUID                eefIxD-Sps3-44v1-3DRB-ODSA-byuq-6QHZe6
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2015-01-26 15:11:02 +0800
  LV Status              available
  # open                 1
  LV Size                50.00 GiB
  Current LE             56832
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0
# 添加容量 
lvresize -L +172GB /dev/mapper/VolGroup-lv_root
#查看 lv 
lvdisplay 
  --- Logical volume ---
  LV Path                /dev/VolGroup/lv_root
  LV Name                lv_root
  VG Name                VolGroup
  LV UUID                eefIxD-Sps3-44v1-3DRB-ODSA-byuq-6QHZe6
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2015-01-26 15:11:02 +0800
  LV Status              available
  # open                 1
  LV Size                222.00 GiB
  Current LE             56832
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

修改文件系统的容量 ,注意这里的device是虚拟设备lv的名称,并不是sdax这样的物理设备了,我的环境下药修改的是分目录的容量,对应的是/dev/VolGroup/lv_root,在 /etc/fstab 文件里面可以查看 或者用 lvdisplay查看

vim /etc/fstab
/dev/mapper/VolGroup-lv_root /                       ext4    defaults        1 1
UUID=c1da8f6f-e67f-4f0f-90de-2883035385d7 /boot                   ext4    defaults        1 2
/dev/mapper/VolGroup-lv_home    /home   ext4    grpquota,usrquota,rw    0       2
/dev/mapper/VolGroup-lv_swap swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
#修改
fsadm resize /dev/VolGroup/lv_root 222G

设置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=&lt;path&gt; 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}" &gt;&amp;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的rpm 包

mysql 5.6
centos 6.8 minimal

下载 src.rpm 后缀的包
安装所需软件

yum install rpm-build

切换到普通用户

su - jin

创建rmpbuild目录

mkdir rembuild

解压rpm包文件

rpm -ivh MySQL-5.6.33-1.el6.src.rpm 

进入 rpmbuild 目录

vim SPECS/mysql.spec 

找到 Build full release ,在修改自定义的编译参数,这里以修改数据库支持的字符集为例,如下

${CMAKE} ../%{src_dir} -DBUILD_CONFIG=mysql_release -DINSTALL_LAYOUT=RPM \
           -DWITH_INNODB_MEMCACHED=1 -DCMAKE_BUILD_TYPE=RelWithDebInfo \
           -DMYSQL_UNIX_ADDR="%{mysqldatadir}/mysql.sock" \
           -DFEATURE_SET="%{feature_set}" \
           %{ssl_option} \
           -DCOMPILATION_COMMENT="%{compilation_comment_release}" \
           -DMYSQL_SERVER_SUFFIX="%{server_suffix}"

改成下面的

${CMAKE} ../%{src_dir} -DBUILD_CONFIG=mysql_release -DINSTALL_LAYOUT=RPM \
           -DWITH_INNODB_MEMCACHED=1 -DCMAKE_BUILD_TYPE=RelWithDebInfo \
           -DMYSQL_UNIX_ADDR="%{mysqldatadir}/mysql.sock" \
           -DFEATURE_SET="%{feature_set}" \
           %{ssl_option} \
           -DCOMPILATION_COMMENT="%{compilation_comment_release}" \
           -DMYSQL_SERVER_SUFFIX="%{server_suffix}" \
           -DWITH_EXTRA_CHARSETS=all

或者直接修改最上面变量声明的位置,这里以修改运行用户为例,

%global mysqld_user     mysql

修改为

%global mysqld_user     mysql1

构建 rpm 包

cd SPECS/
rpmbuild -bb mysql.spec

报错了

gperf is needed by MySQL-5.6.33-1.linux_glibc.x86_64
    time is needed by MySQL-5.6.33-1.linux_glibc.x86_64
    zlib-devel is needed by MySQL-5.6.33-1.linux_glibc.x86_64

缺什么安装即可

sudo yum install -y gperf time zlib-devel

重新构建

rpmbuild -bb mysql.spec

报错

CMake Error at cmake/build_configurations/mysql_release.cmake:46 (MESSAGE):


          aio is required on Linux, you need to install the required library:



            Debian/Ubuntu:              apt-get install libaio-dev
            RedHat/Fedora/Oracle Linux: yum install libaio-devel
            SuSE:                       zypper install libaio-devel



          If you really do not want it, pass -DIGNORE_AIO_CHECK to cmake.

Call Stack (most recent call first):
  CMakeLists.txt:134 (INCLUDE)

按照提示执行即可

sudo yum install libaio-devel

重新构建

rpmbuild -bb mysql.spec

报了个这个错

-DWITH_EXTRA_CHARSETS=all: command not found

犯二了,加的选项没加 \

重新构建

rpmbuild -bb mysql.spec

出现以下的,就ok了

Wrote: /home/jin/rpmbuild/RPMS/x86_64/MySQL-server-5.6.33-1.linux_glibc.x86_64.rpm
Wrote: /home/jin/rpmbuild/RPMS/x86_64/MySQL-client-5.6.33-1.linux_glibc.x86_64.rpm
Wrote: /home/jin/rpmbuild/RPMS/x86_64/MySQL-test-5.6.33-1.linux_glibc.x86_64.rpm
Wrote: /home/jin/rpmbuild/RPMS/x86_64/MySQL-devel-5.6.33-1.linux_glibc.x86_64.rpm
Wrote: /home/jin/rpmbuild/RPMS/x86_64/MySQL-shared-5.6.33-1.linux_glibc.x86_64.rpm
Wrote: /home/jin/rpmbuild/RPMS/x86_64/MySQL-embedded-5.6.33-1.linux_glibc.x86_64.rpm
Executing(%clean): /bin/sh -e /var/tmp/rpm-tmp.0k7Cbk
+ umask 022
+ cd /home/jin/rpmbuild/BUILD
+ cd mysql-5.6.33
+ '[' /home/jin/rpmbuild/BUILDROOT/MySQL-5.6.33-1.linux_glibc.x86_64 '!=' / ']'
+ '[' -d /home/jin/rpmbuild/BUILDROOT/MySQL-5.6.33-1.linux_glibc.x86_64 ']'
+ rm -rf /home/jin/rpmbuild/BUILDROOT/MySQL-5.6.33-1.linux_glibc.x86_64
+ exit 0

测试能否安装

yum install MySQL-server-5.6.33-1.linux_glibc.x86_64.rpm

报错

file /usr/share/mysql/czech/errmsg.sys from install of MySQL-server-5.6.33-1.linux_glibc.x86_64 conflicts with file from package mysql-libs-5.1.73-7.el6.x86_64

是已经安装的 mysql-libs 的版本太低,先删除,在安装

yum remove mysql-libs
yum install MySQL-server-5.6.33-1.linux_glibc.x86_64.rpm

复制配置文件

scp /usr/share/mysql/my-default.cnf /etc/my.cnf

因为在生成 rpm 包的时候,把默认的用户和用户组改成了 mysql1 , 所以在 yum 安装的时候创建了 mysql 的用户,直接启动的时候还是会以 mysql 的身份启动,所以在 my.cnf 中手动指定下 用户

[mysqld]

下加一行

user=mysql1

启动

service mysql start

看到下面的就 ok 了

Starting MySQL.. SUCCESS!

安装客户端

yum install MySQL-client-5.6.33-1.linux_glibc.x86_64.rpm

在这里查看默认密码

cat /root/.mysql_secret

登录,登录之后需要修改密码才能做其他操作

mysql -u root -p 

设置密码

SET PASSWORD = PASSWORD('123456'); 

允许远程登录

update user set password=password('123456') where user='root';
update user set host='%' where user='root' and host='localhost';
flush privileges;