MySQL:用户'root'@'localhost'被拒绝访问

11

很遗憾,我无法连接到MySQL数据库。

我只是从MySQL网站下载并解压了mysql-5.6.10-win32.zip。

我按照这个教程进行操作,但是我无法以root身份连接到我的MySQL数据库。

我知道以root身份连接而不使用密码并不安全,但是我只需要进行一些测试,所以现在这样做对我来说没问题(如果我知道如何以root身份连接,我也知道如何添加另一个用户)。

my.ini

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.

[mysqld]

# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M

# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin

# These are commonly set, remove the # and set as required.
basedir=c:\Programs\mysql-5.6.10-win32
datadir=c:\Programs\mysql-5.6.10-win32\data
port=3306
server_id=1


# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M 

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 

[client]

port=3306

当我启动mysqld时,我看到:

> mysqld --console --init-file=..\set_root_password.sql
2013-04-02 13:48:24 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2013-04-02 13:48:24 8600 [Note] Plugin 'FEDERATED' is disabled.
2013-04-02 13:48:24 8600 [Note] InnoDB: The InnoDB memory heap is disabled
2013-04-02 13:48:24 8600 [Note] InnoDB: Mutexes and rw_locks use Windows interlocked functions
2013-04-02 13:48:24 8600 [Note] InnoDB: Compressed tables use zlib 1.2.3
2013-04-02 13:48:24 8600 [Note] InnoDB: CPU does not support crc32 instructions
2013-04-02 13:48:24 8600 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2013-04-02 13:48:24 8600 [Note] InnoDB: Completed initialization of buffer pool
2013-04-02 13:48:24 8600 [Note] InnoDB: Highest supported file format is Barracuda.
2013-04-02 13:48:24 8600 [Note] InnoDB: 128 rollback segment(s) are active.
2013-04-02 13:48:24 8600 [Note] InnoDB: Waiting for purge to start
2013-04-02 13:48:24 8600 [Note] InnoDB: 1.2.10 started; log sequence number 1626183
2013-04-02 13:48:24 8600 [Note] Server hostname (bind-address): '*'; port: 3306
2013-04-02 13:48:24 8600 [Note] IPv6 is available.
2013-04-02 13:48:24 8600 [Note]   - '::' resolves to '::';
2013-04-02 13:48:24 8600 [Note] Server socket created on IP: '::'.
2013-04-02 13:48:24 8600 [Note] Event Scheduler: Loaded 0 events
2013-04-02 13:48:24 8600 [Note] Execution of init_file '..\set_root_password.sql' started.
2013-04-02 13:48:24 8600 [Note] Execution of init_file '..\set_root_password.sql' ended.
2013-04-02 13:48:24 8600 [Note] mysqld: ready for connections.
Version: '5.6.10'  socket: ''  port: 3306  MySQL Community Server (GPL)

set_root_password.sql 包含了我在 如何重置root密码 找到的一些命令,以及我希望能有所帮助的其他命令:

UPDATE mysql.user SET Password=PASSWORD('pass') WHERE User='root';
grant all privileges on *.* to 'root'@'%';
grant all privileges on *.* to 'root'@'localhost';
grant all privileges on *.* to 'root@localhost';

SHOW GRANTS FOR 'root'@'%';

FLUSH PRIVILEGES;

但我仍然无法登录。

为了登录,我尝试了:

>mysql -u root
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

>mysql -u root --password=pass
Warning: Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

>mysql
ERROR 1045 (28000): Access denied for user 'ODBC'@'localhost' (using password: NO)

>mysql -u root --password=
Warning: Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

>mysql -u root --password=mysql
Warning: Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

>mysql -u root mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

>mysqladmin --no-defaults -u root version
mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'root'@'localhost' (using password: NO)'

我在某个教程中读到了mysqladmin命令,但是它需要root密码。

我尝试了空密码和默认密码mysql,但都不行。

我还尝试了--skip-grant-tables,但没有成功。

我的MySQL主目录的bin文件夹中没有mysqld_safe,所以我无法尝试该方法。

我阅读了所有与此问题相关的问题,但还不知道可以尝试什么...

我很沮丧,为什么它不像“下载并运行”一样简单。我不想下载Win安装程序,因为我不希望服务在Windows启动时启动,并且其他东西也被复制到我的系统中而失去控制...


我必须问一下,您是用什么命令尝试登录的呢 ;-)? - Najzero
@HankyPankyㇱ 我不明白你在建议什么,请描述一下你的意思,我会尝试的。@hjpotter92 有一个叫做 mysql_secure_installation.pl 的文件,但我不知道如何使用它。@Najzero 我会把它添加到问题中,好主意,谢谢。 - Betlista
@Betlista,您能否尝试使用mysql -u root mysql命令,因为在新安装的情况下,root用户不应该有密码(存在安全风险)。为了绕过可能使用的选项,您也可以尝试使用mysqladmin --no-defaults -u root version命令。 - Najzero
@Najzero 我已将它们添加到问题中(最后两个)。 - Betlista
这不是MySql 8.0的有效解决方案,因为它们不再使用ini文件。 - Sean Munson
显示剩余2条评论
1个回答

4

我刚刚发现,我必须通过指定-h和我的PC名称来连接ipconfig -all

>ipconfig -all

Windows IP Configuration

   Host Name . . . . . . . . . . . . : PC1234
   Primary Dns Suffix  . . . . . . . :
   Node Type . . . . . . . . . . . . : Hybrid
   IP Routing Enabled. . . . . . . . : No
   WINS Proxy Enabled. . . . . . . . : No
   DNS Suffix Search List. . . . . . : tmdev

并且

mysql -u root -h PC1234 --password=pass

运行良好。感谢您的帮助。虽然很奇怪,但现在它可以工作了。

编辑:

我连接时遇到了问题。通过在my.ini中指定bind-address解决了这个问题:

bind-address=localhost

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接