我不知道我的MySQL根密码;我怎么才能知道呢?是否有存储此密码的文件?

我正在跟踪这个链接,但我在本地没有directadmin目录。


当前回答

我用另一种方法解决了这个问题,这对一些人来说可能更简单。

我这样做是因为我尝试在安全模式下启动,但无法连接到错误: ERROR 2002 (HY000):无法通过socket '/var/run/mysqld/mysqld连接到本地MySQL服务器。袜子”(2)

我所做的就是以root用户正常连接:

$ sudo mysql -u root

然后我创建了一个新的超级用户:

mysql> grant all privileges on *.* to 'myuser'@'%' identified by 'mypassword' with grant option;
mysql> quit

然后以myuser身份登录

$ mysql -u myuser -p -h localhost

尝试更改密码没有给我任何错误,但对我没有任何帮助,所以我放弃并重新创建根用户

mysql> drop user 'root'@'localhost;
mysql> mysql> grant all privileges on *.* to 'root'@'localhost' identified by 'mypassword' with grant option;

根用户现在正在使用新密码

其他回答

除了其他答案外,在cpanel安装中,mysql根密码存储在一个名为/root/.my.cnf.的文件中(cpanel服务在更改时将其重置,所以这里的其他答案不会有帮助)

在Windows系统下,请按照以下步骤重置密码

从任务管理器中停止Mysql服务 创建一个文本文件并粘贴下面的语句

MySQL 5.7.5及以前版本: SET PASSWORD FOR 'root'@'localhost' = PASSWORD('yournewpassword');


MySQL 5.7.6及以上版本: ALTER USER 'root'@'localhost'

保存为mysql-init.txt,并将其放在“C”盘。 打开命令提示符并粘贴以下内容

C:\\ > mysqld——init-file=C:\\mysql-init.txt

在你的“主机名”中。在MySQL工作的数据文件夹中的err文件中,尝试寻找以以下开头的字符串:

“为roor@localhost生成临时密码”

你可以使用

less /mysql/data/dir/hostname.err 

然后使用斜杠命令,后面跟着要查找的字符串

/"A temporary password"

然后按n,进入下一个结果。

根据MySql版本的不同,该过程有所不同。按照您的版本所描述的步骤进行操作:

HINTS - Read before the instructions page for your version of MySql* In step 5: Instead of run CMD, create a shortcut on your desktop calling CDM.exe. Then right-click on the shortcut and select "Execute as Administrator". In step 6: Skip the first proposed version of the command and execute the second one, the one with the --defaults-file parameter Once you execute the command, if everything is ok, the CMD window remains open and the command of step 6 continues executing. Simply close the window (click 'x'), and then force close MySQl from the Task Manager. Delete the file with the SQL commands, and start again MySQL. The password must be changed now.

5.0 http://dev.mysql.com/doc/refman/5.0/en/resetting-permissions.html

5.1 http://dev.mysql.com/doc/refman/5.1/en/resetting-permissions.html

…只需在链接中更改版本(5.5,5.6,5.7)

如果您在过去已经设置了密码,mysql -uroot -p解决方案将不起作用,

在我的例子中,我使用了上面的一些答案来解决这个问题(Ubuntu 16)。结果是:

sudo service mysql stop
sudo mysqld_safe --skip-grant-tables &

如果你在屏幕上看到这段文字: mysqld_safe UNIX套接字文件目录“/var/run/mysqld”不存在。 然后做:

sudo mkdir -p /var/run/mysqld
sudo chown mysql:mysql /var/run/mysqld

sudo mysqld_safe --skip-grant-tables & # Look at the & at the end! 

进入其他终端设置密码如下:

sudo mysql -u root

mysql> use mysql;
mysql> SET PASSWORD FOR 'root'@'localhost'=PASSWORD('__NEW__PASSWORD__');
mysql> flush privileges;
mysql> quit;

然后重新启动服务并登录

# end mysqld_safe in the other terminal
sudo service mysql start
sudo mysql -h 127.0.0.1 -uroot -p