我一直在按照手册在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并重新安装。重新安装时,您设置了根密码,这就解决了这个问题。

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

其他回答

虽然上面的答案(使用mysqladmin)在macOS v10.15 (Catalina)上有效,但在Ubuntu上无效。然后我尝试了许多其他的选项,包括MySQL的安全启动,但都不起作用。

下面是一个例子:

至少在这个版本中,我得到了5.7.28-0ubuntu0.18.04.4的答案缺少IDENTIFIED WITH mysql_native_password。5.7.28是当前LTS的默认设置,因此应该是大多数新系统的默认设置(直到Ubuntu 20.04 (Focal Fossa) LTS问世)。

我发现不能设置root密码MySQL服务器,现在应用

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_pass_here';

这是可行的。

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

/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

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

忘记密码时的修改步骤:

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

我可以通过执行这条语句来解决这个问题

sudo dpkg-reconfigure mysql-server-5.5

这将改变根密码。

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

停止MySQL服务器(Linux):

/etc/init.d/mysql stop

停止MySQL服务器(Mac OS X):

mysql.server stop

使用——skip-grant-tables启动mysqld_safe守护进程:

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服务器(Linux):

/etc/init.d/mysql stop

停止MySQL服务器(Mac OS X):

mysql.server stop

启动MySQL服务器服务,测试以root用户登录:

mysql -u root -p