MacOSX 自制 mysql 根密码

由于某种原因,MySQL 停止了对 root 用户的访问。卸载并重新安装家酿。新的安装,新的桌子,但当我进入

mysql -u root -p

我得到了这个错误:

拒绝访问用户‘ root’@‘ localhost’(使用密码: NO)

我重新安装了 MySQL 五次,但它仍然要求输入密码。我该如何解决这个问题?

152131 次浏览

几天前我也遇到过同样的问题。当您通过 homebrew安装 mysql并运行启动 mysql守护进程的初始化脚本(mysql_install_db) 之前时,就会发生这种情况。

要修复它,您可以删除 mysql数据文件,重新启动服务,然后 那么运行初始化脚本:

launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
rm -r /usr/local/var/mysql/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
unset TMPDIR
mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp

在终端运行这些线路对我和其他几个有同样问题的人起到了作用。在 brew 成功安装 mysql 之后,终端中会列出这些指令。

mkdir -p ~/Library/LaunchAgents


cp /usr/local/Cellar/mysql/5.5.25a/homebrew.mxcl.mysql.plist ~/Library/LaunchAgents/


launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist


/usr/local/Cellar/mysql/5.5.25a/bin/mysqladmin -u root password 'YOURPASSWORD'

其中 YOURPASSWORD是 root 用户的密码。

只需运行以下命令(其中 NEWPASS是您的密码) :

$(brew --prefix mysql)/bin/mysqladmin -u root password NEWPASS

我也犯过同样的错误,并用这种方法修复了它。

我刚刚注意到这里的大多数答案都有一些共同之处,并确认了我的新安装。如果您查看不使用 -p运行 mysqladmin -u root的建议,那么实际上是显而易见的。

没有密码。

Brew 仅使用一个 root 用户设置 mysql,而且根本没有密码。我想这是有道理的,但是安装后的注意事项根本没有提到它。

如果你不小心设置并忘记了根密码,你不想擦除所有的数据库并重新开始,因为你懒惰,忘记有一个备份解决方案在适当的地方,你正在使用一个相当新的 Homebrew 安装(2013年冬季) ,这里有几个步骤来重置你的密码为 MySQL。

停止当前正在运行的 MySQL 实例

launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

现在通过手动跳过 Grant 表和网络来启动 mysql

$(brew --prefix mysql)/bin/mysqld_safe --skip-grant-tables --skip-networking

请注意,如果在 bash 中运行 Echo $(brew ——前缀 mysql)时它没有作为 ”/usr/local/opt/mysql响应,则需要相应地调整路径。

完成此操作后,现在应该有一个正在运行的、不受保护的 MySQL 实例。

登录并设置密码

mysql -u root

在提示符下,输入以下 MySQL 命令为生效用户设置新密码。

mysql> update mysql.user set password=PASSWORD('new_password_here') WHERE user='root';

如果一切按计划进行,它应该说:

Query OK, 1 row affected (0.02 sec)
Rows matched: 4  Changed: 1  Warnings: 0

退出 MySQL 提示符。

mysql> exit
Bye

停止服务器:

mysqladmin -u root shutdown

现在,让我们把启动守护进程放回去,这样我们的 MySQL 就又准备好了:

launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

恭喜你。你刚刚重置了 mysql 根密码。给自己倒杯咖啡,然后准备一个备份解决方案!

一号客运大楼:

$ mysql_safe

二号客运大楼:

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

通过自制程序安装 mysql 后得到这个错误。

所以,首先删除安装。然后通过 Homebrew 重新安装

brew update
brew doctor
brew install mysql

然后重新启动 mysql 服务

 mysql.server restart

然后运行此命令设置新的 root 密码。

 mysql_secure_installation

最后,它将请求重新加载特权。答应吧。然后再次登录 mysql。并使用已设置的新密码。

mysql -u root -p

检查一下你的家庭目录中没有隐藏.my.cnf,这是我的问题。

通过 brew 安装 mysql 时的默认密码是 root 尝试,它对我很有效

mysql -uroot -proot

这些对我都不管用。我想我的电脑上已经有 mysql 了,所以设置了密码什么的。在花了几个小时尝试各种解决方案之后,下面的方法对我有效:

