这些对我都不管用。我想我的电脑上已经有 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
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!!!!';
如果你在 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
或者使用本机 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
$ 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.
在这里,更新 root 用户表的旧方法不起作用,因为“列‘ Password’不可更新”。
新的方法使用 alter user但只有工程 之后你已经做了 flush privileges;,所以这样做第一。
然后: MariaDB [(none)]> alter user 'root'@'localhost' identified by 'hunter2';
(MariaDB [(none)]>是这里的 MySQL 提示符)
然后再做 flush privileges;。
退出 MySQL 客户端。
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;"