考虑:

./mysqladmin -u root -p** '_redacted_'  

输出(包括输入密码):

输入密码: Mysqladmin: connect to server at 'localhost' failed错误 '用户'root'@'localhost'(使用密码:YES)拒绝访问'

我该如何解决这个问题?


当前回答

修复macOS

Install MySQL from https://downloads.mysql.com/archives/community/ (8.x is the latest as on date, but ensure that the version is compatible with the macOS version) Give password for root (let <root-password> be the password) during installation (don't forget to remember the password!) Select Use Legacy Password Encryption option (that is what I had used and did not try for Use Strong Password Encryption option) Search and open MySQL.prefPane (use search tool) Select Configuration tab Click Select option of Configuration File Select /private/etc/my.cnf From terminal open a new or existing file with name /etc/my.cnf (vi /etc/my.cnf) add the following content: [mysqld] skip-grant-tables Restart mysqld as follows: ps aux | grep mysql kill -9 <pid1> <pid2> ... (grab pids of all MySQL related processes) mysqld gets restarted automatically Verify that the option is set by running the following from terminal: ps aux | grep mysql > mysql/bin/mysqld ... --defaults-file=/private/etc/my.cnf ... (output) Run the following command to connect (let mysql-<version>-macos<version>-x86_64 be the folder where MySQL is installed. To grab the actual folder, run ls /usr/local/ and copy the folder name): /usr/local/mysql-<version>-macos<version>-x86_64/bin/mysql -uroot -p<root-password>

其他回答

对于Ubuntu/Debian用户

(它可能适用于其他发行版,尤其是基于debian的发行版。)

运行以下命令以root身份连接(不需要任何密码)

sudo /usr/bin/mysql --defaults-file=/etc/mysql/debian.cnf

如果你不想每次以根用户连接时都添加——defaults-file,你可以将/etc/mysql/debian.cnf复制到你的主目录:

sudo cp /etc/mysql/debian.cnf ~/.my.cnf

然后:

sudo mysql

解决方法:放弃!

听我说完。我花了整整两天的时间试图让MySQL工作,但没有任何效果,总是被权限错误卡住,没有一个是通过这个问题的答案解决的。我想,如果我继续下去,我会疯掉的。

由于没有耐心,我发送了安装SQLite的命令,只使用了450 KB,它从一开始就工作得很好。

如果你没有圣人的耐心,那就使用SQLite吧,这样可以为你节省大量的时间、精力、痛苦和存储空间。

我找到的所有解决方案都比必要的要复杂得多,没有一个适合我。这是解决我问题的办法。不需要重新启动mysqld或使用特殊权限启动它。

sudo mysql

-- for MySQL
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';

-- for MariaDB
ALTER USER 'root'@'localhost' IDENTIFIED VIA mysql_native_password USING PASSWORD('root');

通过一个查询,我们将auth_plugin更改为mysql_native_password,并将根密码设置为root(可以在查询中随意更改)。

现在您应该可以使用root登录了。更多信息可以在MySQL文档或MariaDB文档中找到。

(使用Ctrl + D或输入Exit退出MySQL控制台。)

之前的答案都没有帮助我解决这个问题,所以这里是我找到的解决方案。

相关部分:

In Ubuntu systems running MySQL 5.7 (and later versions), the root MySQL user is set to authenticate using the auth_socket plugin by default rather than with a password. This allows for some greater security and usability in many cases, but it can also complicate things when you need to allow an external program (e.g., phpMyAdmin) to access the user. In order to use a password to connect to MySQL as root, you will need to switch its authentication method from auth_socket to mysql_native_password. To do this, open up the MySQL prompt from your terminal: sudo mysql Next, check which authentication method each of your MySQL user accounts use with the following command: SELECT user,authentication_string,plugin,host FROM mysql.user; Output +------------------+-------------------------------------------+-----------------------+-----------+ | user | authentication_string | plugin | host | +------------------+-------------------------------------------+-----------------------+-----------+ | root | | auth_socket | localhost | | mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost | | mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost | | debian-sys-maint | *CC744277A401A7D25BE1CA89AFF17BF607F876FF | mysql_native_password | localhost | +------------------+-------------------------------------------+-----------------------+-----------+ 4 rows in set (0.00 sec) In this example, you can see that the root user does in fact authenticate using the auth_socket plugin. To configure the root account to authenticate with a password, run the following ALTER USER command. Be sure to change password to a strong password of your choosing, and note that this command will change the root password you set in Step 2: ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password'; Then, run FLUSH PRIVILEGES which tells the server to reload the grant tables and put your new changes into effect: FLUSH PRIVILEGES; Check the authentication methods employed by each of your users again to confirm that root no longer authenticates using the auth_socket plugin: SELECT user,authentication_string,plugin,host FROM mysql.user; Output +------------------+-------------------------------------------+-----------------------+-----------+ | user | authentication_string | plugin | host | +------------------+-------------------------------------------+-----------------------+-----------+ | root | *3636DACC8616D997782ADD0839F92C1571D6D78F | mysql_native_password | localhost | | mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost | | mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost | | debian-sys-maint | *CC744277A401A7D25BE1CA89AFF17BF607F876FF | mysql_native_password | localhost | +------------------+-------------------------------------------+-----------------------+-----------+ 4 rows in set (0.00 sec) You can see in this example output that the root MySQL user now authenticates using a password. Once you confirm this on your own server, you can exit the MySQL shell: exit

我试图利用Mac上的Docker桌面来运行5.7.35和这个Docker -compose。Yml配置允许它工作:

特别是这一行…

命令:default-authentication-plugin = mysql_native_password

...这招奏效了

version: '3.3'
services:
  mysql_db:
    image: mysql:5.7
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: 'your_password'
    ports:
      - '3306:3306'
    expose:
      - '3306'
    volumes:
      - ~/your/volume/path:/var/lib/mysql