$ brew services stop mysql
$ pkill mysqld
$ rm -rf /usr/local/var/mysql/ # NOTE: this will delete your existing database!!!
$ brew postinstall mysql
$ brew services restart mysql
$ mysql -uroot

都归功于@Ghrua

我在 MAC https://flipdazed.github.io/blog/osx%20maintenance/set-up-mysql-osx上用过

通过运行来启动 mysql

酿酒服务启动 mysql

运行安装脚本

Mysql _ security _ install

您将被要求设置一个安装 VALIDATE PASSWORD 插件。

选择所需的密码验证(如果只是您在使用数据库,那么这并不重要)

现在为所有剩余的选项选择 y: 删除 anon。用户; 禁止远程根登录; 删除测试数据库; 重新加载特权表。 现在您应该收到

All done!

这对我有用。希望这对你也有用! ! ! 跟着他们。

brew services stop mysql
pkill mysqld
# NB: the following command will REMOVE all your databases!
# Make sure you have backups or SQL dumps if you have important data in them already.
rm -rf /usr/local/var/mysql/
brew services restart mysql


mysql -uroot
UPDATE mysql.user SET authentication_string=null WHERE User='root';
FLUSH PRIVILEGES;
exit;


mysql -u root
ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password
BY'YOUR_PASS_WORD!!!!';

这对我很有效:

sudo mysql -u root

尝试使用 sudo来避免“拒绝访问”错误:

sudo $(brew --prefix mariadb)/bin/mysqladmin -u root password NEWPASS

  1. 转到苹果图标-> 系统首选项
  2. 打开 Myql
  3. 在实例中,您将看到“初始化数据库”
  4. 点击那个
  5. 您将被要求为 root 设置密码—— > 在那里设置一个强密码
  6. 下次使用这个密码登录 mysql

希望这个能帮上忙。

如果你在 Mojave,Catalina,Big Sur 上跑步,现在在 macOS 上跑步蒙特雷:

brew install mariadb
...
brew services start mariadb
==> Successfully started `mariadb` (label: homebrew.mxcl.mariadb)


$(brew --prefix mariadb)/bin/mysqladmin -u root password newpass
/usr/local/opt/mariadb/bin/mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'root'@'localhost''

也登录根帐户失败:

mariadb -u root
ERROR 1698 (28000): Access denied for user 'root'@'localhost'

然后默认的管理员用户被创建与您的 MacOS帐户 用户名相同的名称,例如 Johnsmit

要以 root 身份登录并设置 root 密码,发出(使用 你的用户名) :

mariadb -u johnsmit
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 17
Server version: 10.4.11-MariaDB Homebrew


Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.


Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


MariaDB [(none)]> ALTER USER 'root'@'localhost' IDENTIFIED BY 'ROOT-PASSWORD';
Query OK, 0 rows affected (0.006 sec)
MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.002 sec)
MariaDB [(none)]> exit
Bye

因此,您可以在 localhost 上更改 root 密码形式 mysql。

奖励: 改变当前或其他用户通行证,您可以使用 mysqladmin命令:

$(brew --prefix mariadb)/bin/mysqladmin -u arunas password 'newsecret'

但由于某些原因,这并不影响本地主机,但应该适用于应用程序登录。

或者使用本机 MySQL 更改用户密码 SQL,它显式地指定主机,在我的例子中是用户的“ localhost”帐户:

mariadb -u arunas
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 10.5.9-MariaDB Homebrew


Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.


Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


MariaDB [(none)]> ALTER USER 'arunas'@'localhost' IDENTIFIED BY 'newsecret';
Query OK, 0 rows affected (0.006 sec)


MariaDB [(none)]> exit
Bye

现在让我们尝试不用密码登录:

mariadb -u arunas
ERROR 1045 (28000): Access denied for user 'arunas'@'localhost' (using password: NO)

您看到登录失败,因此现在我们需要指定密码的需要:

mariadb -u arunas -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 13
Server version: 10.5.9-MariaDB Homebrew


Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.


Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


MariaDB [(none)]> exit
Bye


使用愉快!

我使用 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.

剩下的就交给你了

所以,如果有人有相同的情况和配置,因为我有,也要疯了-这为 工作。

