Ubuntu server 安装的mysql数据库忘记密码的解决方法

2023-02-22,,

客户端连接时报错MySQL数据库出现:Error 1045错误时,就表明输入的用户名或密码错误被拒绝访问了。

解决办法可以分为以下几步:

1.修改mysql配置文件,使得可以无密码登录mysql

sudo vim /etc/mysql/my.cnf

在[mysqld]项下添加

skip-grant-tables

2.重启mysql服务

sudo service mysql restart

3.无密码登录mysql

mysql -uroot -p

4.修改管理员密码

use mysql;

update user set password=password('123') where user='root';

flush privileges;

exit;

5.还原配置文件

6.可以使用下面的命令登录

mysql -uroot -p123

2003 - Can't connect to MySQL server on 'x.x.x.x' (10038)

mysql服务没问题:

    sean@sean:~$ ps -ef|grep mysqld
    mysql      1219      1  0 21:09 ?        00:00:01 /usr/sbin/mysqld
    sean      10373   9602  0 21:38 pts/7    00:00:00 grep --color=auto mysqld

并且本地的登录也能成功:

    sean@sean:~$ mysql -u root -h 127.0.0.1
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 40
    Server version: 5.5.47-0ubuntu0.14.04.1 (Ubuntu)
    Copyright (c) 2000, 2015, 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>

但是使用外网地址却无法登录:

    sean@sean:~$ mysql -u root -h 192.168.137.128
    ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.137.128' (111)

于是修改了一下MySQL的配置文件:

    sean@sean:~$ sudo vi /etc/mysql/my.cnf

在bind-address= 127.0.0.1这一行前加#(注释掉这行)

    # Instead of skip-networking the default is now to listen only on
    # localhost which is more compatible and is not less secure.
    #bind-address           = 127.0.0.1

然后重启mysql服务:

    sean@sean:~$ sudo service mysql restart
    mysql stop/waiting
    mysql start/running, process 11622

ERROR 1130: mysql 1130连接错误的有效解決方法

    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> select host, user, password from user;
+-----------+------------------+-------------------------------------------+
| host | user | password |
+-----------+------------------+-------------------------------------------+
| localhost | root | |
| sean | root | |
| 127.0.0.1 | root | |
| ::1 | root | |
| localhost | debian-sys-maint | *0AA379AB8AFD785B32D661A07E9D5C7A24E3B186 |
+-----------+------------------+-------------------------------------------+
5 rows in set (0.00 sec) mysql> update user set host = "%" where host = "sean" and user = "root";
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0 mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec) mysql> select host, user, password from user;
+-----------+------------------+-------------------------------------------+
| host | user | password |
+-----------+------------------+-------------------------------------------+
| localhost | root | |
| % | root | |
| 127.0.0.1 | root | |
| ::1 | root | |
| localhost | debian-sys-maint | *0AA379AB8AFD785B32D661A07E9D5C7A24E3B186 |
+-----------+------------------+-------------------------------------------+
5 rows in set (0.00 sec)

使用Navicat就可以成功连接至数据库了.

Ubuntu server 安装的mysql数据库忘记密码解决方法的相关教程结束。

《Ubuntu server 安装的mysql数据库忘记密码的解决方法.doc》

下载本文的Word格式文档,以方便收藏与打印。