说明:
安装8.0.34版本mysql ,脚本执行完成后会自动启动mysql,修改数据库root密码为Lz!2023@aD
使用方法:
./mysql_install.sh install # 安装数据库
./mysql_install.sh uninstall # 卸载数据库
安装包获取地址:链接:https://pan.baidu.com/s/1qpuFXaCTo1NQ44Hq_Vd_vg?pwd=9948
#!/bin/bash
install_mysql() {
MYSQL_VERSION=8.0.34
tar -xf ./mysql-8.0.34-linux-glibc2.12-x86_64.tar.xz -C /home/
mv /home/mysql-8.0.34-linux-glibc2.12-x86_64 /home/mysql
cat >/etc/my13306.cnf <<EOF
[mysqld]
basedir = /home/mysql
datadir = /home/mysql/data
log-error=/home/mysql/data/error.log
port = 13306
socket = /tmp/mysql.sock
character-set-server=utf8mb4
collation-server=utf8mb4_general_ci
max_allowed_packet=256M
sql_mode="STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION"
# 解决日志文件中的时区问题
log_timestamps = SYSTEM
slow_query_log=ON
long_query_time=3
slow_query_log_file=/home/mysql/data/slowquery.log
log_bin=/home/mysql/data/mysql_bin
server_id=1
binlog_format=row
#binlog-do-db=auth_service_java,evaluate,file_manage_v2,fileman_hub
expire-logs-days=7
max_connections=512
[client]
socket = /tmp/mysql.sock
[mysql]
default-character-set=utf8mb4
EOF
# 拷贝mysql启动文件
cp /home/mysql/support-files/mysql.server /etc/init.d/mysql.server
# 添加MySQL环境变量
echo "export PATH=/home/mysql/bin:\$PATH" >>/etc/profile
source /etc/profile
useradd mysql
mkdir /home/mysql/data
chown -R mysql.mysql /home/mysql/data
chmod 750 /home/mysql/data
# 初始化MySQL数据库
cd /home/mysql
./bin/mysqld --initialize --user=mysql --datadir=/home/mysql/data --basedir=/home/mysql --log-error=/home/mysql/data/error.log
# 设置MySQL root用户密码
password=$(grep "temporary password" /home/mysql/data/error.log | awk '{print $NF}')
# 启动MySQL服务
# /etc/init.d/mysql.server start
# 设置MySQL开机自启
# chkconfig --add mysql.server
# chkconfig mysql.server on
cat >/etc/systemd/system/mysqld13306.service <<EOF
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/home/mysql/bin/mysqld --defaults-file=/etc/my13306.cnf
LimitNOFILE = 5000
EOF
systemctl daemon-reload
# 启动MySQL服务
systemctl start mysqld13306
systemctl enable mysqld13306
sleep 10
echo "初始密码:$password"
newpass="Lz!2023@aD"
mysqladmin -uroot -p${password} -P 13306 -h 127.0.0.1 password $newpass
if [ $? -eq 0 ]; then
echo "密码已重置"
echo "MySQL $MYSQL_VERSION has been installed successfully!"
mysql -uroot -p${newpass} -P13306 -h 127.0.0.1 -e "CREATE USER 'root'@'%' IDENTIFIED BY '$newpass';"
mysql -uroot -p${newpass} -P13306 -h 127.0.0.1 -e "grant all privileges on *.* to root@'%'"
mysql -uroot -p${newpass} -P13306 -h 127.0.0.1 -e "alter user root@'%' identified with mysql_native_password by '$newpass'"
mysql -uroot -p${newpass} -P13306 -h 127.0.0.1 -e "alter user root@'localhost' identified by '$newpass' password expire never"
else
echo "重置密码失败,请手动重置,初始密码为:$password"
fi
}
uninstall_mysql() {
# /etc/init.d/mysql.server stop
systemctl stop mysqld13306
if [ $? -eq 0 ]; then
echo "mysql已停止"
else
echo "mysql未正常停止,请检查服务"
exit 1
fi
systemctl stop mysqld13306
systemctl disable mysqld13306
echo "清理数据中..."
rm -rf /home/mysql
echo "Mysql卸载完成"
}
mysql_param=$1
case $mysql_param in
install | INSTALL)
install_mysql
;;
uninstall | UNINSTALL)
uninstall_mysql
;;
*)
echo "usage: mysql_install.sh install|INSTALL|unistall|UNINSTALL."
;;
esac
标签:脚本,etc,mysql8,卸载,34,--,mysql,home,data
From: https://www.cnblogs.com/liangyongwang/p/17703127.html