美文网首页
MySQL 5.7 linux generic 版本安装

MySQL 5.7 linux generic 版本安装

作者: awker | 来源:发表于2019-10-08 00:03 被阅读0次

    1、下载 mysql
    打开 https://dev.mysql.com/downloads/mysql/ 链接,点击 "Looking for previous GA versions?"


    选择 "Linux - Generic" , x86-64 bit 的版本

    就可以下载 mysql 了

    官网下载链接地址:https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.27-linux-glibc2.12-x86_64.tar.gz
    也可以用国内源的链接地址,比如清华大学开源软件镜像站的,速度会快些:https://mirror.tuna.tsinghua.edu.cn/mysql/downloads/MySQL-5.7/mysql-5.7.27-linux-glibc2.12-x86_64.tar.gz

    2、安装配置 mysql
    把下载的 mysql 二进制包上传到 linux服务器上,或者直接在服务上下载 mysql 二进制包

    # cd /usr/local/src/
    # wget https://mirror.tuna.tsinghua.edu.cn/mysql/downloads/MySQL-5.7/mysql-5.7.27-linux-glibc2.12-x86_64.tar.gz
    # tar xvf mysql-5.7.27-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
    # ln -s /usr/local/mysql-5.7.27-linux-glibc2.12-x86_64/ /usr/local/mysql
    
    # cd /usr/local/mysql
    // 创建 mysql 用户
    # groupadd mysql
    # useradd -r -g mysql -s /bin/false mysql
    # mkdir mysql-files
    # chown mysql:mysql mysql-files/
    # chmod 750 mysql-files/
    
    // 先创建 /opt/mysql/data 目录和分配 mysql 权限
    # mkdir -p /opt/mysql/data
    # chown mysql:mysql /opt/mysql/data/
    # chmod 750 /opt/mysql/data/
    
    // 创建 mysql error.log、mysql.pid、mysql.sock存放目录
    # mkdir -p /var/log/mysql
    # chown mysql:mysql /var/log/mysql/
    # chmod 750 /var/log/mysql/
    
    // 注意终端输出的 root@localhost 的初始密码为 -ymdPl>-q0+Q
    # ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/opt/mysql/data/
    2019-10-07T14:46:19.092276Z 0 [Warning] InnoDB: New log files created, LSN=45790
    2019-10-07T14:46:19.113051Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
    2019-10-07T14:46:19.170896Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 3902c202-e911-11e9-bd58-0050568209d2.
    2019-10-07T14:46:19.171605Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
    2019-10-07T14:46:19.173398Z 1 [Note] A temporary password is generated for root@localhost: -ymdPl>-q0+Q
    
    
    // 修改 /etc/my.cnf 中的参数 datadir、socket、log-error、pid-file
    # vim /etc/my.cnf
    [mysqld]
    datadir=/opt/mysql/data
    pid-file=/var/log/mysql/mysql.pid
    
    [mysqld_safe]
    log-error=/var/log/mysql/error.log
    socket=/var/log/mysql/mysql.sock
    
    # cp support-files/mysql.server /etc/init.d/mysql.server
    // 修改 /etc/init.d/mysql.server 中的数据目录参数 datadir 的值为 /opt/mysql/data,基础目录 basedir 参数的值为 /usr/local/mysql
    # vim /etc/init.d/mysql.server
    basedir=/usr/local/mysql
    datadir=/opt/mysql/data
    
    // 启动 mysql
    # /etc/init.d/mysql.server start
    Starting MySQL.Logging to '/var/log/mysql/error.log'.
    . SUCCESS! 
    
    # netstat -nltup|grep 3306
    tcp6       0      0 :::3306                 :::*                    LISTEN      32201/mysqld  
    
    // 登录 mysql,输入上面的初始密码 -ymdPl>-q0+Q
    # mysql -uroot -p
    Enter password: 
    MySQL [(none)]> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    | sys                |
    +--------------------+
    4 rows in set (0.00 sec)
    
    MySQL [(none)]> exit
    Bye
    
    
    // 如果提示密码过期
    ERROR 1862 (HY000): Your password has expired. To log in you must change it using a client that supports expired passwords.
    // 按如下方式重置 mysql 密码
    // 先关闭 mysql
    # /etc/init.d/mysql.server stop
    
    // 再启动无需密码认证的 mysql
    # ./bin/mysqld_safe --skip-grant-tables &
    
    // 直接登录 mysql,无需密码
    # mysql
    
    // 把 root@localhost 的密码设为不过期
    MySQL [(none)]> update mysql.user set password_expired='N' where user='root';
    MySQL [(none)]> flush privileges;
    MySQL [(none)]> exit
    
    // 关闭 "./bin/mysqld_safe --skip-grant-tables &" 这种方式启动的 mysql,此处需要输入上面的初始密码 -ymdPl>-q0+Q
    # mysqladmin -u root -p shutdown --socket=/var/log/mysql/mysql.sock
    
    // 再正常启动 mysql
    # /etc/init.d/mysql.server start
    
    // 输入上面的初始密码 -ymdPl>-q0+Q 登录就行
    # mysql -uroot -p
    Enter password: 
    
    // 更改 root 密码的方法,比如更改 root 密码 为 123456
    # mysql -uroot -p
    MySQL [(none)]> grant all on *.* to 'root'@'localhost' identified by '123456';
    MySQL [(none)]> flush privileges;
    

    参考:

    1. /etc/my.cnf 其他参数可根据实际情况调整,可参考此链接:https://github.com/ackfin/mysql_config/blob/master/my.cnf
    2. mysql 官方安装文档:https://dev.mysql.com/doc/refman/5.7/en/binary-installation.html

    相关文章

      网友评论

          本文标题:MySQL 5.7 linux generic 版本安装

          本文链接:https://www.haomeiwen.com/subject/tievpctx.html