MySql教程

MySQL 5.7自动安装脚本(shell)

本文主要是介绍MySQL 5.7自动安装脚本(shell),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

前言

  无论是工作中还是测试中,手动安装或者卸载MySQL步骤多比较繁琐。脚本写的比较简单。

安装脚本

  1 #!/bin/bash
  2 set -e
  3 MYSQL_PASSWD="TY520@ctyun^1"
  4 MYSQL_PORT='3306'
  5 
  6 #下载安装包
  7 wget_mysql_binary() {
  8 echo "==========================================下载源码包==================================="
  9 if read -t 60 -p "Do you download the MySQL installation package(y/n)?" WGET_VALUE
 10 then
 11     if [ "$WGET_VALUE" = "y" ]
 12     then
 13         wget https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.33-el7-x86_64.tar.gz
 14     fi
 15 else
 16     WGET_VALUE = "n"
 17 fi
 18 }
 19 
 20 #删除mariadb包
 21 drop_mariadb() {
 22 echo "=========================================删除mariadb==================================="
 23 rpm -qa | grep mariadb | xargs rpm -e --nodeps
 24 }
 25 
 26 #安装依赖包
 27 yum_install_depend() {
 28 echo "==========================================安装依赖包==================================="
 29 yum install libaio -y
 30 }
 31 
 32 #创建mysql用户
 33 user_add() {
 34 echo "========================================创建mysql用户=================================="
 35 useradd mysql >>/dev/null
 36 if read -t 60 -p "(Please input the mysql password(default:mysql):" PASSWD
 37 then
 38     if [ "$PASSWD" = "" ]; then
 39         PASSWD="$MYSQL_PASSWD"
 40     fi
 41 else
 42     PASSWD="$MYSQL_PASSWD"
 43 fi
 44 echo "$PASSWD" | passwd mysql --stdin
 45 echo "export PATH=\$PATH:/usr/local/mysql/bin" >>/home/mysql/.bashrc
 46 }
 47 
 48 #配置MySQL目录
 49 install_mysql_path() {
 50 echo "==========================================MySQL目录===================================="
 51 
 52 if [ -d /usr/local/mysql ]
 53 then
 54     mv /usr/local/mysql /usr/local/mysql_$(date +%Y-%m-%d-%H-%M)
 55 fi
 56 
 57 tar -zxf mysql-5.7.33-el7-x86_64.tar.gz -C /usr/local/
 58 ln -s /usr/local/mysql-5.7.33-el7-x86_64 /usr/local/mysql
 59 chown -R root:mysql /usr/local/mysql*
 60 
 61 echo -e "Please input the DATADIR of MySQL:"
 62 read -p "(Default DATADIR: /usr/local/mysql/data):" DATADIR
 63 if [ "$DATADIR" = "" ]; then
 64     DATADIR="/usr/local/mysql/data"
 65 fi
 66 
 67 mkdir -p $DATADIR
 68 chown -R mysql:mysql $DATADIR/
 69 chmod -R 750 $DATADIR
 70 
 71 if [ $? -eq 0 ]
 72 then
 73     echo "Mkdir path Successfully."
 74 else
 75     echo "Mkdir path Failed!!!"
 76 fi
 77 }
 78 
 79 #初始化MySQL
 80 initdb_mysql() {
 81 echo "=========================================初始化MySQL==================================="
 82 su - mysql -c "mysqld --initialize-insecure --user=mysql"
 83 
 84 mkdir -p $DATADIR/logs/{log-bin,log-group,log-relay}
 85 chown -R mysql:mysql $DATADIR/logs
 86 
 87 if [ $? -eq 0 ]
 88 then
 89     echo "MySQL initialize Successfully."
 90 else
 91     echo "MySQL initialize Failed!!!"
 92 fi
 93 }
 94 
 95 #生成MySQL参数文件
 96 conf_parameter_file() {
 97 echo "======================================编辑my.cnf配置文件==============================="
 98 if [ -f /etc/mysql/my.cnf ]
 99 then
100     mv /etc/mysql/my.cnf /etc/mysql/my.cnf_$(date +%Y-%m-%d-%H:%M)    
101 fi
102 
103 if [ -f /etc/my.cnf ]
104 then
105     mv /etc/my.cnf /etc/my.cnf_$(date +%Y-%m-%d-%H:%M)    
106 fi
107 
108 SERVER_ID=$(date +%s)
109 mkdir -p /etc/mysql/    
110 cat >> /etc/mysql/my.cnf <<EOF
111 [client]
112 user = root
113 #password = $PASSWD
114 
115 [mysqld]
116 bind_address = 0.0.0.0
117 port = $MYSQL_PORT
118 basedir = /usr/local/mysql
119 datadir = $DATADIR
120 socket = /tmp/mysql.sock
121 
122 #字符
123 character_set_server = utf8mb4
124 lower-case-table-names = 1
125 
126 #密码策略
127 #plugin-load-add = validate_password.so
128 default_authentication_plugin=mysql_native_password
129 
130 #日志设置
131 log-bin = $DATADIR/logs/log-bin/mysql-bin
132 binlog_format = row
133 binlog_row_image = full
134 log_bin_trust_function_creators = 1
135 expire_logs_days = 30
136 
137 #general_log = on
138 general_log_file = $DATADIR/logs/general.log 
139 
140 log_error = $DATADIR/logs/error.log
141 
142 slow_query_log = on
143 slow_query_log_file = $DATADIR/logs/slow.log
144 long_query_time = 2
145 #min_examined_row_limit = 100
146 #log_queries_not_using_indexes = on
147 #log_throttle_queries_not_using_indexes = 4
148 log_slow_admin_statements = on
149 log_output = file
150 log_slow_slave_statements = on
151 
152 relay-log = $DATADIR/logs/log-relay/relay_log
153 
154 #连接
155 max-connections = 1000
156 back_log = 300
157 max_allowed_packet = 64M
158 interactive_timeout = 28800
159 wait_timeout = 28800
160 
161 #缓存
162 sort_buffer_size = 32M
163 tmp_table_size = 32M
164 join_buffer_size = 32M
165 read_buffer_size = 32M
166 read_rnd_buffer_size = 32M
167 key_buffer_size = 64M
168 myisam_sort_buffer_size = 64M
169 max_heap_table_size = 16M
170 open_files_limit = 65535
171 thread_cache_size = 512
172 table_open_cache = 16384
173 table_definition_cache = 16384
174 table_open_cache_instances = 8
175 
176 #InnoDB
177 default-storage-engine = INNODB
178 innodb_adaptive_flushing = ON
179 innodb_adaptive_flushing_lwm = 15
180 innodb_flushing_avg_loops = 30
181 innodb_adaptive_hash_index = ON
182 innodb_data_home_dir = $DATADIR
183 innodb_file_per_table = ON
184 innodb_log_group_home_dir = $DATADIR/logs/log-group/
185 innodb_log_files_in_group = 3
186 innodb_log_buffer_size = 32M
187 innodb_flush_log_at_trx_commit = 1
188 innodb_flush_log_at_timeout = 1
189 innodb_lock_wait_timeout = 30
190 innodb_buffer_pool_instances = 8
191 innodb_change_buffering = all
192 innodb_change_buffer_max_size = 10
193 innodb_autoextend_increment = 64
194 innodb_thread_concurrency = 32
195 innodb_spin_wait_delay = 6
196 innodb_sync_spin_loops = 30
197 innodb_open_files = 65535
198 innodb_old_blocks_time = 30
199 innodb_online_alter_log_max_size = 512M
200 innodb_write_io_threads = 8
201 innodb_read_io_threads = 8
202 innodb_flush_neighbors = 0
203 innodb_io_capacity = 3000
204 innodb_io_capacity_max = 16000
205 innodb_lru_scan_depth = 1024
206 innodb_purge_threads = 4
207 innodb_purge_batch_size = 400
208 innodb_buffer_pool_filename = ib_buffer_pool
209 innodb_buffer_pool_load_abort = OFF
210 innodb_buffer_pool_dump_now = OFF
211 innodb_buffer_pool_load_now = OFF
212 innodb_max_dirty_pages_pct = 75
213 innodb_max_dirty_pages_pct_lwm = 0
214 #索引列长度
215 innodb_default_row_format = dynamic
216 innodb_file_format = Barracuda
217 innodb_large_prefix = ON
218 
219 #Replication
220 server_id = $SERVER_ID
221 #report_host = 10.46.203.123                   #本地IP
222 gtid_mode = on
223 enforce_gtid_consistency = on
224 #rpl_semi_sync_master_enabled = on              #安装插件后启用
225 #rpl_semi_sync_slave_enabled = on               #安装插件后启用
226 #rpl_semi_sync_master_wait_point = AFTER_SYNC   #安装插件后启用
227 #rpl_semi_sync_master_timeout = 5000            #安装插件后启用
228 #skip-slave-start = on                           启动MySQL跳过启动slave进程
229 slave_parallel_type = LOGICAL_CLOCK
230 slave_parallel_workers = 16
231 slave_preserve_commit_order = on
232 log_slave_updates = on
233 
234 [mysql]
235 prompt=(\u@\h) [\d]>\_
236 no-auto-rehash
237 EOF
238 chown -R mysql:mysql /etc/mysql
239 
240 if [ $? -eq 0 ]
241 then
242     echo "Configure my.cnf Successfully."
243 else
244     echo "Configure my.cnf Failed!!!"
245 fi
246 }
247 
248 #安装MySQL SSL
249 install_ssl() {
250 echo "========================================安装MySQL SSL=================================="
251 su - mysql -c "mysql_ssl_rsa_setup"
252 chown mysql:mysql $DATADIR/*.pem
253 if [ $? -eq 0 ]
254 then
255     echo "Install MySQL SSL Successfully."
256 else
257     echo "Install MySQL SSL Failed!!!"
258 fi
259 }
260 
261 #配置MySQL启动文件
262 conf_mysqld_file() {
263 echo "===================================配置/etc/init.d/mysqld=============================="
264 if [ -f /etc/init.d/mysqld ]
265 then
266     mv /etc/init.d/mysqld /etc/init.d/mysqld_$(date +%Y-%m-%d-%H:%M)
267 fi
268 
269 cp -f /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
270 if [ $? -eq 0 ]
271 then
272     echo "Configure /etc/init.d/mysqld Successfully."
273 else
274     echo "Configure /etc/init.d/mysqld Failed!!!"
275 fi
276 }
277 
278 #启动MySQL
279 start_mysql() {
280 echo "============启动MySQl============="
281 su - mysql -c "/etc/init.d/mysqld start"
282 }
283 
284 #修改数据库root用户的密码
285 change_mysql_root_password() {
286 echo "==========================================启动MySQL===================================="
287 echo -e "Please set the MySQL root password:"
288 read -p "(The default MySQL root password is 'root'):" MYSQL_PASSWD
289 if [ "$MYSQL_PASSWD" = "" ]; then
290     MYSQL_PASSWD="root"
291 fi
292 su - mysql -c mysql <<EOF
293 alter user 'root'@'localhost' identified by "$MYSQL_PASSWD";
294 create user 'root'@'%' identified by "$MYSQL_PASSWD";
295 grant all privileges on *.* to 'root'@'%';
296 flush privileges;
297 exit
298 EOF
299 if [ $? -eq 0 ]
300 then
301     echo "Change mysql password Successfully."
302 else
303     echo "Change mysql password Failed!!!"
304 fi
305 }
306 
307 #修改my.cnf文件
308 change_parater_passwd() {
309 echo "========================================修改my.cnf文件================================="
310 sed -i "s/#password = root/password = $MYSQL_PASSWD/g" /etc/mysql/my.cnf
311 }
312 
313 #下载安装包
314 wget_mysql_binary
315 
316 #删除mariadb包
317 drop_mariadb
318 
319 #安装依赖包
320 yum_install_depend
321 
322 #创建用户和组
323 user_add
324 
325 #配置MySQL目录
326 install_mysql_path
327 
328 #初始化MySQL
329 initdb_mysql
330 
331 #生成MySQL参数文件
332 conf_parameter_file
333 
334 #安装MySQL SSL
335 install_ssl
336 
337 #配置MySQL启动文件
338 conf_mysqld_file
339 
340 #启动MySQL
341 start_mysql
342 
343 #修改数据库root用户的密码
344 change_mysql_root_password
345 
346 #修改my.cnf文件
347 change_parater_passwd
348 
349 echo -e "Os mysql password is:$PASSWD"
350 echo -e "MySQL database root password is:$MYPASSWD"
mysql_5.7_auto_install.sh

 

这篇关于MySQL 5.7自动安装脚本(shell)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!