一键搭建Sersync+Rsync相互实时同步(双机热备)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/bin/bash

rsname=Rsync+Sersync
rsver=1.0
sip=172.16.8.23
dip=172.16.8.24
run_user=rsync
base_dir=/usr/local
rsync_dir=/home/wwwroot/test22
port=873
auth_users=ropon
passwd=test123
sersync_version=2.5.4
mirrorLink=http://panel.ropon.top/rsync/
sersync_install_dir=/usr/local/sersync
confpath=$sersync_install_dir/confxml.xml
rsync_cmd="rsync -artuz -R --delete $auth_users@$dip::rsync $rsync_dir --password-file=/etc/rsyncd.pass"
sersync_cmd="/usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/confxml.xml"
flag=$1

Echo()
{
case $1 in
success) flag="\033[1;32m"
;;
failure) flag="\033[1;31m"
;;
warning) flag="\033[1;33m"
;;
msg) flag="\033[1;34m"
;;
*) flag="\033[1;34m"
;;
esac
if [[ $LANG =~ [Uu][Tt][Ff] ]]
then
echo -e "${flag}${2}\033[0m"
else
echo -e "${flag}${2}\033[0m" iconv -f utf-8 -t gbk
fi
}

header()
{
printf "
###################################################################
# $rsname version $rsver Author: Ropon <idiyrom.com> #
# For more information please visit https://idiyrom.com/83.html #
#-----------------------------------------------------------------#
# Copyright @2017-2018 idiyrom.com. All rights reserved. #
###################################################################

"
}

showhelp()
{
header
Echo "msg" "Usage: $rsname [OPTIONS]"
echo
Echo "msg" "OPTIONS:"
Echo "msg" "-h --help : Show help of $rsname"
Echo "msg" "-u --update : update Check for $rsname"
Echo "msg" "-i --install : install $rsname version $rsver to This System"
Echo "msg" "-U --uninstall : Uninstall $rsname from This System"
echo
}

Download_src() {
[ -s "${src_url##*/}" ] && Echo "msg" "[${src_url##*/}] found" { wget --tries=6 -c --no-check-certificate $src_url; sleep 1; } #判断下载是否已存在
if [ ! -e "${src_url##*/}" ]; then
Echo "msg" "${src_url##*/} download failed, Please contact the author!"
kill -9 $$
fi
}

Install()
{
pushd ${base_dir}/src
id -u $run_user >/dev/null 2>&1
[ $? -ne 0 ] && useradd -M -s /sbin/nologin $run_user
yum install rsync -y
cat > /etc/rsyncd.conf <<EOF
uid=$run_user
gid=$run_user
use chroot = no
port=$port
#address=$sip
max connections=2000
timeout=200
log file=/var/log/rsyncd.log
pid file=/var/run/rsyncd.pid
lock file=/var/run/rsyncd.lock
log format=%t %a %m %f %b

[rsync]
path=$rsync_dir
commemt=rsync
list=yes
read only=no
#write only=no
auth users=$auth_users
secrets file=/etc/rsyncd.secret
ignore errors=yes
hosts allow=$dip
hosts deny=
EOF

cat > /etc/rsyncd.secret <<EOF
$auth_users:$passwd
EOF

cat > /etc/rsyncd.pass <<EOF
$passwd
EOF

chmod 600 /etc/rsyncd.secret /etc/rsyncd.pass
[ ! -d "$rsync_dir" ] && mkdir -p $rsync_dir
chown $run_user.$run_user -R $rsync_dir
systemctl enable rsyncd
systemctl start rsyncd
[ $? -eq 0 ] && Echo "success" "rsyncd服务安装成功,且已正常启动。"

[ ! -f sersync-${sersync_version}.tar.gz ] && src_url=${mirrorLink}/sersync-${sersync_version}.tar.gz && Download_src
[ ! -d "$sersync_install_dir" ] && mkdir -p $sersync_install_dir
tar -xzf sersync-${sersync_version}.tar.gz -C $sersync_install_dir
sed -i "s@/ropon@$rsync_dir@g" $confpath
sed -i "s@pengge@$auth_users@g" $confpath
sed -i "s@192.168.19.19@$dip@g" $confpath
/usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/confxml.xml

cat >> /etc/rc.d/rc.local <<EOF
$rsync_cmd
EOF

cat >> /etc/rc.d/rc.local <<EOF
$sersync_cmd
EOF

[ $? -eq 0 ] && Echo "success" "sersync服务安装成功,且已正常启动。"
rm -rf sersync-${sersync_version}.tar.gz
popd
}

