1
+ # ##### 二进制自动安装数据库脚本root密码MANAGER将脚本和安装包放在/root目录即可###############
2
+ # #####数据库目录/usr/local/mysql############
3
+ # #####数据目录/data/mysql############
4
+ # #####日志目录/log/mysql############
5
+ # #####端口号默认3306其余参数按需自行修改############
6
+ # #################
7
+ # #转载 http://blog.51cto.com/suifu/1830575
8
+
9
+ # #################
1
10
#! /bin/bash
2
- #
3
- # ##
4
- # Filename: install_mariadb.sh
5
- # Author: roguo.wei - [email protected]
6
- # Description:
7
- # Last Modified: 2017-01-17 00:31
8
- # Version: 1.0
9
- # ##
10
-
11
- INSTALL_DIR=" /opt/app"
12
- DATA_DIR=" /opt/data"
13
- MARIADB_GROUP=" mysql"
14
- MARIADB_USER=" mysql"
15
- MARIADB_VERSION=" 10.1.20"
16
- ROOT_PASSWD=" root"
17
- # mariadb-10.1.20-linux-x86_64.tar.gz
18
- TAR_NAME=" mariadb-${MARIADB_VERSION} -linux-x86_64.tar.gz"
19
- UNTAR_NAME=" mariadb-${MARIADB_VERSION} -linux-x86_64"
20
-
21
- # check mariadb user
22
- echo -n " check MariaDB user... "
23
- id -u ${MARIADB_USER} & > /dev/null
24
- if [ $? -ne 0 ]; then
25
- groupadd ${MARIADB_GROUP}
26
- useradd -g ${MARIADB_GROUP} ${MARIADB_USER}
11
+ PATH=/bin:/sbin:/usr/bin:/usr/sbin:/opt/bin:/opt/sbin:~ /bin
12
+ export PATH
13
+ # Check if user is root
14
+ if [ $( id -u) != " 0" ]; then
15
+ echo " Error: You must be root to run this script, please use root to install"
16
+ exit 1
27
17
fi
28
- echo " ok"
29
-
30
- # check install dir
31
- [ ! -d " ${INSTALL_DIR} " ] && mkdir -p ${INSTALL_DIR}
32
- [ ! -d " ${DATA_DIR} " ] && mkdir -p ${DATA_DIR}
33
-
34
- # check mariadb file
35
- if [ ! -f ${TAR_NAME} ]; then
36
- wget http://mirrors.tuna.tsinghua.edu.cn/mariadb//mariadb-${MARIADB_VERSION} /bintar-linux-x86_64/mariadb-${MARIADB_VERSION} -linux-x86_64.tar.gz
18
+ clear
19
+ echo " ========================================================================="
20
+ echo " A tool to auto-compile & install MariaDB 10.1.16 on Redhat/CentOS Linux "
21
+ echo " ========================================================================="
22
+ cur_dir=$( pwd)
23
+ # set mysql root password
24
+ echo " ==========================="
25
+ mysqlrootpwd=" MANAGER"
26
+ echo -e " Please input the root password of mysql:"
27
+ read -p " (Default password: MANAGER):" mysqlrootpwd
28
+ if [ " $mysqlrootpwd " = " " ]; then
29
+ mysqlrootpwd=" MANAGER"
37
30
fi
38
-
39
- # untar file
40
- echo -n " untar file ..."
41
- tar -xf ${TAR_NAME} -C ${INSTALL_DIR}
42
- ln -s ${INSTALL_DIR} /${UNTAR_NAME} ${INSTALL_DIR} /mysql
43
- ln -s ${INSTALL_DIR} /${UNTAR_NAME} /usr/local/mysql
44
- echo " ok"
45
-
46
- # init db and config
47
- echo -n " init db..."
48
- ${INSTALL_DIR} /mysql/scripts/mysql_install_db --user=${MARIADB_USER} --basedir=${INSTALL_DIR} /mysql --datadir=${DATA_DIR}
49
- [ $? -ne 0 ] && exit 1
50
- cp ${INSTALL_DIR} /mysql/support-files/mysql.server /etc/init.d/mysqld
51
- echo " ok"
52
-
53
- [ -f /etc/my.cnf ] && mv /etc/my.cnf{,.ori}
54
- \c p -rf $( cd ` dirname $0 ` && pwd) /my.cnf /etc/my.cnf
55
-
56
- # set server id
57
- IPADDR=$( /sbin/ifconfig eth0| grep " inet addr" | awk ' {print $2}' | awk -F" :" ' {print $2}' )
58
- SERVER_ID=$( echo $IPADDR | awk -F" ." ' {print $4}' )
59
- sed -i " s#server-id = .*#server-id = ${SERVER_ID} #g" /etc/my.cnf
60
- sed -i " s#datadir = .*#datadir = ${DATA_DIR} #g" /etc/my.cnf
61
-
62
- # set purview
63
- chown -R root ${INSTALL_DIR} /${UNTAR_NAME}
64
- chown -R ${MARIADB_USER} ${DATA_DIR}
65
- chmod +x /etc/init.d/mysqld
66
-
67
- # start mariadb
68
- ${INSTALL_DIR} /mysql/bin/mysqld_safe --user=${MARIADB_USER} & & > /dev/null
69
- [ $? -ne 0 ] && exit 1 || echo " mariadb started ok"
70
-
71
- # set root passwd
72
- sleep 10
73
- ${INSTALL_DIR} /mysql/bin/mysqladmin -uroot password " ${ROOT_PASSWD} "
74
- if [ $? -ne 0 ]; then
75
- echo " change password for root failed!"
76
- exit 1
77
- else
78
- echo " change password for root to :${ROOT_PASSWD} "
31
+ echo " ==========================="
32
+ echo " MySQL root password:$mysqlrootpwd "
33
+ echo " ==========================="
34
+ # which MySQL Version do you want to install?
35
+ echo " ==========================="
36
+ isinstallmysql56=" n"
37
+ echo " Install MariaDB 10.1.16,Please input y"
38
+ read -p " (Please input y , n):" isinstallmysql56
39
+ case " $isinstallmysql56 " in
40
+ y|Y|Yes|YES|yes|yES|yEs|YeS|yeS)
41
+ echo " You will install MySQL 5.6.25"
42
+ isinstallmysql56=" y"
43
+ ;;
44
+ * )
45
+ echo " INPUT error,You will exit install MariaDB 10.1.16"
46
+ isinstallmysql56=" n"
47
+ exit
48
+ esac
49
+ get_char ()
50
+ {
51
+ SAVEDSTTY=` stty -g`
52
+ stty -echo
53
+ stty cbreak
54
+ # dd if=/dev/tty bs=1 count=1 2> /dev/null
55
+ stty -raw
56
+ stty echo
57
+ stty $SAVEDSTTY
58
+ }
59
+ echo " "
60
+ echo " Press any key to start...or Press Ctrl+c to cancel"
61
+ char=` get_char`
62
+ # Initialize the installation related content.
63
+ function InitInstall()
64
+ {
65
+ cat /etc/issue
66
+ uname -a
67
+ MemTotal=` free -m | grep Mem | awk ' {print $2}' `
68
+ echo -e " \n Memory is: ${MemTotal} MB "
69
+ # Set timezone
70
+ rm -rf /etc/localtime
71
+ ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
72
+ # Delete Old Mysql program
73
+ rpm -qa| grep mysql
74
+ rpm -e mysql
75
+ # Disable SeLinux
76
+ if [ -s /etc/selinux/config ]; then
77
+ sed -i ' s/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
78
+ fi
79
+ setenforce 0
80
+ }
81
+ # Installation of depend on and optimization options.
82
+ function InstallDependsAndOpt()
83
+ {
84
+ cd $cur_dir
85
+ cat >> /etc/security/limits.conf<< EOF
86
+ * soft nproc 65535
87
+ * hard nproc 65535
88
+ * soft nofile 65535
89
+ * hard nofile 65535
90
+ EOF
91
+ echo " fs.file-max=65535" >> /etc/sysctl.conf
92
+ }
93
+ # Install MySQL
94
+ function InstallMySQL56()
95
+ {
96
+ echo " ============================Install MariaDB 10.1.16=================================="
97
+ cd $cur_dir
98
+ # Backup old my.cnf
99
+ # rm -f /etc/my.cnf
100
+ if [ -s /etc/my.cnf ]; then
101
+ mv /etc/my.cnf /etc/my.cnf.` date +%Y%m%d%H%M%S` .bak
79
102
fi
80
-
81
- #
82
- chkconfig mysqld on
83
- ln -sv ${INSTALL_DIR} /mysql/include /usr/include/mysql
84
- echo ' /${INSTALL_DIR}/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
103
+ # mysql directory configuration
104
+ groupadd mysql -g 512
105
+ useradd -u 512 -g mysql -s /sbin/nologin -d /home/mysql mysql
106
+ tar xvf /root/mariadb-10.1.16-linux-x86_64.tar.gz
107
+ mv /root/mariadb-10.1.16-linux-x86_64 /usr/local/mysql
108
+ mkdir -p /data/mysql
109
+ mkdir -p /log/mysql
110
+ chown -R mysql:mysql /data/mysql
111
+ chown -R mysql:mysql /usr/local/mysql
112
+ chown -R mysql:mysql /log
113
+ SERVERID=` ifconfig eth0 | grep " inet addr" | awk ' { print $2}' | awk -F. ' { print $3$4}' `
114
+ cat >> /etc/my.cnf<< EOF
115
+ [client]
116
+ port= 3306
117
+ socket= /tmp/mysql.sock
118
+ default-character-set=utf8
119
+ [mysql]
120
+ default-character-set=utf8
121
+ [mysqld]
122
+ port= 3306
123
+ socket= /tmp/mysql.sock
124
+ basedir= /usr/local/mysql
125
+ datadir= /data/mysql
126
+ open_files_limit = 3072
127
+ back_log = 103
128
+ max_connections = 800
129
+ max_connect_errors = 100000
130
+ table_open_cache = 512
131
+ external-locking = FALSE
132
+ max_allowed_packet = 32M
133
+ sort_buffer_size = 2M
134
+ join_buffer_size = 2M
135
+ thread_cache_size = 51
136
+ query_cache_size = 32M
137
+ tmp_table_size = 96M
138
+ max_heap_table_size = 96M
139
+ slow_query_log = 1
140
+ slow_query_log_file = /log/mysql/slow.log
141
+ log-error = /log/mysql/error.log
142
+ long_query_time = 1
143
+ server-id = $SERVERID
144
+ log-bin = /log/mysql/mysql-bin
145
+ sync_binlog = 1
146
+ binlog_cache_size = 4M
147
+ max_binlog_cache_size = 8M
148
+ max_binlog_size = 1024M
149
+ expire_logs_days = 60
150
+ key_buffer_size = 32M
151
+ read_buffer_size = 1M
152
+ read_rnd_buffer_size = 16M
153
+ bulk_insert_buffer_size = 64M
154
+ character-set-server=utf8
155
+ default-storage-engine = InnoDB
156
+ binlog_format = row
157
+ innodb_buffer_pool_dump_at_shutdown = 1
158
+ innodb_buffer_pool_load_at_startup = 1
159
+ binlog_rows_query_log_events = 1
160
+ explicit_defaults_for_timestamp = 1
161
+ #log_slave_updates=1
162
+ #gtid_mode=on
163
+ #enforce_gtid_consistency=1
164
+ #innodb_write_io_threads = 8
165
+ #innodb_read_io_threads = 8
166
+ #innodb_thread_concurrency = 0
167
+ transaction_isolation = REPEATABLE-READ
168
+ innodb_additional_mem_pool_size = 16M
169
+ innodb_buffer_pool_size = 512M
170
+ #innodb_data_home_dir =
171
+ innodb_data_file_path = ibdata1:1024M:autoextend
172
+ innodb_flush_log_at_trx_commit = 1
173
+ innodb_log_buffer_size = 16M
174
+ innodb_log_file_size = 512M
175
+ innodb_log_files_in_group = 2
176
+ innodb_max_dirty_pages_pct = 50
177
+ innodb_file_per_table = 1
178
+ innodb_locks_unsafe_for_binlog = 0
179
+ wait_timeout = 14400
180
+ interactive_timeout = 14400
181
+ skip-name-resolve
182
+ [mysqldump]
183
+ quick
184
+ max_allowed_packet = 32M
185
+ EOF
186
+ /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/data/mysql --defaults-file=/etc/my.cnf --user=mysql
187
+ cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
188
+ chmod 700 /etc/init.d/mysqld
189
+ chkconfig --add mysql
190
+ chkconfig --level 2345 mysqld on
191
+ cat >> /etc/ld.so.conf.d/mysql-x86_64.conf<< EOF
192
+ /usr/local/mysql/lib
193
+ EOF
85
194
ldconfig
86
-
87
- # set path
88
- echo -n " set path... "
89
- cat >> /etc/profile << EOF
90
- # mariadb path
91
- PATH=\$ PATH:${INSTALL_DIR} /mysql/bin
92
- export PATH
195
+ if [ -d " /proc/vz" ]; then
196
+ ulimit -s unlimited
197
+ fi
198
+ /etc/init.d/mysqld start
199
+ cat >> /etc/profile << EOF
200
+ export PATH=$PATH :/usr/local/mysql/bin
201
+ export LD_LIBRARY_PATH=/usr/local/mysql/lib
202
+ EOF
203
+ /usr/local/mysql/bin/mysqladmin -u root password $mysqlrootpwd
204
+ cat > /tmp/mysql_sec_script<< EOF
205
+ use mysql;
206
+ delete from mysql.user where user!='root' or host!='localhost';
207
+ flush privileges;
93
208
EOF
94
- echo " ok"
95
- . /etc/profile
96
-
97
- echo " mariadb installed successfully!"
209
+ /usr/local/mysql/bin/mysql -u root -p$mysqlrootpwd -h localhost < /tmp/mysql_sec_script
210
+ # rm -f /tmp/mysql_sec_script
211
+ /etc/init.d/mysqld restart
212
+ source /etc/profile
213
+ echo " ============================MariaDB 10.1.16 install completed========================="
214
+ }
215
+ function CheckInstall()
216
+ {
217
+ echo " ===================================== Check install ==================================="
218
+ clear
219
+ ismysql=" "
220
+ echo " Checking..."
221
+ if [ -s /usr/local/mysql/bin/mysql ] && [ -s /usr/local/mysql/bin/mysqld_safe ] && [ -s /etc/my.cnf ]; then
222
+ echo " MySQL: OK"
223
+ ismysql=" ok"
224
+ else
225
+ echo " Error: /usr/local/mysql not found!!!MySQL install failed."
226
+ fi
227
+ if [ " $ismysql " = " ok" ]; then
228
+ echo " Install MariaDB 10.1.16 completed! enjoy it."
229
+ echo " ========================================================================="
230
+ netstat -ntl
231
+ else
232
+ echo " Sorry,Failed to install MySQL!"
233
+ echo " You can tail /root/mysql-install.log from your server."
234
+ fi
235
+ }
236
+ # The installation log
237
+ InitInstall 2>&1 | tee /root/mysql-install.log
238
+ CheckAndDownloadFiles 2>&1 | tee -a /root/mysql-install.log
239
+ InstallDependsAndOpt 2>&1 | tee -a /root/mysql-install.log
240
+ InstallMySQL56 2>&1 | tee -a /root/mysql-install.log
241
+ CheckInstall 2>&1 | tee -a /root/mysql-install.log
0 commit comments