在一个漫长的故事之后,我有了一个安装了 brew 的 MariaDB,当我关闭它的进程时,它会自动重新启动(这是 brew 的做法) ,这是一个根密码 曾经,我做了 没有知道。

$ brew services list

这显示了类似于:

mariadb started jdoe /path/to/homebrew.mxcl.mariadb.plist

停止 MySQL 服务器:

$ brew services stop mariadb

然后再次启动根用户 没有(不使用 brew) :

$ mariadbd --skip-grant-tables &

在这里,mysql_secure_installation不工作,因为我的 --skip-grant-tables,它不会工作没有 --skip-grant-tables,因为它需要密码(我没有)。
尝试 $(brew --prefix mysql)/bin/mysqladmin -u root password hunter2只返回奇怪的错误,什么也没做; $(brew --prefix mariadb)/bin/mysqladmin -u root password hunter2也不工作,给出不同的错误,并建议,不适合我。

但是您现在没有凭据登录 mysql: $ mysql

在这里,更新 root 用户表的旧方法不起作用,因为“列‘ Password’不可更新”。
新的方法使用 alter user但只有工程 之后你已经做了 flush privileges;,所以这样做第一。
然后:
MariaDB [(none)]> alter user 'root'@'localhost' identified by 'hunter2';
(MariaDB [(none)]>是这里的 MySQL 提示符)
然后再做 flush privileges;
退出 MySQL 客户端。

现在只要涉及到酿造,MariaDB 仍然没有运行,因此使用 $ ps aux | grep -i mariadb查找 pid 并使用 $ kill -9 <pid>查找它。
然后使用 $ brew services start mariadb再次启动它。

使用 init 文件启动 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

输入你的新通行证。

启动服务并删除 init 文件

brew services start mysql
rm /tmp/mysql-init

在 mysql 版本8.0.19上进行了测试

因为这个问题很久以前就有人问过/回答过了,所以那些最好的回答对我来说不起作用。这是我的解决方案,在2020年。

背景 : Fresh mysql/mariadb 由自制软件安装。

问题 : root 用户的密码不是空的,也不是未知的。

解决办法 :

  1. Mysql-u 你的-系统-用户名-p
  2. 密码为空(按回车键)
  3. 用户‘ root’@‘ localhost’标识为‘ NEW-ROOT-PASSWORD’;
  4. 同花特权;

原因 :

  1. Homebrew 将创建一个具有根特权的用户,这个特权由当前 MacOS 用户名命名。
  2. 它没有密码
  3. 因为它拥有所有特权,所以只需要重置该用户的 root 密码。
  4. 根用户的初始密码是随机生成的。

我在 Mac 上新安装的时候遇到了这个问题,我安装 MariaDB 的时候使用了:

brew install mariadb

然后开始了这项服务:

brew services start mariadb

当系统提示输入 root 密码时,我无法运行‘ mysql _ security _ install’,然后我注意到安装输出:

mysql_install_db --verbose --user=jonny --basedir=/usr/local/Cellar/ ....

因此,我尝试以 mysql _ install _ db 输出中指定的用户名登录,并且成功了。

mysql -u jonny

然后在 mysql 提示符下,如果您想为 root 用户设置密码:

SET PASSWORD FOR 'root'@'localhost' = PASSWORD('ToPsEcReT');

我也偶然发现了这个问题,具有讽刺意味的是,解决方案只需运行这个程序:

mysql

对我来说,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的任何值都可以。 我个人用一个分号连接了以上所有内容,但是重新格式化后,希望大家都能更容易阅读。

关注@Roman Escart 的文章。 我想关键是使用“ $brew 链接——强制 mysql@5.7” Https://medium.com/macoclock/setup-mysql-in-a-specific-version-on-macos-35d8ad89c699

在 Mac 上运行 Mysql 最简单的方法是什么?

我的解决方案是安装 MAMP

Https://www.mamp.info/en/mac/

如果你正在使用 macOS 并通过 Homebrew 安装 MariaDB,你可以使用 OS root 密码,然后将密码重置为你想要的任何密码,在这种情况下,我删除了 root 的密码:

sudo mysqladmin -u root password ''

或者,如果你想设置一个密码,你可以把密码放在单引号之间:

sudo mysqladmin -u root password 'NEW-PASSWORD-HERE'