MacOSX homebrew mysql root 密码

163
由于某种原因,MySQL停止了root用户的访问权限。使用Homebrew进行了卸载和重新安装。全新安装,全新的数据表,但是当我输入时,
mysql -u root -p

我遇到了这个错误:
访问被拒绝,用户'root'@'localhost'(未使用密码)。
我已经重新安装了MySQL五次,但它仍然要求输入密码。我该如何解决这个问题?

在Superuser上几乎有一个完全相同的问题。请按照以下步骤操作:http://superuser.com/a/400345 - Icarus
18
只需运行 mysql_secure_installation 命令并回答相应问题即可,非常简单。 - Abhinav Singh
19
mysql_secure_installation命令会提示输入root密码,如果你已经设置但忘记了密码,这个步骤将无法帮助你。 - JohnG
1
如果已经安装了MySQL 5.7,请按照以下步骤操作:https://dev59.com/_2855IYBdhLWcg3waDeR#33924648 - mrucci
2
就我个人而言,“mysql -u root”在这里可行。 - rogerdpack
在 macOS 10.13 上,用户应该使用 'sudo mysql -u root' 而不是 'mysql -u root'。 - NME New Media Entertainment
27个回答

0

对我来说,mysql是使用root用户和无密码设置的。 我想要能够以当前用户身份登录,而不需要-u root这一部分。我使用了以下命令来设置超级用户:

mysql -u root -e "CREATE USER '$USER'@'localhost';"
mysql -u root -e "GRANT ALL PRIVILEGES ON *.* TO '$USER'@'localhost';"
mysql -u root -e "flush privileges;"

$USER 的任何值都可以使用。

我个人将所有内容连接在一起,并用分号进行了重新格式化,希望大家能更容易地阅读。


0

0

终端1:

$ mysql_safe

终端2:

$ mysql -u root
mysql> UPDATE mysql.user SET Password=PASSWORD('new-password') WHERE User='root';
mysql> FLUSH PRIVILEGES;
mysql> quit

这个答案在发布时可能有效,但是在此期间已经失效了;使用(当前的)MySQL版本5.7.21,它会失败并显示“在字段列表中未知的列'Password'”。 - ssc
UPDATE mysql.user SET authentication_string=PASSWORD('xxxxxx!') WHERE User='root'; 请将mysql.user表中User为root的authentication_string字段更新为'xxxxxx!'的加密密码。 - kevinc

0

我正在使用Catalina,并使用此mysql_secure_installation命令,现在对我有效:

$ mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none): << enter root here >>

我输入root作为当前密码

OK, successfully used password, moving on...

Setting the root password or using the unix_socket ensures that nobody
can log into the MariaDB root user without the proper authorisation.

然后做剩下的


0

以 root 身份登录到数据库:

sudo mysql -u root

您可以运行安全的数据库,方法如下:

sudo mysql_secure_installation

-1

-1
使用初始化文件启动mysql以更改root密码。
brew services stop mysql
pkill mysqld
echo "ALTER USER 'root'@'localhost' IDENTIFIED BY 'newRootPass';" > /tmp/mysql-init
$(brew --prefix mysql)/bin/mysqld --init-file=/tmp/mysql-init

您的根密码已更改。请确保正确关闭服务器以保存密码更改。在新终端窗口中执行以下命令

mysqladmin -u root -p shutdown

输入您的新密码。

启动您的服务并删除初始化文件。

brew services start mysql
rm /tmp/mysql-init

已在mysql版本8.0.19上进行测试


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