乐趣区

关于shell:Centos7一键编译搭建Mysql脚本

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;
        echo "下载实现, 正在解压.......";
        tar -zxvf mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz
        mv mysql-5.7.37-linux-glibc2.12-x86_64 /usr/local/mysql
        cd /usr/local/mysql
        #exit 0
    fi

    echo "创立用户组"
    userdel mysql;
    groupadd mysql;
    useradd -r -g mysql mysql;
    mkdir -p  /data/mysql;
    chown mysql:mysql -R /data/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/mysql                     #Mysql 数据寄存的绝对路径
socket=/tmp/mysql.sock                  #套接字文件
log-error=/data/mysql/mysql.err         #mysql 生成的谬误日志寄存的门路
pid-file=/data/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/mysql/ --user=mysql --initialize
sleep 2s

ehco "启动 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/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'root@.com';"
$MYSQL_BASE/bin/mysql --socket=/tmp/mysql.sock -uroot -proot@.com -e "FLUSH PRIVILEGES;"
$MYSQL_BASE/bin/mysql --socket=/tmp/mysql.sock -uroot -proot@.com -e "USE mysql;"
$MYSQL_BASE/bin/mysql --socket=/tmp/mysql.sock -uroot -proot@.com -e "UPDATE user SET host ='%'WHERE user ='root';"
$MYSQL_BASE/bin/mysql --socket=/tmp/mysql.sock -uroot -proot@.com -e "FLUSH PRIVILEGES;"
$MYSQL_BASE/bin/mysql --socket=/tmp/mysql.sock -uroot -proot@.com -e "exit;"

echo "重启数据库"
service mysql restart;
service mysql status;

echo "以服务项启动"
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 "数据库明码 : root@.com"
echo "数据库端口 : 3306"
echo "BASEDIR 目录: /usr/local/mysql"
echo "DATADIR 目录: /data/mysql"
}
install_mysql
退出移动版