Uninstall()
{
userdel $run_user
systemctl disable rsyncd
yum remove -y rsync
rm -rf /etc/rsyncd.conf*
rm -rf /etc/rsyncd.secret /etc/rsyncd.pass /etc/rsync_exclude.lst
pkill sersync2
rm -rf $sersync_install_dir
sed -i '/^\/usr\/local\/sersync.*/d' /etc/rc.d/rc.local
sed -i '/^rsync.*/d' /etc/rc.d/rc.local
Echo "success" "卸载完成"
}

Update()
{
Echo "msg" "暂未开通"
}

Main()
{
case $flag in
'-h' '--help' '?' )
showhelp
exit
;;
'--install' '-i' )
Install
exit
;;
'--uninstall' '-U' )
Uninstall
exit
;;
'--update' '-u' )
Update
exit
;;
* )
showhelp
exit
;;
esac
}
Main

nfs高可用

1
2
3
4
cat /etc/hosts
172.16.8.20 fs1
172.16.8.21 fs2
172.16.8.22 fs

1、安装服务

1
2
3
4
5
6
yum -y install keepalived
yum install nfs-utils rpcbind -y

net.ipv4.ip_forward=1
net.ipv4.conf.all.rp_filter=0
net.ipv4.conf.default.rp_filter=0

2、配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
! Configuration File for keepalived

global_defs {
router_id RSYNC_MASTER
}

vrrp_instance VI_1 {
state MASTER
interface eth1
virtual_router_id 60
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.16.8.120
}
}

virtual_server 192.16.8.120 873 {
delay_loop 6
lb_algo rr
lb_kind NAT
nat_mask 255.255.255.0
persistence_timeout 5
protocol TCP

real_server 172.16.8.20 873 {
weight 3
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 873
}
}
real_server 172.16.9.21 873 {
weight 3
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 873
}
}
}

Sersync+Rsync互相实时同步

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
1、关闭selinux
2、防火墙(iptables、Firewalld)放行873端口(默认)
3、安装Rsync服务
yum install rsync -y
useradd -r -s /sbin/nologin rsync -M
cat > /etc/rsyncd.conf <<EOF
uid = rsync
gid = rsync
use chroot = no
port = 873
address = 192.168.230.10
max connections = 2000
timeout = 200
log file = /var/run/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log format = %t %a %m %f %b

[rsync]
path = /home/wwwroot/test/
commemt = rsync
list = yes
read only = no
#write only = no
auth users = homeuser
secrets file = /etc/rsyncd.secret
ignore errors = yes
hosts allow = 192.168.250.101
hosts deny =
EOF
read only 指定是否允许客户上传文件。若为 true 则不允许上传;若为 false 并且服务器目录也具有读写权限则允许上传。 true
write only 指定是否允许客户下载文件。若为 true 则不允许下载;若为 false 并且服务器目录也具有读权限则允许下载。 false

read only 默认值true 不允许上传,只能读;false 可写(可上传),是否可读取决于write only
write only 默认值false 可读而且可下载;true 不允许下载,是否可写取决于read only

cat > /etc/rsyncd.secret <<EOF
homeuser:test123
EOF
chmod 600 /etc/rsyncd.secret

cat > /etc/rsyncd.pass <<EOF
test123
EOF
chmod 600 /etc/rsyncd.pass

