centos7系统安装mysql8脚本(20200617)
#!/bin/bash
#centos7系统安装mysql8脚本(20200617)
# 1.清理自带数据库
systemctl stop mariadb
yum remove -y mariadb*
mv /etc/my.cnf /etc/my.cnf.old
rm -rf /var/lib/mysql*
# 2.安装可能需要依赖包
# wget http://mirrors.aliyun.com/repo/Centos-7.repo -O /etc/yum.repos.d/Centos-7.repo
# wget http://mirrors.aliyun.com/repo/epel-7.repo -O /etc/yum.repos.d/epel-7.repo
yum -y install perl-Data-Dumper perl-JSON perl-Time-HiRes libaio openssl-devel
# 3.下载捆绑全包并解压,使用升级方式安装
wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.20-1.el7.x86_64.rpm-bundle.tar
echo > /var/log/mysqld.log #先清空以前日志
chmod 666 /var/log/mysqld.log
tar -axf mysql-8.0.*.rpm-bundle.tar
rpm -Uvh mysql-community-*.rpm
\cp /etc/my.cnf /etc/my.cnf.bak
# 4.启动服务并修改初始密码
systemctl restart mysqld && systemctl status mysqld && TMPASS=`awk '/temporary password/{print $NF}' /var/log/mysqld.log` && mysqladmin --user=root --password=$TMPASS password "Tempass@1" >/dev/null #修改临时密码为Tempass@1,符合强密码四分之三原则
# 5.根据业务需要修改配置文件
cat > /etc/my.cnf << \EOF
# for mysql8.0
[mysqld_safe]
log-error=/var/log/mysqld.log
[client]
default-character-set=utf8mb4
socket = /var/lib/mysql/mysql.sock
[mysql]
default-character-set=utf8mb4
[mysqld]
bind-address=0.0.0.0
explicit_defaults_for_timestamp=true
character-set-server=utf8mb4
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
max_connections = 65535
wait_timeout = 1800
interactive_timeout = 1800
thread_cache_size = 400
max_connect_errors = 200
slow_query_log = ON
slow_query_log_file=mysql_slow.log
max_allowed_packet = 500M
default-storage-engine=INNODB
validate_password.policy=0
validate_password.length=6
default_authentication_plugin=mysql_native_password
expire_logs_days = 7
log_bin_trust_function_creators = 1
server_id=1
log-bin=host1-binlog
binlog_format="mixed"
relay_log_purge=off
lower_case_table_names=1
skip_symbolic_links=yes
local-infile=0
EOF
systemctl restart mysqld;systemctl status mysqld
echo '临时密码为"Tempass@1" ,不含双引号,请注意修改'
#CREATE USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'Tempass@2' && GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION ;