Skip to main content

MySQL 5.7 单节点

服务器IP主机角色
192.168.10.2MySQL Server

MySQL Server

  1. 下载 mysql 安装包

    wget http://pdpublic.mingdao.com/private-deployment/offline/common/mysql-5.7.42-linux-glibc2.12-x86_64.tar.gz
  2. 解压至安装目录

    tar -zxvf mysql-5.7.42-linux-glibc2.12-x86_64.tar.gz
    mv mysql-5.7.42-linux-glibc2.12-x86_64 /usr/local/mysql
  3. 创建 mysql 用户

    useradd -U -M -s /sbin/nologin mysql
  4. 创建数据、日志目录并授予权限

    mkdir -p /data/mysql/ /data/logs/mysql
    chown -R mysql:mysql /usr/local/mysql/ /data/mysql/ /data/logs
  5. 配置 systemd 管理文件

    cat > /etc/systemd/system/mysql.service <<EOF
    [Unit]
    Description=MySQL Server
    Documentation=man:mysqld(8)
    Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
    After=network.target
    After=syslog.target
    [Service]
    User=mysql
    Group=mysql
    Type=forking
    PIDFile=/usr/local/mysql/mysqld.pid
    TimeoutSec=0
    PermissionsStartOnly=true
    ExecStart=/usr/local/mysql/bin/mysqld --daemonize --log-error=/data/logs/mysql/mysqld.log --datadir=/data/mysql --socket=/usr/local/mysql/mysql.sock --character-set-server=utf8 --pid-file=/usr/local/mysql/mysqld.pid --server-id=1 --log-bin=mysql-bin --max_connections=2000 --slow_query_log=1 --slow_query_log_file=/data/logs/mysql/mysql-slow.log
    LimitNOFILE=102400
    Restart=on-failure
    RestartPreventExitStatus=1
    PrivateTmp=false
    [Install]
    WantedBy=multi-user.target
    EOF
  6. 初始化

    /usr/local/mysql/bin/mysqld --initialize --datadir=/data/mysql/ --user=mysql --log-error=/data/logs/mysql/mysqld.log
  7. 启动 mysql 并加入开机自启动

    systemctl daemon-reload
    systemctl enable mysql
    systemctl start mysql
  8. 修改 mysql 密码

    /usr/local/mysql/bin/mysql -h127.0.0.1 -uroot -p$(grep 'temporary password' /data/logs/mysql/mysqld.log  | awk '{print $NF}')
    set PASSWORD = PASSWORD('123456');
    GRANT ALL ON *.* to root@'%' IDENTIFIED BY '123456';
    FLUSH PRIVILEGES;
    quit;
    • 命令中修改的 root 用户密码为 123456,实际部署时注意替换