美文网首页
Centos安装Mysql

Centos安装Mysql

作者: 小二哥很二 | 来源:发表于2021-07-24 14:03 被阅读0次

    这里介绍的是yum安装,数据库8.0

    1、下载yum源:https://dev.mysql.com/downloads/repo/yum/
    注意点击对应的系统版本~

    yum源 复制链接地址

    2、执行:wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
    得到 mysql80-community-release-el7-3.noarch.rpm

    [root@iZwz9ga2spyeb5u9tdq8t5Z default]# ls
    grub  mysql80-community-release-el7-3.noarch.rpm  nss  useradd
    

    3、安装资源包:yum install mysql80-community-release-el7-3.noarch.rpm -y
    4、查看资源包仓库

    [root@iZwz9ga2spyeb5u9tdq8t5Z default]# yum repolist|grep mysql
    mysql-connectors-community/x86_64 MySQL Connectors Community                 212
    mysql-tools-community/x86_64      MySQL Tools Community                      132
    mysql80-community/x86_64          MySQL 8.0 Community Server                 283
    

    4、安装:yum install mysql-community-server -y
    5、启动:systemctl start mysqld

    • 查看进程和版本:
    [root@iZwz9ga2spyeb5u9tdq8t5Z default]# ps -aux|grep mysql
    mysql    15511 40.2 19.4 1313080 366352 ?      Ssl  13:43   0:01 /usr/sbin/mysqld
    root     15649  0.0  0.0 112824   980 pts/0    R+   13:43   0:00 grep --color=auto mysql
    [root@iZwz9ga2spyeb5u9tdq8t5Z default]# mysql --version
    mysql  Ver 8.0.26 for Linux on x86_64 (MySQL Community Server - GPL)
    

    6、查看数据库登录密码:cat /var/log/mysqld.log |grep password

    [root@iZwz9ga2spyeb5u9tdq8t5Z default]# cat /var/log/mysqld.log |grep password
    2021-07-24T05:43:37.091643Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: =rsa%f%+T7j<
    [root@iZwz9ga2spyeb5u9tdq8t5Z default]# 
    

    7、登录:mysql -uroot -p,并输入上面的密码

    [root@iZwz9ga2spyeb5u9tdq8t5Z default]# mysql -uroot -p
    Enter password: 
    ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
    

    8、报错处理
    上面一步看到,输入密码后报错,那么解决方案是在my.cnf里面,在[mysqld]后面任意一行添加“skip-grant-tables”用来跳过密码验证的过程

    9、清空root密码

    • 上一步就可以免密码登录:mysql -uroot -p
    • 使用mysql这个库:use mysql
    • 清空root的密码:update user set authentication_string='' where user='root';
    • 退出数据库:exit
    mysql> use mysql;
    Reading table information for completion of table and column names
    You can turn off this feature to get a quicker startup with -A
    
    Database changed
    mysql> update user set authentication_string='' where user='root';
    Query OK, 1 row affected (0.00 sec)
    Rows matched: 1  Changed: 1  Warnings: 0
    mysql> exit
    Bye
    

    10、更改root密码:上一步清空了密码,那么就可以免密登录了

    • 注释掉my.cnf里的skip-grant-tables
    datadir=/var/lib/mysql
    socket=/var/lib/mysql/mysql.sock
    # skip-grant-tables
    log-error=/var/log/mysqld.log
    pid-file=/var/run/mysqld/mysqld.pid
    -- 插入 --  
    
    • 重启mysql:systemctl restart mysqld
    • 进入数据库修改密码:
    mysql> alter user user() identified by "xxxxxxx";
    Query OK, 0 rows affected (0.00 sec)
    #  刷新权限
    mysql> flush privileges;
    Query OK, 0 rows affected (0.02 sec)
    

    11、设置允许远程用户访问:GRANT ALL ON *.* TO 'root'@'%'

    • 此时会报错:GRANT ALL ON *.* TO 'root'@'%'
    mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
    ERROR 1410 (42000): You are not allowed to create a user with GRANT
    
    • 原因:当前user表中没有root - %记录; 可以更新root - localhost 为 root - %
    mysql> update user set host = '%' where user = 'root' and host='localhost';
    Query OK, 1 row affected (0.00 sec)
    Rows matched: 1  Changed: 1  Warnings: 0
    
    • 再设置远程用户访问:GRANT ALL ON *.* TO 'root'@'%';
    mysql> GRANT ALL ON *.* TO 'root'@'%';
    Query OK, 0 rows affected (0.01 sec)
    
    mysql> flush privileges;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> exit
    Bye
    

    12、登录Navicat
    此时就可以登录远程连接Mysql了~

    相关文章

      网友评论

          本文标题:Centos安装Mysql

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