我一直在按照手册在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)

我该如何修复它并继续?


当前回答

我想在建议重新安装MySql的答案中再补充一个提示:在我的情况下,重新安装还不够,我还必须删除/etc/mysql文件夹(MySql 8, Ubuntu 20)。

其他回答

请阅读官方文档:MySQL:如何重置Root密码

如果您可以访问终端:

MySQL 5.7.6及以上版本:

mysql

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';

MySQL 5.7.5及以前版本:

mysql

mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('MyNewPass');

你必须重设密码!Mac OS X(已测试并正在运行)和Ubuntu的步骤:

停止使用MySQL

sudo service mysql stop

or

sudo /usr/local/mysql/support-files/mysql.server stop

以安全模式启动:

sudo mysqld_safe --skip-grant-tables --skip-networking

(上面一行是整个命令)

这将是一个持续的命令,直到进程结束,所以打开另一个shell/终端窗口,登录没有密码:

mysql -u root

mysql> UPDATE mysql.user SET Password=PASSWORD('password') WHERE User='root';

根据@IberoMedia的评论,对于新版本的MySQL,该字段被称为authentication_string:

mysql> UPDATE mysql.user SET authentication_string =PASSWORD('password') WHERE User='root';

启动MySQL使用:

sudo service mysql start

or

sudo /usr/local/mysql/support-files/mysql.server start

你的新密码是“password”。

注意:对于MySQL > 5.7版本,请尝试以下操作:

update mysql.user set authentication_string='password' where user='root';

在我的情况下,我发现我的根密码在日志文件“mysqld.log”,路径“/var/log”。

在我运行命令“mysql -u root -p”后,我输入我的root密码,我在/var/log/mysql .log中找到。

在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

root# mysql

Use mysql

mysql> update user set password=PASSWORD('root') where User='root';

最后,重新加载特权:

mysql> flush privileges;
mysql> quit