MySql教程

详解centos系统上安装mysql8

本文主要是介绍详解centos系统上安装mysql8,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

一、准备工作

  • 1、更新包

    yum update   
    
  • 2、查看是否已经安装过mysql

    rpm -qa | grep -i mysql
    
  • 3、删除mysql

    yum -y remove MySQL-*
    
  • 4、删除配置文件

    rm -rf /etc/my.cnf
    
  • 5、删除默认密码

    rm -rf /root/.mysql_sercret
    

二、安装mysql

  • 1、进入usr目录并且创建目录

    cd /usr
    mkdir soft
    
  • 2、配置mysql的安装源

    sudo rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
    
  • 3、安装mysql

    sudo yum --enablerepo=mysql80-community install mysql-community-server
    
  • 4、启动mysql

    sudo service mysqld start
    
  • 5、查看状态

    service mysqld status
    
    [root@iZwz9et2qekjwu8mwmxgxhZ usr]# service mysqld status
    Redirecting to /bin/systemctl status mysqld.service
    ● mysqld.service - MySQL Server
       Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
       Active: active (running) since Thu 2021-07-01 16:23:53 CST; 8s ago
         Docs: man:mysqld(8)
               http://dev.mysql.com/doc/refman/en/using-systemd.html
      Process: 1171 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
     Main PID: 1252 (mysqld)
       Status: "Server is operational"
       CGroup: /system.slice/mysqld.service
               └─1252 /usr/sbin/mysqld
    
    Jul 01 16:23:44 iZwz9et2qekjwu8mwmxgxhZ systemd[1]: Starting MySQL Server...
    Jul 01 16:23:53 iZwz9et2qekjwu8mwmxgxhZ systemd[1]: Started MySQL Server.
    [root@iZwz9et2qekjwu8mwmxgxhZ usr]# 
    

三、修改密码和远程登录配置

  • 1、查看临时密码

    grep "A temporary password" /var/log/mysqld.log
    

    在这里插入图片描述

  • 2、使用临时密码登录

    mysql -uroot -p
    
  • 3、修改数据库密码(直接修改这样的简单密码会报错的,因为密码策略的问题,必须包括:[大小写字母+数据+符号])

    # 简单密码
    ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
    # 符合标准的密码
    ALTER USER 'root'@'localhost' IDENTIFIED BY 'Aa@123456';
    
  • 4、查看密码策略(要先修改密码才能执行)

    SHOW VARIABLES LIKE 'validate_password.%';
    
    mysql> SHOW VARIABLES LIKE 'validate_password.%';
    +--------------------------------------+--------+
    | Variable_name                        | Value  |
    +--------------------------------------+--------+
    | validate_password.check_user_name    | ON     |
    | validate_password.dictionary_file    |        |
    | validate_password.length             | 8      |
    | validate_password.mixed_case_count   | 1      |
    | validate_password.number_count       | 1      |
    | validate_password.policy             | MEDIUM |
    | validate_password.special_char_count | 1      |
    +--------------------------------------+--------+
    7 rows in set (0.01 sec)
    
    • validate_password.length 是密码的最小长度,默认是8,我们把它改成6

      set global validate_password.length=6;
      
    • validate_password.policy 验证密码的复杂程度,我们把它改成0

      set global validate_password.policy=0;
      
    • validate_password.check_user_name 用户名检查,用户名和密码不能相同,我们也把它关掉

      set global validate_password.check_user_name=off;
      
  • 5、这时候可以修改为简单密码了(简单密码只适合玩玩的数据库密码,不适合生产项目)

  • 6、配置远程可以登录

    CREATE USER 'root'@'%' IDENTIFIED BY '123456';
    
  • 7、刷新配置

    FLUSH PRIVILEGES
    
  • 8、直接在远程使用navicat连接

  • 9、赋权限

    # mysql8之前是这样的
    GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
    # mysql8之后是这样的
    grant all privileges on *.* to root@'%' with grant option;
    
  • 10、补充内容,设置mysql开机启动

    systemctl start mysqld
    systemctl enable mysqld
    systemctl daemon-reload
    
  • 11、停止mysql

    systemctl stop mysqld
    # or 
    service mysqld stop
    

四、使用docker安装mysql8

  • 1、下载mysql

    docker pull mysql
    
  • 2、查看已经安装的mysql版本

    docker image inspect mysql:latest | grep -i version
    
  • 3、在宿主机创建放置mysql的配置文件的目录和数据目录,并且进行授权

    mkdir -p /usr/mysql/conf /usr/mysql/data
    chmod -R 755 /usr/mysql/
    
  • 4、创建配置文件

    vim /usr/mysql/conf/my.cnf
    
    [client]
    
    #socket = /usr/mysql/mysqld.sock
    
    default-character-set = utf8mb4
    
    [mysqld]
    
    #pid-file        = /var/run/mysqld/mysqld.pid
    
    #socket          = /var/run/mysqld/mysqld.sock
    
    #datadir         = /var/lib/mysql
    
    #socket = /usr/mysql/mysqld.sock
    
    #pid-file = /usr/mysql/mysqld.pid
    
    datadir = /usr/mysql/data
    
    character_set_server = utf8mb4
    
    collation_server = utf8mb4_bin
    
    secure-file-priv= NULL
    
    # Disabling symbolic-links is recommended to prevent assorted security risks
    
    symbolic-links=0
    
    # Custom config should go here
    
    !includedir /etc/mysql/conf.d/
    
  • 5、启动容器(注意是一行的)

    docker run --restart=unless-stopped -d --name mysql -v /usr/mysql/conf/my.cnf:/etc/mysql/my.cnf -v 
    /usr/mysql/data:/var/lib/mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql
    
  • 6、查看启动的容器

    docker ps -a
    
  • 7、进入容器中赋权远程连接

    # 进入容器
    docker exec -it mysql /bin/bash
    # 进入mysql
    mysql -uroot -p
    # 修改权限
    grant all privileges on *.* to root@'%' with grant option;
    # 最后别忘记刷新
    FLUSH PRIVILEGES
    
  • 8、在容器中创建一个数据库,在客户端连接是否可以看到新的数据库

    create database docker_test;
    

    五、如果你使用node项目连接mysql的时候会报错,需要以下几步

  • 1、更改加密方式,更改为自己的密码

    ALTER USER 'root'@'%' IDENTIFIED BY '123456' PASSWORD EXPIRE NEVER;
    
  • 2、再次修改下密码

    ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
    
  • 3、刷新

 

这篇关于详解centos系统上安装mysql8的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!