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

在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

我以root用户($SUDO)安装MySQL,得到了同样的问题

以下是我的解决方法:

sudo cat /etc/mysql/debian.cnf This will show details as: # Automatically generated for Debian scripts. DO NOT TOUCH! [client] host = localhost user = debian-sys-maint password = GUx0RblkD3sPhHL5 socket = /var/run/mysqld/mysqld.sock [mysql_upgrade] host = localhost user = debian-sys-maint password = GUx0RblkD3sPhHL5 socket = /var/run/mysqld/mysqld.sock Above we can see the password. But we are just going to use(GUx0RblkD3sPhHL5) that in the prompt. `mysql -u debian-sys-maint -p Enter password: ` Now provide the password (GUx0RblkD3sPhHL5). Now exit from MySQL and log in again as: `mysql -u root -p Enter password: `

现在提供新密码。这是所有。我们有新密码供以后使用。

这对我很管用。

在服务器初始启动时,假设服务器的数据目录为空,会发生以下情况:

完成服务器的初始化。 在data目录下生成SSL证书和密钥文件。 安装并启用validate_password插件。 创建超级用户帐户“root”@“localhost”。超级用户的密码被设置并存储在错误日志文件中。 要显示它,使用以下命令:

Shell > sudo grep '临时密码' /var/log/mysqld.log

请使用临时密码登录,并自定义超级用户密码,尽快修改root用户密码:

Shell > mysql的root -p

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass5!

我遇到了这个非常烦人的问题,并且找到了许多不管用的答案。我遇到的最好的解决方案是完全卸载MySQL并重新安装。重新安装时,您设置了根密码,这就解决了这个问题。

sudo apt-get purge mysql-server mysql-client mysql-common mysql-server-core-5.5 mysql-client-core-5.5
sudo rm -rf /etc/mysql /var/lib/mysql
sudo apt-get autoremove
sudo apt-get autoclean

这段代码是我在其他地方找到的,所以我没有任何功劳。但它确实有效。要在卸载MySQL后安装它,我认为DigitalOcean有一个很好的教程。看看我的要点。

如何在Ubuntu上安装MySQL