【快捷部署】005_MySQL(5.6)
【摘要】 快捷部署系列,CentOS 7下,通过脚本,快速完成MySQL的安装,并修改root默认本地密码。
保存下面脚本到文件:install-Mysql5.6.sh
#!/bin/bash
#################################################################################
# 作者:cxy-2023-02-21 #
# 一键自动安装 mysql 5.6
# #
#################################################################################
#注意:本脚本中,有删除mariadb的操作!!
#mysql安装包目录
install_dir=/opt/mysql_install
#mysql tar包下载地址(如此地址失效,可自行更换)
mysql_tar=https://hciecloud.obs.cn-north-4.myhuaweicloud.com/MySQL-5.6.45-1.el6.x86_64.rpm-bundle.tar
#mysql初始化本地密码
mysql_pw=123qwE
#创建目录
mkdir -p ${install_dir}
cd ${install_dir}
#下载tar,并解压
wget https://hciecloud.obs.cn-north-4.myhuaweicloud.com/MySQL-5.6.45-1.el6.x86_64.rpm-bundle.tar
tar -xvf MySQL-5.6.45-1.el6.x86_64.rpm-bundle.tar
#删除mariadb,并安装mysql
yum -y remove mariadb*
yum install -y MySQL-shared-compat-5.6.45-1.el6.x86_64.rpm
yum install -y MySQL-server-5.6.45-1.el6.x86_64.rpm
yum install -y MySQL-client-5.6.45-1.el6.x86_64.rpm
yum install -y MySQL-devel-5.6.45-1.el6.x86_64.rpm
yum install -y MySQL-shared-5.6.45-1.el6.x86_64.rpm
#配置文件
cat << EOF >> /etc/my.cnf
[mysqld]
join_buffer_size = 128M
sort_buffer_size = 2M
read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
lower_case_table_names = 1
innodb_strict_mode = 1
sql_mode =
symbolic-links=0
character_set_server = utf8
log-bin = mysql-bin
binlog_format=row
server-id = 2
expire_logs_days = 10
slave_skip_errors = 1062
innodb_strict_mode = 0
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
EOF
#启动,并验证安装效果
systemctl start mysql
systemctl status mysql
echo "MySQL安装完成!"
#获取安装成功后的初始随机密码
chushi_pw=`awk -F"[ :]+" 'NR==1{print $NF}' /root/.mysql_secret`
#创建密码修改脚本
cat << EOF >> mysql_cpw.sh
#!/bin/bash
# 安装 expect 插件
yum install -y expect
# 修改密码
/bin/expect << EOF
spawn /usr/bin/mysql -h127.0.0.1 -uroot -p`echo -e ${chushi_pw}`
expect "mysql>"
send "SET PASSWORD FOR root@localhost=PASSWORD('${mysql_pw}');"
send "\n"
expect "mysql>"
send 'flush privileges\n'
expect "mysql>"
send 'quit\n'
interact
EOF
chmod +x mysql_cpw.sh
./mysql_cpw.sh
echo "MySQL密码修改成功!密码为:${mysql_pw}"
echo "赶紧执行:mysql -u root -p 试一下吧 ~"
#授权并执行
chmod +x install-Mysql5.6.sh
./install-Mysql5.6.sh
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)