mysql 5.7安装脚本

  1 [root@HE2 ~]# cat mysql_auto_install.sh
  2 ###### 二进制自动安装数据库脚本root密码MANAGER将脚本和安装包放在/root目录即可###############
  3 ######数据库目录/usr/local/mysql############
  4 ######数据目录/data/mysql############
  5 ######慢日志目录/data/slowlog############
  6 ######端口号默认3306其余参数按需自行修改############
  7  
  8 ##################
  9 #author 10 ##################
 11 #!/bin/bash
 12  
 13  
 14 # Check if user is root
 15 if [ $(id -u) != "0" ]; then
 16     echo "Error: You must be root to run this script, please use root to install"
 17     exit 1
 18 fi
 19  
 20 clear
 21 echo "========================================================================="
 22 echo "A tool to auto-compile & install MySQL 5.7.15 on Redhat/CentOS Linux "
 23 echo "========================================================================="
 24 cur_dir=$(pwd)
 25  
 26 #set mysql root password
 27     echo "==========================="
 28  
 29     mysqlrootpwd="MANAGER"
 30     echo -e "Please input the root password of mysql:"
 31     read -p "(Default password: MANAGER):" mysqlrootpwd
 32     if [ "$mysqlrootpwd" = "" ]; then
 33         mysqlrootpwd="MANAGER"
 34     fi
 35     echo "==========================="
 36     echo "MySQL root password:$mysqlrootpwd"
 37     echo "==========================="
 38  
 39 #which MySQL Version do you want to install?
 40 echo "==========================="
 41  
 42  
 43     isinstallmysql57="n"
 44     echo "Install MySQL 5.7.15,Please input y"
 45     read -p "(Please input y , n):" isinstallmysql57
 46  
 47  
 48     case "$isinstallmysql57" in
 49     y|Y|Yes|YES|yes|yES|yEs|YeS|yeS)
 50     echo "You will install MySQL 5.7.15"
 51  
 52     isinstallmysql57="y"
 53     ;;
 54     *)
 55     echo "INPUT error,You will exit install MySQL 5.7.15"
 56  
 57     isinstallmysql57="n"
 58     exit
 59     esac
 60  
 61     get_char()
 62     {
 63     SAVEDSTTY=`stty -g`
 64     stty -echo
 65     stty cbreak
 66     #dd if=/dev/tty bs=1 count=1 2> /dev/null
 67     stty -raw
 68     stty echo
 69     stty $SAVEDSTTY
 70     }
 71     echo ""
 72     echo "Press any key to start...or Press Ctrl+c to cancel"
 73     char=`get_char`
 74  
 75 # Initialize  the installation related content.
 76 function InitInstall()
 77 {
 78     cat /etc/issue
 79     uname -a
 80     MemTotal=`free -m | grep Mem | awk '{print  $2}'`  
 81     echo -e "
 Memory is: ${MemTotal} MB "
 82     #Set timezone
 83     #rm -rf /etc/localtime
 84     #ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
 85  
 86  
 87     #Delete Old Mysql program
 88     rpm -qa|grep mysql
 89     rpm -e mysql
 90  
 91  
 92  
 93  
 94  
 95  
 96     #Disable SeLinux
 97     if [ -s /etc/selinux/config ]; then
 98     sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
 99     fi
100     setenforce 0
101  
102  
103 }
104  
105  
106 #Installation of depend on and optimization options.
107 function InstallDependsAndOpt()
108 {
109 cd $cur_dir
110  
111 cat >>/etc/security/limits.conf<<EOF
112 * soft nproc 65535
113 * hard nproc 65535
114 * soft nofile 65535
115 * hard nofile 65535
116 EOF
117  
118 echo "fs.file-max=65535" >> /etc/sysctl.conf
119 }
120  
121 #Install MySQL
122 function InstallMySQL57()
123 {
124 echo "============================Install MySQL 5.7.15=================================="
125 cd $cur_dir
126  
127 #Backup old my.cnf
128 #rm -f /etc/my.cnf
129 if [ -s /etc/my.cnf ]; then
130     mv /etc/my.cnf /etc/my.cnf.`date +%Y%m%d%H%M%S`.bak
131 fi
132  
133 echo "============================MySQL 5.7.15 installing…………========================="
134 #mysql directory configuration
135 tar xvf /root/mysql-5.7.15-linux-glibc2.5-x86_64.tar.gz
136 mv /root/mysql-5.7.15-linux-glibc2.5-x86_64 /usr/local/mysql
137 groupadd mysql -g 512
138 useradd -u 512 -g mysql -s /sbin/nologin -d /home/mysql mysql
139 mkdir -p /data/mysql
140 mkdir -p /data/slowlog
141 chown -R mysql:mysql /data/mysql
142 chown -R mysql:mysql /usr/local/mysql
143 chown -R mysql:mysql /data/slowlog
144  
145  
146 #edit /etc/my.cnf
147 SERVERID=`ifconfig eth0 | grep "inet addr" | awk '{ print $2}'| awk -F. '{ print $3$4}'`
148 cat >>/etc/my.cnf<<EOF
149 [client]
150 port=3306
151 socket=/tmp/mysql.sock
152 default-character-set=utf8
153  
154 [mysql]
155 no-auto-rehash
156 default-character-set=utf8
157  
158 [mysqld]
159 port=3306
160 character-set-server=utf8
161 socket=/tmp/mysql.sock
162 basedir=/usr/local/mysql
163 datadir=/data/mysql
164 explicit_defaults_for_timestamp=true
165 lower_case_table_names=1
166 back_log=103
167 max_connections=3000
168 max_connect_errors=100000
169 table_open_cache=512
170 external-locking=FALSE
171 max_allowed_packet=32M
172 sort_buffer_size=2M
173 join_buffer_size=2M
174 thread_cache_size=51
175 query_cache_size=32M
176 #query_cache_limit=4M
177 transaction_isolation=REPEATABLE-READ
178 tmp_table_size=96M
179 max_heap_table_size=96M
180  
181 ###***slowqueryparameters
182 long_query_time=1
183 slow_query_log = 1
184 slow_query_log_file=/data/slowlog/slow.log
185  
186 ###***binlogparameters
187 log-bin=mysql-bin
188 binlog_cache_size=4M
189 max_binlog_cache_size=4096M
190 max_binlog_size=1024M
191 binlog_format=MIXED
192 expire_logs_days=7
193  
194 ###***relay-logparameters
195 #relay-log=/data/3307/relay-bin
196 #relay-log-info-file=/data/3307/relay-log.info
197 #master-info-repository=table
198 #relay-log-info-repository=table
199 #relay-log-recovery=1
200  
201 #***MyISAMparameters
202 key_buffer_size=16M
203 read_buffer_size=1M
204 read_rnd_buffer_size=16M
205 bulk_insert_buffer_size=1M
206  
207 #skip-name-resolve
208  
209 ###***master-slavereplicationparameters
210 server-id=$SERVERID
211 #slave-skip-errors=all
212  
213 #***Innodbstorageengineparameters
214 innodb_buffer_pool_size=512M
215 innodb_data_file_path=ibdata1:10M:autoextend
216 #innodb_file_io_threads=8
217 innodb_thread_concurrency=16
218 innodb_flush_log_at_trx_commit=1
219 innodb_log_buffer_size=16M
220 innodb_log_file_size=512M
221 innodb_log_files_in_group=2
222 innodb_max_dirty_pages_pct=75
223 innodb_buffer_pool_dump_pct=50
224 innodb_lock_wait_timeout=50
225 innodb_file_per_table=on
226  
227 [mysqldump]
228 quick
229 max_allowed_packet=32M
230  
231 [myisamchk]
232 key_buffer=16M
233 sort_buffer_size=16M
234 read_buffer=8M
235 write_buffer=8M
236  
237 [mysqld_safe]
238 open-files-limit=8192
239 log-error=/data/mysql/error.log
240 pid-file=/data/mysql/mysqld.pid
241  
242 EOF
243  
244  
245  
246  
247 /usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --user=mysql --datadir=/data/mysql --basedir=/usr/local/mysql --initialize-insecure
248  
249 cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
250 chmod 700 /etc/init.d/mysqld
251 chkconfig --add mysqld
252 chkconfig --level 2345 mysqld on
253  
254 cat >> /etc/ld.so.conf.d/mysql-x86_64.conf<<EOF
255 /usr/local/mysql/lib
256 EOF
257 ldconfig
258  
259 if [ -d "/proc/vz" ];then
260 ulimit -s unlimited
261 fi
262  
263 /etc/init.d/mysqld start
264  
265  
266 cat >> /etc/profile <<EOF
267 export PATH=$PATH:/usr/local/mysql/bin
268 export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/mysql/lib
269 EOF
270  
271 /usr/local/mysql/bin/mysqladmin -u root password $mysqlrootpwd
272  
273 cat > /tmp/mysql_sec_script<<EOF
274 use mysql;
275 delete from mysql.user where user!='root' or host!='localhost';
276 grant all privileges on *.* to 'sys_admin'@'%' identified by 'MANAGER';
277 flush privileges;
278 EOF
279  
280 /usr/local/mysql/bin/mysql -u root -p$mysqlrootpwd -h localhost < /tmp/mysql_sec_script
281  
282 #rm -f /tmp/mysql_sec_script
283  
284  
285 #/etc/init.d/mysqld restart
286  
287  
288  
289  
290 echo "============================MySQL 5.7.15 install completed========================="
291 }
292  
293  
294  
295 function CheckInstall()
296 {
297 echo "===================================== Check install ==================================="
298 clear
299 ismysql=""
300 echo "Checking..."
301  
302 if [ -s /usr/local/mysql/bin/mysql ] && [ -s /usr/local/mysql/bin/mysqld_safe ] && [ -s /etc/my.cnf ]; then
303   echo "MySQL: OK"
304   ismysql="ok"
305   else
306   echo "Error: /usr/local/mysql not found!!!MySQL install failed."
307 fi
308  
309 if [ "$ismysql" = "ok" ]; then
310 echo "Install MySQL 5.7.15 completed! enjoy it."
311 echo "========================================================================="
312 netstat -ntl
313 else
314 echo "Sorry,Failed to install MySQL!"
315 echo "You can tail /root/mysql-install.log from your server."
316 fi
317 }
318  
319 #The installation log
320 InitInstall 2>&1 | tee /root/mysql-install.log
321 InstallDependsAndOpt 2>&1 | tee -a /root/mysql-install.log
322 InstallMySQL57 > /dev/null
323 CheckInstall 2>&1 | tee -a /root/mysql-install.log
原文地址:https://www.cnblogs.com/yangliheng/p/6083045.html