mkdir -p /home/wwwroot/test
chown rsync.rsync -R /home/wwwroot/test
systemctl enable rsyncd
systemctl start rsyncd

4、安装Sersync服务
wget http://panel.ropon.top/rsync/sersync.zip
unzip sersync.zip -C /usr/local/
tar xf sersync.zip -C /usr/local/
cat //usr/local/sersync/config.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
<host hostip="localhost" port="8008"></host>
<debug start="false"/>
<fileSystem xfs="false"/>
<filter start="false">
<exclude expression="(.*)\.svn"></exclude>
<exclude expression="(.*)\.gz"></exclude>
<exclude expression="^info/*"></exclude>
<exclude expression="^static/*"></exclude>
</filter>
<inotify>
<delete start="true"/>
<createFolder start="true"/>
<createFile start="false"/>
<closeWrite start="true"/>
<moveFrom start="true"/>
<moveTo start="true"/>
<attrib start="false"/>
<modify start="false"/>
</inotify>

<sersync>
<localpath watch="/home/wwwroot/test">
<remote ip="192.168.250.101" name="rsync"/>
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
<commonParams params="-artuz"/>
<auth start="true" users="homeuser" passwordfile="/etc/rsyncd.pass"/>
<userDefinedPort start="false" port="873"/><!-- port=874 -->
<timeout start="false" time="100"/><!-- timeout=100 -->
<ssh start="false"/>
</rsync>
<failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
<crontab start="false" schedule="600"><!--600mins-->
<crontabfilter start="false">
<exclude expression="*.php"></exclude>
<exclude expression="info/*"></exclude>
</crontabfilter>
</crontab>
<plugin start="false" name="command"/>
</sersync>

<plugin name="command">
<param prefix="/bin/sh" suffix="" ignoreError="true"/> <!--prefix /opt/tongbu/mmm.sh suffix-->
<filter start="false">
<include expression="(.*)\.php"/>
<include expression="(.*)\.sh"/>
</filter>
</plugin>

<plugin name="socket">
<localpath watch="/home/wwwroot/test/">
<deshost ip="192.168.250.101" port="8009"/>
</localpath>
</plugin>
<plugin name="refreshCDN">
<localpath watch="/data0/htdocs/cms.xoyo.com/site/">
<cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
<sendurl base="http://pic.xoyo.com/cms"/>
<regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>
</localpath>
</plugin>
</head>

cat /etc/rc.local
/usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/confxml.xml

B服务器同理
注意开机启动项
cd /home/wwwroot/test && rsync -artuz -R --delete homeuser@192.168.230.10::rsync ./ --password-file=/etc/rsyncd.pass
/usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/confxml.xml

Mysql bin-log日志设置自动清理及如何手工清理

bin-log日志作用 1、数据恢复:数据库出现故障时,从二进制日志找出那个那个语句引起,从而会写数据。 2、主从同步:主服务增删改查操作,从服务器通过二进制重写报错数据同步。 查看bin-log日志 show binlog events; show binlog events in ‘mysql-bin.000002’; 清理bin-log日志 1、自动清理: cat /etc/my.cnf expire_logs_days = 5 //保留最近5天二进制日志 2、手工清理: 没有设置主从复制,可使用reset master;命令清空二进制日志 存在主从复制,使用以下命令清理二进制日志 purge master logs to ‘mysql-bin.000002’; //清理mysql-bin.000002日志 purge master logs to ‘2018-03-24 14:30:00’; //清理2018-03-24 14:30:00前的日志 purge master logs to before date_sub(now(),interval 2 day); //清理2天前的日志

Shell中的${}、##和%%使用范例

