Mysql8.0 - 一键安装脚本
0. 概述
我之前写过一篇博客,https://www.cnblogs.com/ddzj01/p/10678296.html ,讲述了mysql5.7如何一键安装在centos6上面。现在公司开始采用centos7 + mysql8.0的架构,由于mysql8.0的参数设置有变化,因此重新写一篇博客记录下。
1. 准备mysql的安装包
mysql的安装包下载地址:https://dev.mysql.com/downloads/mysql/
注意需要下载的是linux - Generic版本 64bit,文件名类似于:(mysql-8.0.22-linux-glibc2.12-x86_64.tar.xz)
2. 准备install.sh
这个安装脚本对比老的版本,它能更清晰的反映脚本执行到哪一步了,而且安装前会对系统进行预检,如果发现不适合安装,会有很显眼的错误提示。
#!/bin/sh
# author: yang bao
# time: 2021-02-09
# note: this script is used to install mysql8.0 on centos7.
# 1. at first, you should prepare mysql install binary package like 'mysql-8.0.22-linux-glibc2.12-x86_64.tar.xz'
# and install.sh and my.cnf under same directory such as /root.
# 2. then exec 'sh install.sh' and see the output.
# log function, 0 = pass, 1 = fail
log() {
if [ $1 -eq 0 ]; then
echo " --> PASS"
elif [ $1 -eq 1 ]; then
echo -e "\033[1;41;33m --> FAIL \033[0m"
echo -e "\033[1;41;33m $2 \033[0m"
exit 1
fi
}
# prepare mysql related file
echo "[1] start to check mysql related file --> PASS"
echo -n "[1.1] check if there is mysql install binary package"
cnt1=`find . -maxdepth 1 -name 'mysql-*-linux-glibc2.12-x86_64.tar.xz' -type f | wc -l`
if [ $cnt1 -lt 1 ]; then
log 1 "It seems there isn't mysql install binary package in current directory!"
elif [ $cnt1 -gt 1 ]; then
log 1 "It seems there are too many mysql install binary packages in current directory,\
please just keep one, rename or move the others!"
else
log 0
fi
echo -n "[1.2] check if there is my.cnf in current directory"
cnt2=`find . -maxdepth 1 -name 'my.cnf' -type f | wc -l`
if [ $cnt2 -lt 1 ]; then
log 1 "It seems there isn't my.cnf in current directory!"
else
log 0
fi
echo -n "[1.3] check if there is my.cnf in /etc"
cnt3=`find /etc -maxdepth 1 -name 'my.cnf' -type f | wc -l`
if [ $cnt3 -eq 1 ]; then
log 1 "It seems there is my.cnf in /etc already, please delete it first!"
else
log 0
fi
echo -n "[1.4] check if there is /opt/mydata"
cnt4=`find / -maxdepth 1 -name 'opt' -type d | wc -l`
if [ $cnt4 -eq 1 ]; then
cnt5=`find /opt -maxdepth 1 -name 'mydata' -type d | wc -l`
if [ $cnt5 -eq 1 ]; then
log 1 "It seems there is /opt/mydata already, please delete it first!"
else
log 0
fi
else
log 0
fi
echo -n "[2] check if there is mysql user"
id mysql &> /dev/null
if [ $? -eq 0 ]; then
log 1 "mysql user is alreay exist, please delete it first!"
else
log 0
fi
echo "[3] clear old mysql version --> PASS"
rpm -qa | grep -i mysql | grep -v libs | xargs rpm -ev --nodeps &> /dev/null
echo "[4] install required package --> PASS"
yum install -y libaio &> /dev/null
echo -n "[5] check if there is libaio package"
cnt6=`rpm -qa | grep libaio | wc -l`
if [ $cnt6 -lt 1 ]; then
log 1 "libaio package is not install, please check!"
else
log 0
fi
echo "[6] adjust parameter in /etc/security/limits.conf --> PASS"
cat >> /etc/security/limits.conf << EOF
mysql soft nproc 16384
mysql hard nproc 16384
mysql soft nofile 65536
mysql hard nofile 65536
mysql soft stack 1024000
mysql hard stack 1024000
EOF
echo "[7] adjust parameter in /etc/sysctl.conf --> PASS"
echo "vm.swappiness = 5" >> /etc/sysctl.conf
sysctl -p &> /etc/null
echo "[8] turn off firewall --> PASS"
systemctl stop firewalld
systemctl disable firewalld
sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
echo "[9] move my.cnf to /etc --> PASS"
mv my.cnf /etc/
echo "[10] add user mysql --> PASS"
groupadd mysql
useradd -r -g mysql -s /bin/false mysql
echo "[11] prepare directory --> PASS"
mkdir -p /opt/mydata/data
mkdir -p /opt/mydata/tmp
mkdir -p /opt/mydata/log/binlog
mkdir -p /opt/mydata/log/relaylog
chown -R mysql:mysql /opt/mydata
echo "[12] add path to profile --> PASS"
echo 'export PATH=$PATH:/usr/local/mysql/bin' >> /root/.bash_profile
echo -n "[13] unpackage mysql..."
rm -rf /usr/local/mysql-*-linux-glibc2.12-x86_64 mysql
tar -xvf mysql-*-linux-glibc2.12-x86_64.tar.xz -C /usr/local/ &> /etc/null
cd /usr/local/
ln -s mysql-*-linux-glibc2.12-x86_64 mysql
echo " --> PASS"
echo -n "[14] start initialize mysql..."
cd mysql
bin/mysqld --initialize --user=mysql
if [ $? -ne 0 ]; then
log 1 "mysql initialize failed, please check the error log!"
else
log 0
fi
echo -n "[15] start run mysql"
nohup bin/mysqld_safe --user=mysql &
if [ $? -ne 0 ]; then
log 1 "mysql start failed, please check the error log!"
else
log 0
fi
cp support-files/mysql.server /etc/init.d/mysql
cnt7=`ps -ef | grep mysqld | grep -v grep | wc -l`
while [ $cnt7 -lt 2 ]
do
echo " wait mysql startup..."
sleep 3
cnt7=`ps -ef | grep mysqld | grep -v grep | wc -l`
done
echo "[16] wait 10s for mysql startup completly --> PASS"
sleep 10
echo "[17] change mysql root password --> PASS"
pass=`grep "temporary password" /opt/mydata/log/error.log |awk -F " " '{print $13}'`
/usr/local/mysql/bin/mysqladmin -uroot -p$pass password 'root'
echo -e "[18]\033[1;41;33m root@localhost initial password is 'root'\033[0m"
exit 0
3. 准备my.cnf文件
由于我这个是测试环境,所以内存值调的比较小,生产上面可以将下面几个参数进行调整
innodb_buffer_pool_size # buffer pool大小,一般设置成物理内存 * 60%
innodb_max_undo_log_size # 单个undo表空间最大值
innodb_log_file_size # 单个redo日志大小
[client]
port = 3306
socket = /opt/mydata/data/mysql.sock
default_character_set = utf8mb4
[mysql]
prompt="(\\u@\\h)[\\d]> "
default_character_set = utf8mb4
[mysqldump]
default_character_set = utf8mb4
[mysqld]
# basic settings #
server_id = 128
user = mysql
port = 3306
basedir = /usr/local/mysql
datadir = /opt/mydata/data
tmpdir = /opt/mydata/tmp
pid_file = /opt/mydata/data/mysql.pid
socket = /opt/mydata/data/mysql.sock
character_set_server = utf8mb4
collation_server = utf8mb4_unicode_ci
transaction_isolation = READ-COMMITTED
explicit_defaults_for_timestamp = 1
max_allowed_packet = 1024M
lower_case_table_names = 1
secure_file_priv = ''
open_files_limit = 65535
skip-ssl
default_time_zone='+8:00'
# connection #
skip_name_resolve = 1
max_connections = 1000
max_user_connections = 1000
max_connect_errors = 1000000
thread_cache_size = 512
default_authentication_plugin = mysql_native_password
# memory && myisam #
max_heap_table_size = 128M
tmp_table_size = 128M
join_buffer_size = 16M
key_buffer_size = 64M
bulk_insert_buffer_size = 16M
myisam_sort_buffer_size = 64M
myisam_max_sort_file_size = 6G
myisam_recover_options = DEFAULT
# log settings #
log_error = /opt/mydata/log/error.log
log_timestamps = SYSTEM
slow_query_log_file = /opt/mydata/log/slowquery.log
slow_query_log = 1
long_query_time = 10
log_queries_not_using_indexes = 1
log_throttle_queries_not_using_indexes = 10
min_examined_row_limit = 100
log_slow_admin_statements = 1
log_bin = /opt/mydata/log/binlog/binlog
binlog_format = row
binlog_expire_logs_seconds = 604800
binlog_rows_query_log_events = 1
binlog_row_image = minimal
binlog_cache_size = 8M
max_binlog_cache_size = 2G
max_binlog_size = 1G
log_bin_trust_function_creators = 1
general_log = 0
general_log_file= /opt/mydata/log/general.log
# innodb settings #
innodb_data_file_path = ibdata1:1024M:autoextend
innodb_buffer_pool_size = 1G
innodb_lock_wait_timeout = 10
innodb_io_capacity = 4000
innodb_io_capacity_max = 8000
innodb_flush_method = O_DIRECT
innodb_flush_neighbors = 0
innodb_max_undo_log_size = 2G
innodb_log_file_size = 1G
innodb_log_files_in_group = 4
innodb_log_buffer_size = 32M
innodb_thread_concurrency = 16
innodb_print_all_deadlocks = 1
innodb_sort_buffer_size = 16M
innodb_write_io_threads = 4
innodb_read_io_threads = 8
innodb_rollback_on_timeout = 1
innodb_file_per_table = 1
innodb_open_files = 65535
innodb_stats_persistent_sample_pages = 64
innodb_autoinc_lock_mode = 2
# slave #
relay_log = /opt/mydata/log/relaylog/relaylog
log_slave_updates = 1
relay_log_purge = 1
relay_log_space_limit = 30G
relay_log_recovery = 1
relay_log_info_repository = TABLE
master_info_repository = TABLE
skip-slave-start
至此当前目录已有三个文件
[root@mysqltest ~]# ls
install.sh my.cnf mysql-8.0.22-linux-glibc2.12-x86_64.tar.xz
4. 执行脚本,并查看输出
[root@mysqltest ~]# sh install.sh
[1] start to check mysql related file --> PASS
[1.1] check if there is mysql install binary package --> PASS
[1.2] check if there is my.cnf in current directory --> PASS
[1.3] check if there is my.cnf in /etc --> PASS
[1.4] check if there is /opt/mydata --> PASS
[2] check if there is mysql user --> PASS
[3] clear old mysql version --> PASS
[4] install required package --> PASS
[5] check if there is libaio package --> PASS
[6] adjust parameter in /etc/security/limits.conf --> PASS
[7] adjust parameter in /etc/sysctl.conf --> PASS
[8] turn off firewall --> PASS
[9] move my.cnf to /etc --> PASS
[10] add user mysql --> PASS
[11] prepare directory --> PASS
[12] add path to profile --> PASS
[13] unpackage mysql... --> PASS
[14] start initialize mysql... --> PASS
[15] start run mysql --> PASS
nohup: appending output to ‘nohup.out’
wait mysql startup...
[16] wait 10s for mysql startup completly --> PASS
[17] change mysql root password --> PASS
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
[18] root@localhost initial password is 'root'
5. 新开一个窗口登陆mysql
[root@mysqltest ~]# mysql -uroot -proot
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.22 MySQL Community Server - GPL
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
(root@localhost)[(none)]>
6. 总结
mysql8.0跟mysql5.7安装的方式没有变化,变化在于参数,文章的参数是从5.7移植过来的,到底这些参数是不是最优的,还需要进一步测试。