年年有"余"

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1621|回复: 0

Ubuntu mysql首次登录root密码

[复制链接]
  • TA的每日心情
    奋斗
    2022-12-13 21:26
  • 签到天数: 371 天

    [LV.9]以坛为家II

    发表于 2019-4-19 14:30:29 | 显示全部楼层 |阅读模式
    1、在Ubuntu上安装mysql
    [Bash shell] 纯文本查看 复制代码
    parallels@ubuntu:~$ sudo apt install mysql

    2、登录mysql时使用root用户,默认密码为空;
    [Bash shell] 纯文本查看 复制代码
    parallels@ubuntu:~$ mysql -uroot -p
    Enter password:
    ERROR 1698 (28000): Access denied for user 'root'@'localhost'

    3、无法登录,可能是密码不正确,也有可能密码为空,但mysql不允许直接使用空密码登录,也有可能root不能使用localhost主机名登录等等;
    4、以上猜测适应所有mysql第一次root登录不成功的可能结论,但如果是Ubuntu中使用apt安装的mysql,有个文件需要关注一下;
    [Bash shell] 纯文本查看 复制代码
    parallels@ubuntu:~$ sudo cat /etc/mysql/debian.cnf
    # Automatically generated for Debian scripts. DO NOT TOUCH!
    [client]
    host     = localhost
    user     = debian-sys-maint
    password = pYL0Xezi0kD8cISU
    socket   = /var/run/mysqld/mysqld.sock
    [mysql_upgrade]
    host     = localhost
    user     = debian-sys-maint
    password = pYL0Xezi0kD8cISU
    socket   = /var/run/mysqld/mysqld.sock
    parallels@ubuntu:~$

    5、这里有mysql相关的用户名和密码,登录client中的user与password试了一下,还真登录上去了。
    [Bash shell] 纯文本查看 复制代码
    parallels@ubuntu:~$ mysql -udebian-sys-maint -p
    Enter password:
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 4
    Server version: 5.7.25-0ubuntu0.18.04.2 (Ubuntu)
    
    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>

    6、查看当前用户,修改密码
    [Bash shell] 纯文本查看 复制代码
    mysql> use mysql
    Database changed
    mysql> select User from user;
    +------------------+
    | User             |
    +------------------+
    | debian-sys-maint |
    | mysql.session    |
    | mysql.sys        |
    | root             |
    +------------------+
    4 rows in set (0.00 sec)
    
    mysql>

    [Bash shell] 纯文本查看 复制代码
    mysql> update user SET authentication_string = PASSWORD('root') WHERE user = 'root';
    Query OK, 1 row affected, 1 warning (0.01 sec)
    Rows matched: 1  Changed: 1  Warnings: 1
    
    mysql> update user set plugin = 'mysql_native_password' where User = 'root';
    Query OK, 1 row affected (0.00 sec)
    Rows matched: 1  Changed: 1  Warnings: 0
    
    mysql> select User,authentication_string,plugin from user;
    +------------------+-------------------------------------------+-----------------------+
    | User             | authentication_string                     | plugin                |
    +------------------+-------------------------------------------+-----------------------+
    | root             | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B | mysql_native_password |
    | mysql.session    | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password |
    | mysql.sys        | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password |
    | debian-sys-maint | *10C1A2E9D2EAE636C49E6D113EE86D10F8B2E108 | mysql_native_password |
    +------------------+-------------------------------------------+-----------------------+
    4 rows in set (0.00 sec)
    
    mysql> FLUSH PRIVILEGES;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql>

    主要四条命令:
    [SQL] 纯文本查看 复制代码
    mysql> use mysql
    mysql> update user SET authentication_string = PASSWORD('root') WHERE user = 'root';
    mysql> update user set plugin = 'mysql_native_password' where User = 'root';
    mysql> FLUSH PRIVILEGES;

    7、搞定
    [Bash shell] 纯文本查看 复制代码
    parallels@ubuntu:~$ mysql -uroot -p
    Enter password:
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 11
    Server version: 5.7.25-0ubuntu0.18.04.2 (Ubuntu)
    
    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>

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    手机版|小黑屋|Archiver|iOS开发笔记 ( 湘ICP备14010846号 )

    GMT+8, 2024-3-29 00:31 , Processed in 0.044576 second(s), 18 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表