MySQL安装脚本(yum+源码)

MySQL安装

环境:Centos7.6 能访问外网

方式一:yum安装

vim yum_install_mysql.sh

#!/bin/bash
#Author:perin
#time:2020-6-17
#脚本执行方式:bash yum_install_mysql.sh 5.7      #传入的参数为想要安装版本
#脚本仅支持5.5  5.6  5.7  8.0  这几个版本的MySQL

echo "此脚本用于yum 安装mysql"
if [[ $UID -ne 0 ]];then
    echo "使用root 执行此脚本"
    exit 1
fi
if [[ $# != 1 ]];then
    echo "Usage script (5.5|5.6|5.7|8.0)"
    exit 123
fi
echo "清理环境"
systemctl stop mysqld mariadb &>/dev/null
yum erase -y `rpm -qa |grep mariadb` 2>/dev/null
yum erase -y `rpm -qa |grep mysql`  2>/dev/null
rm -rvf /etc/my.cnf /var/lib/mysql /var/log/mysql*
userdel -rf mysql &>/dev/null
ping -c1 -w1 www.baidu.com &>/dev/null
if [[ $? -eq 0 ]];then
    yum install -y wget yum-utils &>/dev/null
    if [[ $? -ne 0 ]];then
        echo "yum 配置错误"
        exit 110
    fi
    wget -O ./mysql8.0.rpm https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm
    yum install -y mysql8.0.rpm
    yum clean all
    yum makecache fast
else
    echo "网络错误"
    exit 22
fi

case $1 in
5.5)
        yum-config-manager  --disable mysql80-community
        yum-config-manager  --enable mysql55-community
        yum -y install mysql-community-server
;;
5.6)
    yum-config-manager  --disable mysql80-community
    yum-config-manager  --enable mysql56-community
    yum -y install mysql-community-server
;;
5.7)
    yum-config-manager  --disable mysql80-community
    yum-config-manager  --enable mysql57-community
    yum install -y mysql-community-server
;;
8.0)
    yum install -y mysql-community-server
;;
*)
    yum repolist all |grep mysql
    echo "其他版本可自行选择下载并配置"
    exit 13
;;
esac
systemctl start mysqld
echo "启动成功,初始密码如下(mysql5.7前版本没有初始密码)"
grep -o 'root@localhost.*' /var/log/mysqld.log
echo "安装完成"

chmod a+x yum_install_mysql.sh
bash yum_install_mysql.sh 5.7 #通过脚本安装5.7版本的MySQL

方式二:源码安装

vim by_install_mysql.sh

#!/bin/bash
#author:perin
#功能:编译安装mysql-5.7.24
#说明:该脚本仅适用于新机或未安装过数据库的机器

#安装依赖
yum -y install ncurses ncurses-devel bison libgcrypt perl make cmake
#下载源码包
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-boost-5.7.24.tar.gz
#删除mariadb
yum erase mariadb mariadb-server mariadb-libs mariadb-devel -y
sleep 1
rm -rf /etc/my* &>/dev/null
rm -rf /var/lib/mysql &>/dev/null
rm -rf /etc/init.d/mysql* &>/dev/null
userdel -r mysql &>/dev/null
groupadd mysql
useradd -M -g mysql -s /sbin/nologin mysql

#自定义目录
mkdir -p /usr/local/mysqld/{data,mysql,log,tmp}
chown -R mysql:mysql /usr/local/mysqld/*

tar xf mysql-boost-5.7.24.tar.gz
cd mysql-5.7.24
#编译安装
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysqld/mysql \
-DMYSQL_DATADIR=/usr/local/mysqld/data \
-DDOWNLOAD_BOOST=1 \
-DWITH_BOOST=/root/mysql-5.7.24/boost \
-DSYSCONFDIR=/etc \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DENABLED_LOCAL_INFILE=1 \
-DENABLE_DTRACE=0 \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EMBEDDED_SERVER=1

if [ $? -eq 0 ]; then
   make -j `lscpu | awk 'NR==4{ print $2 }'`
   if [ $? -eq 0 ];then
       make && make install
       if [ $? -eq 0 ]; then
          echo "安装编译完成。。。"
       else
          echo "make && make install 失败 。。。"
          exit 3
       fi
   else
       echo "make -j 失败 。。。"
       exit 2
   fi
else
   echo "cmake 失败 。。。"
   exit 1
fi

#提升mysql命令成为系统命令
echo "export PATH=$PATH:/usr/local/mysqld/mysql/bin" >>/etc/profile
source /etc/profile

#编辑配置文件
#cd /usr/local/mysqld/mysql/mysql-test/include
#cp -rf /etc/my.cnf /etc/my.cnf.bak
#cp -rf default_mysqld.cnf /etc/my.cnf
cat > /etc/my.cnf <

为源码安装的mysql配置systemctl管理方式

源码安装的mysql默认管理方式为

service mysql status
service mysql start
service mysql stop

配置systemctl方式
service mysql stop
vim /usr/lib/systemd/system/mysql.service

[Unit]
Description=MySQL Server
Documentation=man:mysqld
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

PIDFile=/usr/local/mysqld/tmp/mysqld.pid
# Disable service start and stop timeout logic of systemd for mysqld service.
TimeoutSec=0
# Execute pre and post scripts as root
PermissionsStartOnly=true
# Needed to create system tables
#ExecStartPre=/usr/bin/mysqld_pre_systemd
# Start main service
ExecStart=/usr/local/mysqld/mysql/bin/mysqld --daemonize --pid-file=/usr/local/mysqld/tmp/mysqld.pid
#注意这里要加上 --daemonize
# Use this to switch malloc implementation
#EnvironmentFile=-/etc/sysconfig/mysql
# Sets open_files_limit
LimitNOFILE = 5000
Restart=on-failure
RestartPreventExitStatus=1
PrivateTmp=false

systemctl daemon-reload
systemctl restart mysqld
ss -tunlp | grep 3306

修改密码为简单密码

[root@mysql ~]# vim /etc/my.cnf
[mysqld]
skip-grant-tables=1      #免密登录
[root@mysql ~]# systemctl restart mysqld
[root@mysql ~]# mysql -uroot
mysql > UPDATE mysql.user SET authentication_string=password('new_password') WHERE user='root' AND host='localhost';
mysql > flush privileges;
mysql > exit
[root@mysql ~]# vim /etc/my.cnf
[mysqld]
#skip-grant-tables=1      #解除免密登录
[root@mysql ~]# systemctl restart mysqld
[root@mysql ~]# mysql -uroot -p"12345678"

若以上方法仍无法设置简单密码,可尝试以下方案

[root@mysql ~]# vim /etc/my.cnf
[mysqld]
plugin-load=validate_password.so 
validate-password=OFF
[root@mysql ~]# systemctl restart mysqld
[root@mysql ~]# mysql -uroot -p"初始密码"
mysql > ALTER USER 'root'@'localhost' identified by "11";       #修改为简单密码
mysql > flush privileges;
mysql > exit
[root@mysql ~]# mysql -uroot -p"11"      #尝试使用简单密码是否能登录成功

你可能感兴趣的:(MySQL安装脚本(yum+源码))