我一直在按照手册在Ubuntu上安装软件套件。我完全不了解MySQL。我已经在我的Ubuntu上完成了以下安装。

sudo apt-get update
sudo apt-get install mysql-server-5.5
sudo apt-get install mysql-client-5.5
sudo apt-get install mysql-common
sudo apt-get install glade
sudo apt-get install ntp

然后我做了

cd ~/Desktop/iPDC-v1.3.1/DBServer-1.1
mysql -uroot -proot <"Db.sql"

我最终得到以下错误消息。

错误1045(28000):拒绝访问用户'root'@'localhost'(使用密码:YES)

我该如何修复它并继续?


当前回答

我使用Ubuntu 16.04 (Xenial Xerus)和安装MySQL 5.7。

我也有同样的问题

禁止root用户登录。

我尝试了以下步骤:

get-select | grep mysql(获取mysql版本)。 dpkg-reconfigure mysql -服务器- 5.7 Mysql -u root -p

如果没有-p,则不会提示您询问密码。进入后,您可以按照以下步骤创建一个带有密码的用户:

CREATE USER 'your_new_username'@'your-hostname' IDENTIFIED BY 'your-password';

GRANT ALL PRIVILEGES ON *.* to 'your_new_username'@'your-hostname' WITH GRANT OPTION;

从根目录退出,从上面给出的<name>登录。

mysql -u <your_new_username> -p

由于某种原因,仍然不能输入MySQL。在所有。我建议养成使用mysql -u <name> -p的习惯。

其他回答

如果问题仍然存在,请尝试强制修改密码:

/etc/init.d/mysql stop

mysqld_safe --skip-grant-tables &

mysql -u root

设置新的MySQL root用户密码:

use mysql;
update user set password=PASSWORD("NEW-ROOT-PASSWORD") where User='root';
flush privileges;
quit;

停止MySQL服务器:

/etc/init.d/mysql stop

启动MySQL服务器并测试它:

mysql -u root -p

在Ubuntu 16.04 (Xenial Xerus)和MySQL 5.7.13版本中,我能够通过以下步骤解决这个问题:

Follow the instructions from section B.5.3.2.2 Resetting the Root Password: Unix and Unix-Like Systems MySQL 5.7 reference manual When I tried #sudo mysqld_safe --init-file=/home/me/mysql-init & it failed. The error was in /var/log/mysql/error.log: 2016-08-10T11:41:20.421946Z 0 [Note] Execution of init_file '/home/me/mysql/mysql-init' started. 2016-08-10T11:41:20.422070Z 0 [ERROR] /usr/sbin/mysqld: File '/home/me/mysql/mysql-init' not found (Errcode: 13 - Permission denied) 2016-08-10T11:41:20.422096Z 0 [ERROR] Aborting

mysql-init的文件权限不是问题。我们需要编辑AppArmor权限。

Edit by sudo vi /etc/apparmor.d/usr.sbin.mysqld .... /var/log/mysql/ r, /var/log/mysql/** rw, # Allow user init file /home/pranab/mysql/* r, # Site-specific additions and overrides. See local/README for details. #include <local/usr.sbin.mysqld> } Do sudo /etc/init.d/apparmor reload Start mysqld_safe again. Try step 2 above. Check file /var/log/mysql/error.log. Make sure there is no error and the mysqld is successfully started. Run mysql -u root -p Enter password: Enter the password that you specified in mysql-init. You should be able to log in as root now. Shutdown mysqld_safe by sudo mysqladmin -u root -p shutdown Start mysqld the normal way by sudo systemctl start mysql

答案可能听起来很傻,但在浪费了几个小时的时间后,我是这样做到的:

mysql -u root -p

我收到了错误信息

错误1045(28000):拒绝访问用户'root'@'localhost'(使用密码:YES)

尽管我输入了正确的密码(你第一次安装MySQL时得到的临时密码)。

当密码提示闪烁时,我输入了密码。

当您的密码丢失时就会发生这种情况。

忘记密码时的修改步骤:

Stop MySQL Server (on Linux): sudo systemctl stop mysql Start the database without loading the grant tables or enabling networking: sudo mysqld_safe --skip-grant-tables --skip-networking & The ampersand at the end of this command will make this process run in the background, so you can continue to use your terminal and run mysql -u root (as root). It will not ask for a password. If you get error like as below: 2018-02-12T08:57:39.826071Z mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exists. mysql -u root ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) [1]+ Exit 1 Make MySQL service directory. sudo mkdir /var/run/mysqld Give MySQL user permission to write to the service directory. sudo chown mysql: /var/run/mysqld Run the same command in step 2 to run MySQL in background. Run mysql -u root. You will get the MySQL console without entering a password. Run these commands FLUSH PRIVILEGES; For MySQL 5.7.6 and newer ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password'; For MySQL 5.7.5 and older SET PASSWORD FOR 'root'@'localhost' = PASSWORD('new_password'); If the ALTER USER command doesn't work use: UPDATE mysql.user SET authentication_string = PASSWORD('new_password') WHERE User = 'root' AND Host = 'localhost'; Now exit To stop the instance started manually: sudo kill `cat /var/run/mysqld/mysqld.pid` Restart MySQL sudo systemctl start mysql

如果你把MySQL作为Docker镜像的一部分(比如在6606端口上),而Ubuntu安装(在3306端口上),指定端口是不够的:

mysql -u root -p -P 6606

将把:

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

因为默认情况下它试图连接到localhost,指定您的本地IP地址可以修复这个问题:

mysql -u root -p -P 6606 -h 127.0.0.1