Linux安装MySQL_全网最详细

Linux安装MySQL

编辑于2021-02-19

本文中MySQL安装目录为/usr/local/mysql

  1. 上传MySQL安装包至/usr/local目录,执行命令解压

    [root@JetXi local]# tar zxf mysql-5.7.28-el7-x86_64.tar.gz
  2. 对该目录进行改名,便于配置

    [root@JetXi local]# mv mysql-5.7.28-el7-x86_64 mysql
  3. 创建data目录和log目录,并创建log文件

    [root@JetXi local]# mkdir mysql/data
    [root@JetXi local]# mkdir mysql/log
    [root@JetXi local]# touch mysql/log/mysql.log
  4. 创建mysql组和mysql用户

    [root@JetXi local]# groupadd mysql
    [root@JetXi local]# useradd -g mysql mysql
  5. 将MySQL安装目录所属的用户和组改为MySQL

    [root@JetXi local]# chown -R mysql:mysql mysql
  6. 将MySQL安装目录的权限修改为755,即文件拥有者对其可读可写可执行,其他同组用户和组外用户对其可读可执行

    [root@JetXi local]# chmod 755 mysql
  7. 拷贝mysql.server文件

    [root@JetXi local]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
  8. 配置该文件

    [root@JetXi local]# vi /etc/init.d/mysql
       basedir=/usr/local/mysql
       datadir=/usr/local/mysql/data
  9. 配置/etc/my.cnf文件

    [root@JetXi local]# vi /etc/my.cnf
      [mysql]
       default-character-set=utf-8
       socket=/usr/local/mysql/mysql.sock
    ?
      [mysqld]
       basedir=/usr/local/mysql
       datadir=/usr/local/mysql/data
       socket=/usr/local/mysql/mysql.sock
       port=3306
       default-storage-engine=INNODB
       # Disabling symbolic-links is recommended to prevent assorted security risks
       symbolic-links=0
       # Settings user and group are ignored when systemd is used.
       # If you need to run mysqld under a different user or group,
       # customize your systemd unit file for mariadb according to the
       # instructions in http://fedoraproject.org/wiki/Systemd
    ?
      [mysqld_safe]
       log-error=/usr/local/mysql/log/mysql.log
       pid-file=/usr/local/mysql/mysql.pid
  10. 将MySQL添加到环境变量

    [root@JetXi local]# vi /etc/profile
    export PATH=$PATH:/usr/local/mysql/bin
    [root@JetXi local]# source /etc/profile
  11. 初始化MySQL,初始化成功输出的信息中,最后一行是MySQL的临时密码

    mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
    ...
    2021-02-19T20:41:02.054551Z 1 [Note] A temporary password is generated for           root@localhost: BUcyY36C3L;<
  12. 启动MySQL服务

    [root@JetXi local]# service mysql start
  13. 使用初始化生成的密码登录MySQL,登录成功说明配置成功

    [root@JetXi ~]# mysql -uroot -p
    Enter password:
    Welcome to the MySQL monitor. Commands end with ; or \g.
    Your MySQL connection id is 2
    Server version: 5.7.28
    ?
    Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
    ?
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    ?
    Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.
    ?
    mysql>
  14. 修改MySQL密码

    mysql> set password=password("JetXi");
    Query OK, 0 rows affected, 1 warning (0.00 sec)
  15. 若登录时提示密码过期,则编辑my.cnf文件,配置免密码登录

    [root@JetXi local]# vi /etc/my.cnf
    skip-grant-tables

    重启MySQL服务

    [root@JetXi local]# service mysql restart

    登录MySQL,直接回车进入

    [root@JetXi local]# mysql -uroot -p

    更新相关字段,解除密码过期限制

    mysql> update mysql.user set password_expired=‘N‘

    退出MySQL,注释掉刚才my.cnf中的跳过验证代码,保存退出,重启MySQL服务

    [root@JetXi local]# service mysql restart

    重复13、14步骤

Linux安装MySQL_全网最详细

上一篇:配置Django实现数据库读写分离


下一篇:How to see log files in MySQL?