Shell脚本一键编译安装Mysql

因工作量越来越大,使用shell脚本能够提高我们的工作效率并节省时间,让运维工作自动化,所以分享一下mysql一键编译安装的脚本

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
# !/bin/bash    
GROUP=mysql
USER=mysql
MYSQL_HOME=/usr/local/mysql
MYSQL_DATA_HOME=/data/mysql/data
D_VERSION="mysql-boost-5.7.30"
MYSQL_VERSION="mysql-5.7.30"
DOWNLOAD_PATH="/usr/local/src"
echo "=================================start install mysql==================================="
if [ $(id -u) != "0" ];then
echo "Error: You must be root to run this script!"
exit 1
fi
if [ -z $(cat /etc/group|awk -F: '{print $1}'| grep -w "$GROUP") ]
then
groupadd $GROUP
if(( $? == 0 ))
then
echo "group $GROUP add sucessfully!"
fi
else
echo "$GROUP is exsits"
fi
if [ -z $(cat /etc/passwd|awk -F: '{print $1}'| grep -w "$USER") ]
then
adduser -g $GROUP $USER
if (( $? == 0 ))
then
echo "user $USER add sucessfully!"
fi
else
echo "$USER is exsits"
fi
for i in make gcc-c++ bison-devel ncurses-devel perl perl-devel wget cmake bzip2 bzip2-devel bzip2-libs python-devel openssl-devel
do
yum -y install $i
done >/dev/null
echo "==================================downloading mysql===================================="
rm -rf ${DOWNLOAD_PATH}/mysql*
wget https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads/MySQL-5.7/${D_VERSION}.tar.gz -P ${DOWNLOAD_PATH} >/dev/null
if(( $? == 0 ))
then
echo "MySQL DownLoad sucessfully!"
else
echo "MySQL DownLoad failed!"
exit 1
fi
cd ${DOWNLOAD_PATH}
echo "=================================unpackaging mysql====================================="
tar xzvf ${D_VERSION}.tar.gz >/dev/null
cd ${MYSQL_VERSION}
if [ -s /etc/my.cnf ]; then
mv /etc/my.cnf /etc/my.cnf.`date +%Y%m%d%H%M%S`.bak
fi
cat << EOF >/etc/my.cnf
[mysqld]
basedir=/usr/local/mysql
datadir=/data/mysql/data
port=3306
bind-address=0.0.0.0
socket=/usr/local/mysql/mysql.sock
lower_case_table_names=1
slow_query_log=1
long_query_time=1
max_connections=1000
gtid_mode=ON
enforce-gtid-consistency=true
log_error_verbosity=2
back_log=200
open_files_limit=60000
table_open_cache=2000
thread_cache_size=400
server-id=1
lower_case_table_names=1
skip-external-locking
local_infile=0
character-set-server=utf8
collation-server=utf8_general_ci
default-storage-engine=InnoDB
transaction_isolation=READ-COMMITTED
autocommit=1
sql_mode=TRADITIONAL
max_allowed_packet=256M
show_compatibility_56=1
interactive_timeout=7200
wait_timeout=7200
user=mysql
early-plugin-load=''
skip-ssl
explicit_defaults_for_timestamp=1
symbolic-links=0
query_cache_type=0
query_cache_size=0
enforce-gtid-consistency
innodb_undo_tablespaces=16
innodb_max_undo_log_size=1G
innodb_undo_logs=128
innodb_undo_log_truncate=1
innodb_purge_rseg_truncate_frequency=128
innodb_data_file_path=ibdata1:10M:autoextend
innodb_autoextend_increment=128
innodb_file_per_table=1
innodb_log_files_in_group=4
innodb_log_file_size=128M
innodb_log_buffer_size=8M
innodb_flush_log_at_trx_commit=1
innodb_buffer_pool_size=4G
innodb_buffer_pool_instances=4
innodb_old_blocks_pct=40
innodb_max_dirty_pages_pct=90
innodb_io_capacity=200
innodb_io_capacity_max=2000
innodb_doublewrite=1
sort_buffer_size=8M
join_buffer_size=8M
innodb_lock_wait_timeout=30
innodb_thread_concurrency=4
tmp_table_size=256M
max_heap_table_size=128M
innodb_temp_data_file_path=ibtmp1:12M:autoextend:max:4G
log_timestamps=SYSTEM
innodb_stats_auto_recalc=1
innodb_stats_persistent=1
log_output='TABLE'
slow_query_log=0
min_examined_row_limit=0
log_queries_not_using_indexes=0
log_throttle_queries_not_using_indexes=20
log_slow_admin_statements=1
general_log=0
concurrent_insert=1
key_buffer_size=128M
read_buffer_size=2M
read_rnd_buffer_size=8M
myisam_sort_buffer_size=64M
log_slave_updates=0
relay_log_purge = 1
relay_log_recovery = 1
master_info_repository=TABLE
relay_log_info_repository=TABLE
slave_parallel_workers=2
slave_parallel_type=LOGICAL_CLOCK
EOF
echo "==========================configuring mysql,please wait================================"
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/data/mysql/data \
-DSYSCONFDIR=/etc \
-DWITH_READLINE=1 \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DMYSQL_TCP_PORT=3306 \
-DENABLED_LOCAL_INFILE=1 \
-DENABLE_DOWNLOADS=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_DEBUG=0 \
-DWITH_SSH=system \
-DMYSQL_MAINTAINER_MODE=0 \
-DWITH_BOOST=boost >/dev/null
echo "==========================make mysql, pleas,please wait================================"
make && make install
if [ $? -ne 0 ];then
echo "make failed ,please check it out!"
exit 1
fi
mkdir -p "${MYSQL_DATA_HOME}"
chown -R mysql:mysql $MYSQL_HOME
chown -R mysql:mysql $MYSQL_DATA_HOME
$MYSQL_HOME/bin/mysqld --initialize-insecure --user=$USER
cp $MYSQL_HOME/support-files/mysql.server /etc/init.d/mysql
echo "export PATH=$PATH:/usr/local/mysql/bin/" >>/etc/profile
source /etc/profile
/etc/init.d/mysql start
echo "==========================Mysql installation completed================================"
exit 0

注意事项:

1.mysql 密码默认为空