#!/bin/bash
MYSQL_V=5.7.37
TMP_DIR=/tmp
INSTALL_DIR=/usr/local
function install_mysql(){
MYSQL_BASE=/usr/local/mysql
cd $TMP_DIR
file="mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz"
if [ ! -f $file ]; then
echo "File not found!"
yum install -y wget && wget -c wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz;
#exit 0
fi
echo "下载完成,正在解压.......";
tar -zxvf /tmp/mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz
mv /tmp/mysql-5.7.37-linux-glibc2.12-x86_64 /usr/local/mysql
cd /usr/local/mysql
echo "创建用户组"
userdel mysql;
groupadd mysql;
useradd -r -g mysql mysql;
mkdir -p /data/modules/mysql;
chown mysql:mysql -R /data/modules/mysql;
cd /etc
echo "写入配置文件"
cat > my.cnf <<EOF
[mysqld]
bind-address=0.0.0.0 #绑定地址运行远程连接
port=3306 #Mysql开放的端口
user=mysql #数据库登录用户
basedir=/usr/local/mysql #Mysql安装的绝对路径
datadir=/data/modules/mysql #Mysql数据存放的绝对路径
socket=/tmp/mysql.sock #套接字文件
log-error=/data/modules/mysql/mysql.err #mysql生成的错误日志存放的路径
pid-file=/data/modules/mysql/mysql.pid #为mysqld程序指定一个存放进程ID的文件
character_set_server=utf8mb4 #数据库字符编码
symbolic-links=0 #是否开启链接符号
explicit_defaults_for_timestamp=true #数据库timestamp类型的列自动更新
EOF
echo "初始化Mysql"
cd /usr/local/mysql/bin/
./mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql/ --datadir=/data/modules/mysql/ --user=mysql --initialize
sleep 2s
echo "启动mysql"
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
service mysql start
service mysql status
ln -s /usr/local/mysql/bin/mysql /usr/bin
echo "获取mysql初始密码"
PASSWORD=`cat /data/modules/mysql/mysql.err |grep "temporary password"|awk -F"root@localhost: " '{print $2}'`
echo "修改mysql密码"
$MYSQL_BASE/bin/mysql --socket=/tmp/mysql.sock --connect-expired-password -uroot -p${PASSWORD} -e "ALTER USER 'root'@'localhost' IDENTIFIED BY '[email protected]';"
$MYSQL_BASE/bin/mysql --socket=/tmp/mysql.sock -uroot [email protected] -e "FLUSH PRIVILEGES;"
$MYSQL_BASE/bin/mysql --socket=/tmp/mysql.sock -uroot [email protected] -e "USE mysql;"
$MYSQL_BASE/bin/mysql --socket=/tmp/mysql.sock -uroot [email protected] -e "UPDATE user SET host = '%' WHERE user = 'root';"
$MYSQL_BASE/bin/mysql --socket=/tmp/mysql.sock -uroot [email protected] -e "FLUSH PRIVILEGES;"
$MYSQL_BASE/bin/mysql --socket=/tmp/mysql.sock -uroot [email protected] -e "exit;"
echo "重启数据库"
service mysql restart;
service mysql status;
service mysql stop;
sleep 2s;
echo "以服务项启动"
cd /usr/lib/systemd/system
cat > mysq.service << EOF
[Unit]
Description=Mysql
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/data/modules/mysql/mysql.pid
ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=false
[Install]
WantedBy=multi-user.target
EOF
systemctl start mysql
systemctl enable mysql
systemctl status mysql
#cp /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld;
#chmod +x /etc/init.d/mysqld;
#chkconfig --add mysqld;
#chkconfig --list;
firewall-cmd --zone=public --add-port=3306/tcp --permanent;
firewall-cmd --reload;
firewall-cmd --list-all;
echo "==========> MYSQL信息 <========== "
echo " 数据库版本 : 5.7.37 "
echo " 数据库密码 : [email protected] "
echo " 数据库端口 : 3306 "
echo " BASEDIR目录: /usr/local/mysql "
echo " DATADIR目录: /data/modules/mysql "
}
install_mysql