自制 Mariadb Mysql 安装 root 访问被拒绝

Homebrew Mariadb Mysql installation root access denied

所以我基本上是使用自制软件在 mac 上安装带有 mysql 的 mariadb。 这些是我执行的步骤:

运行 mysql_upgrade 之后给了我以下错误:

Version check failed. Got the following error when calling the 'mysql' command line client ERROR 1698 (28000): Access denied for user 'root'@'localhost' FATAL ERROR: Upgrade failed

我不能这样输入mysql:

mysql -uroot
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

但像这样:

sudo mysql -u root

用户tablereturns这个:

MariaDB [(none)]> USE mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [mysql]> SELECT User, Host, plugin FROM mysql.user;
+---------------+-------------------------+-----------------------+
| User          | Host                    | plugin                |
+---------------+-------------------------+-----------------------+
| root          | localhost               | mysql_native_password |
| toms          | localhost               | mysql_native_password |
|               | localhost               |                       |
|               | toms-macbook-pro.local |                       |
+---------------+-------------------------+-----------------------+
4 rows in set (0.004 sec)

MariaDB 10.4 默认为本地 root 启用 Unix socket 身份验证插件。这意味着在新安装的系统上,只要您是本地根用户(例如 sudo 下的 运行)并使用套接字,就可以连接到 运行ning 服务器而无需密码而不是 TCP。

此外,MariaDB 10.4 允许多种帐户身份验证方法。它将本地根配置为也能够使用密码身份验证,但它最初使密码无效(不像过去那样设置空密码)。如果您想使用密码验证并以 mysql -uroot -p 身份连接,您需要首先使用 Unix socket 和 运行 SET PASSWORD=... 以 root 身份连接。

高级用户配置现在以 JSON 格式存储在 mysql.global_priv table 中。 mysql.user 已保留以实现向后兼容性,但它已不再是 table,而是成为一个视图。由于允许多种身份验证方法,它并不总是准确地显示用户配置。具体来说,它不会显示用户可用的所有身份验证方法,您需要为此查询 mysql.global_priv。在全新安装中,您会看到类似

的内容
+-----------+--------+--------------------------------------------------------------------------------------------------------------------------------------------+
| Host      | User   | Priv                                                                                                                                       |
+-----------+--------+--------------------------------------------------------------------------------------------------------------------------------------------+
| localhost | root   | {"access":18446744073709551615,"plugin":"mysql_native_password","authentication_string":"invalid","auth_or":[{},{"plugin":"unix_socket"}]} |
...

您可以找到有关 10.4 身份验证更改的更多信息here

您可以尝试更新root密码,然后再访问

ALTER USER 'root'@'localhost' IDENTIFIED BY 'root';

退出Mysql并尝试登录

mysql -uroot -p # then use root as a password

我正在使用这个 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 install mariadb@10.2

  • 尝试重置 root 密码。

    • 方法一:mysqld_safe命令

      • 运行 命令:brew services stop mariadb@10.2
      • 运行 命令:mysqld_safe --skip-grant-tables --skip-networking
      • 在新的终端选项卡上,
        运行 MariaDB <= 10.4 的命令:mysql_secure_installation
        运行 MariaDB 命令 >= 10.4 mariadb-secure-installation
      • 这将要求输入 root 密码
      • 在不输入任何密码的情况下按回车键(这一步可能永远不会消失!)
      • 如果在上一步中授予空根密码
        在接下来的步骤中输入并重新输入新密码
      • 这可能会显示一些错误 Password update failed!
    • 方法二:/usr/local/mysql/bin/mysqladmin -u root -p password

      • 这将要求输入密码
      • 不输入任何密码直接回车
      • 这会显示一些错误!

但是前面两种方法都不行!

  • 遵循工作方法:
    • 启动 mariadb@10.2 服务brew services start mariadb@10.2

    • 运行 mysql.servert start

    • 这将显示带有错误日志文件位置的错误

    • 典型的mariadb错误文件位置:/usr/local/var/mysql/<filename>.local.err

    • 运行 tail -f /usr/local/var/mysql/<filename>.local.err

    • 再运行mysql.servert start

    • 会出现与Invalid flags lib

      相关的错误
    • 运行 brew services stop mariadb@10.2

    • (备份,备份,备份您的数据库!这将删除所有数据库!)
      运行 sudo rm -rf /usr/local/var/mysql

    • 运行

      mysql_install_db --verbose --user=`whoami`
      --basedir="$(brew --prefix mariadb@10.2)"
      --datadir="/usr/local/var/mysql" --tempdir="/tmp"
      

      这将从brew获取mariaDB Cellar安装路径 这将安装初始数据库。

    • 而不是运行宁mysql_secure_installationmariadb-secure-installation 运行: sudo mysql -u root

    • 这将下降到 mysql shell

    • 输入命令:use mysql;

    • 输入命令:ALTER USER 'root@localhost' IDENTIFIED BY '<password>';(替换)

    • 输入命令:ALTER USER 'root@127.0.0.1' IDENTIFIED BY '<password>';(替换)

    • 输入命令:FLUSH PRIVILEGES;

    • 输入命令:exit

    • 现在您可以 运行 mysql -u root -p 并使用之前输入的 <password>

就这些了!