今天看一个脚本文件的时候有一些地方不太懂,找了一篇文章看了一些,觉得不错,保留下来。 假设我们定义了一个变量为: file=/dir1/dir2/dir3/my.file.txt 可以用${ }分别替换得到不同的值: ${file#*/}:删掉第一个 / 及其左边的字符串:dir1/dir2/dir3/my.file.txt ${file##*/}:删掉最后一个 / 及其左边的字符串:my.file.txt ${file#*.}:删掉第一个 . 及其左边的字符串:file.txt ${file##*.}:删掉最后一个 . 及其左边的字符串:txt ${file%/*}:删掉最后一个 / 及其右边的字符串:/dir1/dir2/dir3 ${file%%/*}:删掉第一个 / 及其右边的字符串:(空值) ${file%.*}:删掉最后一个 . 及其右边的字符串:/dir1/dir2/dir3/my.file ${file%%.*}:删掉第一个 . 及其右边的字符串:/dir1/dir2/dir3/my 记忆的方法为: # 是 去掉左边(键盘上#在 $ 的左边) %是去掉右边(键盘上% 在$ 的右边) 单一符号是最小匹配;两个符号是最大匹配 ${file:0:5}:提取最左边的 5 个字节:/dir1 ${file:5:5}:提取第 5 个字节右边的连续5个字节:/dir2 也可以对变量值里的字符串作替换: ${file/dir/path}:将第一个dir 替换为path:/path1/dir2/dir3/my.file.txt ${file//dir/path}:将全部dir 替换为 path:/path1/path2/path3/my.file.txt 利用 ${ } 还可针对不同的变数状态赋值(沒设定、空值、非空值): ${file-my.file.txt} :假如 $file 沒有设定,則使用 my.file.txt 作传回值。(空值及非空值時不作处理) ${file:-my.file.txt} :假如 $file 沒有設定或為空值,則使用 my.file.txt 作傳回值。 (非空值時不作处理) ${file+my.file.txt} :假如 $file 設為空值或非空值,均使用 my.file.txt 作傳回值。(沒設定時不作处理) ${file:+my.file.txt} :若 $file 為非空值,則使用 my.file.txt 作傳回值。 (沒設定及空值時不作处理) ${file=my.file.txt} :若 $file 沒設定,則使用 my.file.txt 作傳回值,同時將 $file 賦值為 my.file.txt 。 (空值及非空值時不作处理) ${file:=my.file.txt} :若 $file 沒設定或為空值,則使用 my.file.txt 作傳回值,同時將 $file 賦值為my.file.txt 。 (非空值時不作处理) ${file?my.file.txt} :若 $file 沒設定,則將 my.file.txt 輸出至 STDERR。 (空值及非空值時不作处理) ${file:?my.file.txt} :若 $file 没设定或为空值,则将 my.file.txt 输出至 STDERR。 (非空值時不作处理) $

手工安装redis、ioncube、memcached常用组件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
1、安装redis服务
yum -y install redis
2、安装php组件
wget http://pecl.php.net/get/redis-3.1.0.tgz
tar xzf redis-3.1.0.tgz
cd redis-3.1.0
/usr/local/php56/bin/phpize
./configure --with-php-config=/usr/local/php56/bin/php-config
make && make install
cat /usr/local/php56/etc/php.d/ext-redis.ini << EOF
extension=redis.so
EOF

1、安装ioncube组件
cd `/usr/local/php56/bin/php-config --extension-dir`
wget http://download.myhostadmin.net/php/ioncube/ioncube_loader_lin_5.6.so
cat /usr/local/php56/etc/php.d/ext-ioncube.ini << EOF
[ioncube]
zend_extension=ioncube_loader_lin_5.6.so
EOF

1、安装memcached服务
yum -y install memcached
修改为127.0.0.1运行
cat /etc/sysconfig/memcached << EOF
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="256"
OPTIONS="-l 127.0.0.1"
EOF
systemctl restart memcached
2、安装memcached组件
wget http://pecl.php.net/get/memcache-2.2.5.tgz
tar xzf memcache-2.2.5.tgz
cd memcache-2.2.5
/usr/local/php56/bin/phpize
./configure --enable-memcache --with-php-config=/usr/local/php56/bin/php-config --with-zlib-dir
make && make install
cat /usr/local/php56/etc/php.d/ext-memcache.ini << EOF
extension=memcache.so
EOF

Linux shell `` '' \"\" 字符含义

‘’ “” 主要解决变量中间有空格 比如: str = This is String 这样写会报错,正确书写:str = “This is String” 单引号’’,双引号””的区别是单引号’’剥夺了所有字符的特殊含义,单引号’’内就变成了单纯的字符。双引号””则对于双引号””内的参数替换($)和命令替换(``)是个例外。 比如说   n=3 echo ‘$n’ 结果就是$n 改成双引号  echo “$n” 结果就是3 `` 是命令替换,命令替换是指Shell可以先执行``中的命令,将输出结果暂时保存,在适当的地方输出。语法:`command` #!/bin/bash DATE=`date` echo “Date is $DATE” USERS=`who wc -l` echo “Logged in user are $USERS” UP=`date ; uptime` echo “Uptime is $UP” 运行结果: Date is Thu Jul 2 03:59:57 MST 2009 Logged in user are 1 Uptime is Thu Jul 2 03:59:57 MST 2009 03:59:57 up 20 days, 14:03, 1 user, load avg: 0.13, 0.07, 0.15

详解FTP

1.下载安装包

1
wget http://192.168.6.188/down/ftp/pure-ftpd-1.0.47.tar.gz

2.解压并进入解压后的目录

1
2
tar -zxvf pure-ftpd-1.0.47.tar.gz
cd pure-ftpd-1.0.47

3.安装编译需要的gcc组件,已安装的跳过

1
yum -y install gcc

4.编辑编译配置

1
./configure --prefix=/usr/local/pure-ftpd --with-language=simplified-chinese --with-everything

5.编译及编译安装

1
2
make
make install

6.如果想要和yum一样使用快捷命令,设置环境变量或增加软链接

1
ln -s /usr/local/pure-ftpd/bin/* /usr/bin/

编辑配置文件和创建ftp站点,同方式一第3步及之后步骤

7.配置文件路径/usr/local/pure-ftpd/etc/pure-ftpd.conf,我们一般需要做几个修改:

a.设置数据库
1
2
#去掉前面的#注释符号
#PureDB /usr/local/pure-ftpd/etc/pure-ftpd/pureftpd.pdb
b.开启日志
1
VerboseLog yes
c.关闭匿名登录
1
NoAnonymous yes
d.使用被动模式,设置被动端口范围
1
2
#去掉前面的#注释符号,及修改端口范围
#PassivePortRange 48000 50000

8.给ftp创建虚拟用户,密码及指定目录

1
2
3
4
5
mkdir -p /home/wwwroot/ropon/
useradd www -s /sbin/nologin -M
pure-pw useradd ropon -d /home/wwwroot/ropon/ -u www -m
#回车执行后会提示输入密码
#成功执行后,会生成两个文件pureftpd.passwd和pureftpd.pdb

9.最后配置好防火墙策略即可

1
2
iptables -A INPUT -p tcp --dport 21 -j ACCEPT
iptables -A INPUT -p tcp --dport 48000:50000 -j ACCEPT

10.启动服务

1
2
#开机自启动,添加到/etc/rc.local中
/usr/local/pure-ftpd/sbin/pure-ftpd /usr/local/pure-ftpd/etc/pure-ftpd.conf

11.如何修改已经创建的ftp虚拟用户密码呢?

1
pure-pw passwd ropon -m

12.删除ftp的虚拟用户

1
pure-pw userdel ropon

13.查看ftp虚拟用户列表

1
pure-pw list

详解PHP

检查安装基本组件库

yum install -y zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel

yum install -y freetype-devel libpng-devel gd-devel libcurl-devel libxslt-devel icu libicu libicu-devel

cd /usr/local/src

wget http://luopengtest.gotoip2.com/shell/lnmp/libiconv-1.15.tar.gz

tar xzf libiconv-1.15.tar.gz

cd libiconv-1.15

mkdir -p /usr/local/libiconv

./configure –prefix=/usr/local/libiconv/

make

make install

配置epel源

mv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.backup

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

yum install -y libmcrypt-devel mhash mcrypt

wget http://luopengtest.gotoip2.com/shell/lnmp/php-5.6.32.tar.gz

tar xzf php-5.6.32.tar.gz

cd php-5.6.32

mkdir -p /usr/local/php

./configure –prefix=/usr/local/php –with-config-file-path=/usr/local/php/etc \

--with-config-file-scan-dir=/usr/local/php/etc/php.d \

--with-fpm-user=www –with-fpm-group=www –enable-fpm –disable-opcache –disable-fileinfo \

--with-mysql=mysqlnd –with-mysqli=mysqlnd –with-pdo-mysql=mysqlnd \

--with-iconv-dir=/usr/local/libiconv –with-freetype-dir –with-jpeg-dir –with-png-dir –with-zlib \

--with-libxml-dir –enable-xml –disable-rpath –enable-bcmath –enable-shmop –enable-exif \

--enable-sysvsem –enable-inline-optimization –with-curl=/usr/local –enable-mbregex \

--enable-mbstring –with-mcrypt –with-gd –enable-gd-native-ttf –with-openssl \

--with-mhash –enable-pcntl –enable-sockets –with-xmlrpc –enable-ftp –enable-intl –with-xsl \

--with-gettext –enable-zip –enable-soap –disable-debug $php_modules_options

make

make install

[ -z “`grep ^’export PATH=’ /etc/profile`“ ] && echo “export PATH=/usr/local/php/bin:\$PATH” >> /etc/profile

[ -n “`grep ^’export PATH=’ /etc/profile`“ -a -z “`grep $php_install_dir /etc/profile`“ ] && sed -i “s@^export PATH=\(.*\)@export PATH=/usr/local/php/bin:\1@” /etc/profile

. /etc/profile

/bin/cp php.ini-production $php_install_dir/etc/php.ini

/bin/cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

chmod +x /etc/init.d/php-fpm

chkconfig –add php-fpm && chkconfig php-fpm on

http://luopengtest.gotoip2.com/shell/lnmp/php.sh php自动安装shell脚本

详解MYSQL

安装基本组件库

yum -y install gcc-c++ perl ncurses ncurses-devel libaio numactl numactl-libs perl-Module-Install.noarch net-tools vim wget

mysql

1、创建mysql普通用户

id -u mysql >/dev/null 2>&1

[ $? -ne 0 ] && useradd mysql -s /sbin/nologin -M -g mysql

[ ! -d /home/ropon/tools ] && mkdir -p /home/ropon/tools

cd /home/ropon/tools

wget -c https://cmake.org/files/v3.6/cmake-3.6.2.tar.gz (mysql编译工具)

tar zxvf cmake-3.6.2.tar.gz

cd cmake-3.6.2

./configure

make && make install

wget -c http://cdn.mysql.com/Downloads/MySQL-5.6/mysql-5.6.38.tar.gz

[ ! -d /usr/local/mysql-5.6.38 ] && mkdir -p /usr/local/mysql-5.6.38 #创建安装目录

[ ! -d /home/mysql ] && mkdir -p /home/mysql #创建数据库存放目录

chown mysql.mysql -R /home/mysql

ln -s /usr/local/mysql-5.6.38 /usr/local/mysql

cd /home/ropon/tools

tar zxvf mysql-5.6.38.tar.gz

cd mysql-5.6.38

cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ #安装目录

-DMYSQL_DATADIR=/home/mysql \ #数据库存放目录

-DSYSCONFDIR=/etc #配置文件目录

-DWITH_INNOBASE_STORAGE_ENGINE=1 \ #安装INNOBASE存储引擎

-DWITH_PARTITION_STORAGE_ENGINE=1 \ #安装数据库分区

-DWITH_FEDERATED_STORAGE_ENGINE=1 \

-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \ #安装BLACKHOLE存储引擎

-DWITH_MYISAM_STORAGE_ENGINE=1 \ #安装MYISAM存储引擎

-DWITH_EMBEDDED_SERVER=1 \

-DENABLE_DTRACE=0 \

-DENABLED_LOCAL_INFILE=1 \

-DDEFAULT_CHARSET=utf8mb4 \

-DDEFAULT_COLLATION=utf8mb4_general_ci \

-DEXTRA_CHARSETS=all \

-DCMAKE_EXE_LINKER_FLAGS=’-ljemalloc’

以上可选

make -j 4 #多进程编译,4这个数值请修改cpu核心数

make install

/bin/cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld #拷贝启动项

sed -i “s@^basedir=.*@basedir=/usr/local/mysql@” /etc/init.d/mysqld #修改启动项mysql安装目录

sed -i “s@^datadir=.*@datadir=/home/mysql@” /etc/init.d/mysqld #修改启动项mysql存放目录

chmod +x /etc/init.d/mysqld

chkconfig –add mysqld && chkconfig mysqld on #给执行权限添加开机自启

优化my.cnf

[client]

port = 3306

socket = /tmp/mysql.sock

default-character-set = utf8mb4

[mysql]

prompt=”MySQL [\\d]> “ #prompt命令可以在mysql提示符中显示当前用户、数据库、时间等信息

#prompt=”MySQL [\\u@\\h:\\d]> “

#\\u 当前用户 \\h 当前链接地址 \\d 当前数据库

no-auto-rehash #命令自动补全的意思

[mysqld]

port = 3306

socket = /tmp/mysql.sock

basedir = /usr/local/mysql

datadir = /home/mysql

pid-file = /home/mysq/mysql.pid

user = mysql

bind-address = 0.0.0.0

server-id = 1

init-connect = ‘SET NAMES utf8mb4’

character-set-server = utf8mb4

skip-name-resolve #禁止掉DNS的查询

#mysql会在用户登录过程中对客户端IP进行DNS反查,不管你是使用IP登录还是域名登录,这个反查#的过程都是在的。所以如果你的mysql所在的服务器的DNS有问题或者质量不好,那么就有可能造成#我遇到的这个问题,DNS解析出现问题。

#skip-networking #开启该选项后就不能远程访问MySQL

back_log = 300

#指出在MySQL暂时停止回答新请求之前的短时间内多少个请求可以被存在堆栈中。也就#是说,如果MySql的连接数达到max_connections时,新来的请求将会被存

#在堆栈中,以等待某一连接释放资源,该堆栈的数量即back_log,如果等待连接的数量#超过back_log,将不被授予连接资源。

max_connections = 1000

max_connect_errors = 6000

#当此值设置为10时,意味着如果某一客户端尝试连接此MySQL服务器,但是失败(如密码错误等 #)10次,则MySQL会无条件强制阻止此客户端连接。如果希望重置此计数器的值,则必须重启#MySQL服务器或者执行Mysql> FLUSH HOSTS;

open_files_limit = 65535 #打开多少文件描述符

table_open_cache = 128

max_allowed_packet = 500M

binlog_cache_size = 1M

max_heap_table_size = 8M

tmp_table_size = 16M

read_buffer_size = 2M

read_rnd_buffer_size = 8M #MySQL的随机读缓冲区大小

sort_buffer_size = 8M

join_buffer_size = 8M

key_buffer_size = 4M

thread_cache_size = 8

query_cache_type = 1 #查询总是先到查询缓存中查找,即使使用了sql_no_cache仍然查询缓存,

#因为sql_no_cache只是不缓存查询结果,而不是不使用查询结果。

query_cache_size = 8M

query_cache_limit = 2M

#ft_min_word_len = 4 #配置最小搜索长度

#从 Mysql 4.0 开始就支持全文索引功能,但是 Mysql 默认的最小索引长度是 4。如果是#英文默认值是比较合理的,但是中文绝大部分词都是2个字符,这就导致小于4个字的词#都不能被索引,全文索引功能就形同虚设了。国内的空间商大部分可能并没有注意到这#个问题,没有修改 Mysql 的默认设置。

log_bin = mysql-bin

binlog_format = mixed

expire_logs_days = 7

log_error = /home/mysql/mysql-error.log

slow_query_log = 1

long_query_time = 1

slow_query_log_file = /home/mysql/mysql-slow.log

performance_schema = 0

explicit_defaults_for_timestamp

#lower_case_table_names = 1

skip-external-locking #跳过外部锁定

#当外部锁定(external-locking)起作用时,每个进程若要访问数据表,则必须等待之前的进程完成

#操作并解除锁定。由于服务器访问数据表时经常需要等待解锁,因此在单服务器环境下external #locking会让MySQL性能下降。所以在很多Linux发行版的源中,MySQL配置文件中默认使用了skip-#external-locking来避免external locking。

default_storage_engine = InnoDB

innodb_file_per_table = 1

innodb_open_files = 500

innodb_buffer_pool_size = 64M

innodb_write_io_threads = 4

innodb_read_io_threads = 4

innodb_thread_concurrency = 0

innodb_purge_threads = 1

innodb_flush_log_at_trx_commit = 2

innodb_log_buffer_size = 2M

innodb_log_file_size = 32M

innodb_log_files_in_group = 3

innodb_max_dirty_pages_pct = 90

innodb_lock_wait_timeout = 120

bulk_insert_buffer_size = 8M

myisam_sort_buffer_size = 8M

myisam_max_sort_file_size = 10G

myisam_repair_threads = 1

interactive_timeout = 28800

wait_timeout = 28800

[mysqldump]

quick

max_allowed_packet = 500M

[myisamchk]

key_buffer_size = 8M

sort_buffer_size = 8M

read_buffer = 4M

mysql_install_dir=/usr/local/mysql

mysql_data_dir=/home/mysql

dbrootpwd=west263.com

${mysql_install_dir}/scripts/mysql_install_db –user=mysql –basedir=${mysql_install_dir} –datadir=${mysql_data_dir}

/bin/cp ${mysql_install_dir}/support-files/mysql.server /etc/init.d/mysqld

sed -i “s@^basedir=.*@basedir=${mysql_install_dir}@” /etc/init.d/mysqld

sed -i “s@^datadir=.*@datadir=${mysql_data_dir}@” /etc/init.d/mysqld

chmod +x /etc/init.d/mysqld

chkconfig –add mysqld

chkconfig mysqld on

service mysqld start

[ -z “$(grep ^’export PATH=’ /etc/profile)” ] && echo “export PATH=${mysql_install_dir}/bin:\$PATH” >> /etc/profile

[ -n “$(grep ^’export PATH=’ /etc/profile)” -a -z “$(grep ${mysql_install_dir} /etc/profile)” ] && sed -i “s@^export PATH=\(.*\)@export PATH=${mysql_install_dir}/bin:\1@” /etc/profile

. /etc/profile

${mysql_install_dir}/bin/mysql -e “grant all privileges on *.* to root@’127.0.0.1’ identified by \“${dbrootpwd}\“ with grant option;”

${mysql_install_dir}/bin/mysql -e “grant all privileges on *.* to root@’localhost’ identified by \“${dbrootpwd}\“ with grant option;”

${mysql_install_dir}/bin/mysql -uroot -p${dbrootpwd} -e “delete from mysql.user where Password=’’;”

${mysql_install_dir}/bin/mysql -uroot -p${dbrootpwd} -e “delete from mysql.db where User=’’;”

${mysql_install_dir}/bin/mysql -uroot -p${dbrootpwd} -e “delete from mysql.proxies_priv where Host!=’localhost’;”

${mysql_install_dir}/bin/mysql -uroot -p${dbrootpwd} -e “drop database test;”

${mysql_install_dir}/bin/mysql -uroot -p${dbrootpwd} -e “reset master;”

http://luopengtest.gotoip2.com/shell/lnmp/mysql.sh mysql自动安装shell脚本