一键安装MySQL-8.0.28

CentOS7脚本安装MySQL8

话不多说,直接上脚本

一、脚本

vim install_mysql.sh

#!/bin/bash
#定义安装软件版本
MYSQL_V=8.0.28
#定义临时目录
TMP_DIR=/tmp
#定义菜单
echo
echo -e "\tMenu\n"
echo -e "1. Install MySQL"
echo -e "2. Quit"
#定义函数
function command_status_check() {
if [ $? -ne 0 ]; then
echo $1
exit
fi
}
function install_mysql() {
cd $TMP_DIR
yum install numactl ncurses-devel libaio-devel wget -y
yum remove mariadb-libs -y
wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.28-linux-glibc2.12-x86_64.tar.xz
tar -Jxvf mysql-${MYSQL_V}-linux-glibc2.12-x86_64.tar.xz
mv mysql-${MYSQL_V}-linux-glibc2.12-x86_64 /web/mysql
grep mysql /etc/passwd
RETVAL=$?
if [ $RETVAL -ne 0 ];then
useradd mysql -s /sbin/nologin -M
echo "mysql user added successfully!"
else
echo " $(echo -e "\e[31;47;5m mysql user already exists\e[0m")"
echo " $(echo -e "\e[31;47;5m Don't worry,continue......\e[0m")"
fi
mkdir -p /web/mysql/{binlog,data,logs}
chown -R mysql:mysql /web/mysql
chown -R mysql:mysql /web/mysql/*
####定义配置文件
cat >>/etc/my.cnf<<EOF
[client]
port = 3306
[mysqld]
user =mysql
basedir =/web/mysql/
datadir =/web/mysql/data
server_id =6
port =3306
character_set_server =utf8
socket =/tmp/mysql.sock
read_only =0
skip_name_resolve =1
auto_increment_increment =1
auto_increment_offset =1
lower_case_table_names =1
open_files_limit =65536
max_connections =2048
####: for binlog
binlog_format =row
log_bin =/web/mysql/binlog/mysql-bin
binlog_rows_query_log_events =on
log_slave_updates =on
expire_logs_days =7
binlog_cache_size =65536
sync_binlog =1
slave-preserve-commit-order =ON
####: for error-log
log_error =/web/mysql/logs/error.log
general_log =off
general_log_file =/web/mysql/logs/general.log
####: for slow query log
slow_query_log =on
slow_query_log_file =/web/mysql/logs/slow.log
long_query_time =1.000000
####: for gtid
gtid_mode =on
enforce_gtid_consistency =on
EOF
#数据库初始化,初始化的密码为空
/web/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/web/mysql --datadir=/web/mysql/data
command_status_check "MySQL - 初始化失败!"
cp /web/mysql/support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on
/etc/init.d/mysqld start
command_status_check "MySQL - 启动失败!"
#添加环境变量,命令行需要手动执行source /etc/profile以使环境变量生效
echo 'export PATH=/web/mysql/bin:$PATH' >> /etc/profile
}
read -p "请输入编号:" number
case $number in
1)
install_mysql;;
2)
exit;;
esac

二、添加执行权限

chmod +x install_myql.sh

三、执行脚本测试


耐心等待脚本执行完成.......

四、安装完成结果

五、查看进程

ps -ef|grep mysql
netstat -lntp|grep 3306

六、使环境变量生效

source /etc/profile

七、测试登录

mysql -u root -p

初始密码为空,直接回车即可登录

八、修改mysql登录密码

mysqladmin -uroot -p password
第一次输入密码直接回车
第二次第三次输入自己设定的密码即可。

mysql启动
/etc/init.d/mysqld start
mysql停止
/etc/init.d/mysqld stop
mysql重启
/etc/init.d/mysqld restart
至此,MySQL-8.0.28安装完成,感谢观看。

posted @ 2022-06-10 14:38  霸都运维  阅读(574)  评论(0编辑  收藏  举报