# 一、准备工作

  1. 下载安装包 下载地址

  2. 查看系统⾃带的 MARIADB

    rpm -qa|grep mariadb
    
  3. 卸载系统⾃带的 MARIADB(如果有),remove后面加 rpm -qa|grep mariadb 查询出来的路径

    yum -y remove
    

# 二、解压

  1. mysql-8.0.26-linux-glibc2.12-x86_64.tar.xz 安装包上传到 /usr/local/ 目录下

  2. 安装包解压,并重命名为mysql

    xz -d mysql-8.0.26-linux-glibc2.12-x86_64.tar.xz
    tar -xvf mysql-8.0.26-linux-glibc2.12-x86_64.tar 
    mv mysql-8.0.26-linux-glibc2.12-x86_64 mysql
    
  3. 创建MYSQL⽤户和⽤户组

    groupadd mysql
    useradd -g mysql mysql
    
  4. 修改MYSQL⽬录的归属⽤户

    chown -R mysql:mysql ./
    
  5. 准备MYSQL的配置⽂件,编辑 vim /etc/my.cnf

    [mysql]
    # 设置mysql客户端默认字符集
    default-character-set=utf8
    socket=/var/lib/mysql/mysql.sock
    [mysqld]
    skip-name-resolve
    #设置3306端⼝
    port = 3306
    socket=/var/lib/mysql/mysql.sock
    # 设置mysql的安装⽬录
    basedir=/usr/local/mysql
    # 设置mysql数据库的数据的存放⽬录
    datadir=/usr/local/mysql/data
    # 允许最⼤连接数
    max_connections=200
    # 服务端使⽤的字符集默认为8⽐特编码的latin1字符集
    character-set-server=utf8
    # 创建新表时将使⽤的默认存储引擎
    default-storage-engine=INNODB
    lower_case_table_names=1
    max_allowed_packet=16M
    # binglog配置
    server-id = 1
    log-bin = mysql-bin
    log_bin_index = binlog.index
    binlog_format = ROW
    # binlog过期清理时间;
    expire_logs_days= 7
    # binlog每个日志文件大小;
    max_binlog_size = 1024m
    # binlog缓存大小;
    binlog_cache_size = 128m
    # 最大binlog缓存大小。
    max_binlog_cache_size = 1024m
    

    并且修改权限

    mkdir /var/lib/mysql 
    chmod 777 /var/lib/mysql
    

# 三、 安装MySQL

  1. 进入cd /usr/local/mysql 目录 进行初始化

    ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
    
  2. 执行初始化后,控制台会返回临时密码, 记录临时密码 ,后面会用到
    例如:像我的例子在最后一行 A temporary password is generated for root@localhost:kvct:yIy8_qk
    我的临时密码就是这个: kvct:yIy8_qk

    例子:
    2021-09-14T01:30:19.227943Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.26) initializing of server in progress as process 1808
    2021-09-14T01:30:19.250431Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
    2021-09-14T01:30:20.184005Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
    2021-09-14T01:30:21.140583Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1 is enabled for channel mysql_main
    2021-09-14T01:30:21.141148Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1.1 is enabled for channel mysql_main
    2021-09-14T01:30:21.341903Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: kvct:yIy8_qk
    
  3. 复制启动脚本到资源目录

    cp ./support-files/mysql.server /etc/init.d/mysqld
    
  4. 编辑vim /etc/init.d/mysqld ,修改 basedirdatadir ,为其实际对应的目录

    basedir=/usr/local/mysql 
    datadir=/usr/local/mysql/data
    

# 四、设置MYSQL服务开机自启

  1. 增加 mysqld 服务控制脚本执⾏权限

    chmod +x /etc/init.d/mysqld
    
  2. 将 mysqld 服务加⼊到系统服务

    chkconfig --add mysqld
    
  3. 检查 mysqld 服务是否已经⽣效即可
    mysqld 0:关 1:关 2:开 3:开 4:开 5:开 6:关

    chkconfig --list mysqld
    
  4. 启动 mysql 服务, 返回 SUCCESS!

    service mysqld start
    

    如何停止 mysql 服务

     service mysqld stop
    

    # 五、配置环境变量

  5. 编辑 vim ~/.bash_profile,在文件末尾添加下面信息

    export PATH=$PATH:/usr/local/mysql/bin
    
  6. 执行下面命令,使环境变量生效

    source ~/.bash_profile
    

