zoukankan      html  css  js  c++  java
  • Shell 脚本之 MySQL 一键安装及基本配置(几分钟搞定)

    准备工作

    MySQL 该版本下载地址:

    https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz

    image-20210716111126345

    mysql_install-v1.0.sh脚本如下:

    #!/bin/bash
    
    # MySQL 安装日志信息
    log=/var/log/mysql_install.log
    # MySQL压缩包路径
    MySQLPath=/root/mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz
    # sql 文件
    sqlfile=/root/test.sql
    
    # 卸载系统⾃带的MARIADB
    rpm -qa|grep mariadb |xargs yum remove -y > /dev/null
    
    # 解压MYSQL安装包
    echo '===================================='
    echo 'Unzip....'
    tar -zxf $MySQLPath -C /usr/local/
    mv /usr/local/mysql-5.7.30-linux-glibc2.12-x86_64 /usr/local/mysql
    if [ $? -ne 0 ];then
    echo 'MySQL unzip failed!!!'
    exit
    else
    echo '===================================='
    echo 'Unpack the success!'
    fi
    
    # 创建MYSQL⽤户和⽤户组
    groupadd mysql
    useradd -g mysql mysql
    mkdir /usr/local/mysql/data
    chown -R mysql:mysql /usr/local/mysql
    echo '===================================='
    echo 'User created successfully!'
    
    # MYSQL的配置⽂件
    cat > /etc/my.cnf << EOF
    [mysql]
    default-character-set=utf8
    socket=/var/lib/mysql/mysql.sock
    [mysqld]
    skip-name-resolve
    port = 3306
    socket=/var/lib/mysql/mysql.sock
    basedir=/usr/local/mysql
    datadir=/usr/local/mysql/data
    max_connections=200
    character-set-server=utf8
    default-storage-engine=INNODB
    lower_case_table_names=1
    max_allowed_packet=16M
    EOF
    if [ $? -ne 0 ];then
    echo 'Failed to write the my.cnf configuration file!!!'
    exit
    else
    echo '===================================='
    echo 'my.cnf configuration success!'
    echo '===================================='
    fi
    
    # 创建 /var/lib/mysql ⽬录,并修改权限
    mkdir /var/lib/mysql
    chmod 777 /var/lib/mysql
    
    # 开始安装 MYSQL
    cd /usr/local/mysql
    ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data 2>&1 | tee $log
    
    # 获取 root 初始密码
    InitialPassword=`tail -1 $log |awk '{print $NF}'`
    
    # 复制启动脚本到资源⽬录,并修改 basedir 和 datadir 为对应⽬录
    cp ./support-files/mysql.server /etc/init.d/mysqld
    sed -i '46s/basedir=/basedir=/usr/local/mysql/g' /etc/init.d/mysqld
    sed -i '47s/datadir=/datadir=/usr/local/mysql/data/g' /etc/init.d/mysqld
    if [ $? -ne 0 ];then
    echo 'Failed to replace /etc/init.d/mysqld!!!'
    exit
    else
    echo '===================================='
    echo '/etc/init.d/mysqld successfully modified!'
    echo '===================================='
    fi
    
    # 设置MYSQL系统服务并开启⾃启
    chmod +x /etc/init.d/mysqld
    chkconfig --add mysqld
    
    # 启动MYSQLD
    service mysqld restart
    
    # 检查 MySQL 服务是否启动成功
    netstat -ntlp | grep 3306
    if [ $? -ne 0 ];then
    echo 'MySQL Boot failure!!!'
    exit
    else
    echo '===================================='
    echo 'start MySQL successfully!'
    fi
    
    # 将 MYSQL 的 BIN ⽬录加⼊ PATH 环境变量
    echo "export PATH=$PATH:/usr/local/mysql/bin" >> ~/.bash_profile
    source ~/.bash_profile
    
    if [ $? -ne 0 ];then
    echo 'Variable write failed!!!'
    exit
    else
    echo '===================================='
    echo 'Variable write successful!'
    echo '===================================='
    fi
    
    # 登陆MYSQL,并执行 SQL 语句
    mysql -uroot -p"$InitialPassword" --connect-expired-password < $sqlfile 2> /dev/null
    
    if [ $? -ne 0 ];then
    echo 'fail to perform MySQL operation!!!'
    exit
    else
    echo '===================================='
    echo 'All operations were successful and can be connected through Navicat!!!'
    echo '===================================='
    fi
    

    test.sql文件内容如下:

    alter user user() identified by "123456";
    use mysql;
    update user set user.Host='%' where user.User='root';
    flush privileges;
    

    第一、修改 root 密码;

    第二、设置远程主机登录,可以通过Navicat来登录。

    执行脚本

    这里需要注意的是不要使用 ./mysql_install-v1.0.sh方式来执行,否则系统环境变量执行完脚本之后不生效;

    需要使用source mysql_install-v1.0.sh来执行脚本才可以。

    image-20210716112006040

    使用修改后的密码登录测试

    image-20210716112105269

    Navicat 连接测试

    Snipaste_2021-07-16_11-07-26

    后续还会继续更新其他应用一键安装脚本,欢迎关注我哦!!!


    作者:神奇二进制
    文章出处:https://www.cnblogs.com/l-hh/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。
    文章如有叙述不当的地方,欢迎指正。如果觉得文章对你有帮助,可以精神上的支持 [推荐] 或者 [关注我] ,一起交流,共同进步!

  • 相关阅读:
    bootstrap不同屏幕区分数值
    jq星星评分
    大話西遊
    HDU 5353 Average 贪心
    HDU 5358 First One 数学+尺取法
    生活感受
    HDU 4372 Count the Buildings 组合数学
    暑假集训-合训第九场
    一些资料
    多校-HDU 5351 MZL's Border 数学规律
  • 原文地址:https://www.cnblogs.com/l-hh/p/15019316.html
Copyright © 2011-2022 走看看