Note when going straight for success of Mr.Wang

0%

Linux命令安装Mysql

Linux(Centos7.6)安装mysql-5.7.25

  1. 下载 https://downloads.mysql.com/archives/community/
  2. 检测是否已经安装mysql

    1
    2
    3
    rpm -qa | grep mysql #如有类似 mysql-libs-5.1.52-1.el6_0.1.x86_64 进行卸载
    rpm -e mysql-libs-5.1.52-1.el6_0.1.x86_64  #普通删除模式
    rpm -e --nodeps mysql-libs-5.1.52-1.el6_0.1.x86_64  #强力删除模式,如果使用上面命令删除时,提示有依赖的其它文件,则用该命令可以对其进行强力删除
  3. 检测系统是否有mariadb,若有,同上面步骤卸载

    1
    rpm -qa | grep mariadb
  4. 上传文件到服务器 /opt/mysql

  5. 解压文件

    1
    tar -zxvf mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz
  6. 重命名文件

    1
    mv mysql-5.7.25-linux-glibc2.12-x86_64 mysql-5.7.25
  7. 创建mysql用户组和mysql用户(先检查是否存在,若不存在再添加)

    1
    2
    3
    4
    cat /etc/group | grep mysql
    cat /etc/passwd | grep mysql
    groupadd mysql
    useradd -r -g mysql mysql #useradd -r 参数表示mysql用户是系统用户,不可用于登录系统
  8. 创建data目录

    1
    cd mysql-5.7.25 & mkdir data
  9. 将/opt/mysql/mysql-5.7.25的所有者及所属组改为mysql

    1
    chown -R mysql:mysql /opt/mysql/mysql-5.7.25
  10. 在/opt/mysql/mysql-5.7.25/support-files目录下创建my_default.cnf

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    [mysqld]
    sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
    basedir = /opt/mysql/mysql-5.7.25
    datadir = /opt/mysql/mysql-5.7.25/data
    port = 3306
    socket = /tmp/mysql.sock
    character-set-server=utf8
    log-error = /opt/mysql/mysql-5.7.25/data/mysqld.log
    pid-file = /opt/mysql/mysql-5.7.25/data/mysqld.pid
    复制文件到/etc目录下
    cp support-files/my_default.cnf /etc/my.cnf
  11. 初始化mysqld

    1
    2
    3
    cd /opt/mysql/mysql-5.7.25
    ./bin/mysqld --initialize --user=mysql --basedir=/opt/mysql/mysql-5.7.25/ --datadir=/opt/mysql/mysql-5.7.25/data/
    /opt/mysql/mysql-5.7.25/data/mysqld.log #查看日志,获取初始化密码 example : 2019-04-08T06:14:29.790033Z 1 [Note] A temporary password is generated for root@localhost: a8?DQir=T+k+
  12. 把启动脚本放到开机初始化目录

    1
    cp support-files/mysql.server /etc/init.d/mysql
  13. 启动

    1
    service mysql start
  14. 创建软连接使得在任意目录都可以执行mysql命令, 类似于windows桌面快捷方式

    1
    ln -s /opt/mysql/mysql5.7.25/bin/mysql /usr/bin
  15. 登录mysql,密码为初始密码

    1
    mysql -u root -p #Enter password: a8?DQir=T+k+
  16. 修改密码:root

    1
    2
    3
    4
    mysql> set password=password('root');
    // 添加数据库访问权限(all)
    mysql> grant all privileges on *.* to root@'%' identified by 'root';
    mysql> flush privileges;
  17. 退出mysql操作界面

    1
    quit
  18. 重启mysql生效

    1
    service mysql restart
-------------the end-------------