# 六、登录MySQL,并修改密码

  1. 以 root 账户登录 mysql

    mysql -u root -p
    
  2. 输入刚才记录的 临时密码

  3. 修改 Root 账户密码,并刷新权限

    alter user user() identified by "root";
    flush privileges;
    
  4. 设置远程主机登录

    use mysql;
    update user set user.Host='%' where user.User='root';
    flush privileges;
    

# 七、检查防火墙

  1. 做完上面步骤,肯能还是远程连接不上,开放端口或者关闭防火墙

  2. 查看防火墙状态

    systemctl status firewalld
    
  3. 查看开机是否启动防火墙服务

    systemctl is-enabled firewalld
    
  4. 关闭防火墙

    systemctl stop firewalld
    
  5. 禁用防火墙(系统启动时不启动防火墙服务)

    systemctl disable firewalld
    systemctl is-enabled firewalld
    
  6. 开放端口

    firewall-cmd --zone=public --add-port=3306/tcp --permanent
    firewall-cmd --reload   # 配置立即生效 
    
  7. 关闭端口

    firewall-cmd --zone=public --remove-port=3306/tcp --permanent
    firewall-cmd --reload   # 配置立即生效
    
  8. 查看防火墙所有开放的端口

    firewall-cmd --zone=public --list-ports
    
  9. 查看监听的端口
    centos7默认没有 netstat 命令,需要安装 net-tools 工具,yum install -y net-tools

    netstat -lnpt
    
  10. 检查端口被哪个进程占用

```
netstat -lnpt |grep 3306
```
  1. 查看进程的详细信息
```
ps 6832
```
  1. 查看包含 mysql 的所有进程
```
ps -ef | grep mysql
```
  1. 中止进程
```
kill -9 6832
```

# 八、忘记 mysql 密码, 找回密码

  1. 停止 mysql 服务

     service mysqld stop
    
  2. 修改 mysql 的配置文件 my.cnf
    my.cnf 配置文件的位置,一般在 /etc/my.cnf ,有些版本在 /etc/mysql/my.cnf
    在配置文件中添加下面代码, 安全模式启动

    [mysqld]
    skip-grant-tables
    
  3. 启动 mysql 服务

    service mysqld start
    
  4. 进入 mysql, 这样可以不用输入密码进入 mysql

    mysql -u root 
    

    修改密码, 并刷新权限;

    mysql> use mysql;
    Database changed
    mysql> update user set authentication_string="123456" where user="root";
    Query OK, 0 rows affected (0.00 sec)
    Rows matched: 1  Changed: 0  Warnings: 0
    mysql> flush privileges;
    Query OK, 0 rows affected (0.00 sec)
    
  5. 将刚才新添加的配置文件 my.cnf 删除这两行

    [mysqld]
    skip-grant-tables
    
  6. 重新启动 mysql 服务

    service mysqld start
    

    MYSQL 8.0 默认user表

    mysql> select host, user, authentication_string, plugin from user;
    +-----------+------------------+------------------------------------------------------------------------+-----------------------+
    | host      | user             | authentication_string                                                  | plugin                |
    +-----------+------------------+------------------------------------------------------------------------+-----------------------+
    | %         | root             | $A$005$_N6
    HbERn<n&lg8k*XmaW25XLFVs8MDOgM1K18egNm1p87ttKMQ9Awcf0/rUA  | caching_sha2_password |
    | localhost | mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
    | localhost | mysql.session    | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
    | localhost | mysql.sys        | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
    +-----------+------------------+------------------------------------------------------------------------+-----------------------+
    

# 九、Access denied for user 'root'@'localhost' (using password: YES)

  1. 安全模式启动 mysql服务,先将密码设置为空

    use mysql;
    update user set authentication_string='' where user='root';
    
  2. 在重新设置密码, 刷新权限

    use mysql;
    alter user user() identified by "root";
    flush privileges;