我一直在按照手册在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:如何重置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');

其他回答

我以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: `

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

这对我很管用。

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

忘记密码时的修改步骤:

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

在Mac上,如果你在使用安装时提供的第一个密码登录时遇到问题,也许你可以简单地关闭MySQL进程,然后再尝试。

So:

执行如下命令查看MySQL的PID: mysql | grep -v grep 终止进程: Kill -15[进程id]

可以使用初始密码登录:

mysql -uroot -p

它要求你输入密码。只需输入初始密码。

2021年。

回答Ubuntu 20.04 (Focal Fossa)(也可能是其他发行版)。

在四处游荡了几天之后…这些答案对我都没用,我就这么做了,而且成功了!

始终在Bash shell中:

sudo systemctl disable mysql

为了阻止守护进程在启动时启动。

sudo apt purge mysql-server

and

sudo apt purge mysql-community-server*

在那里,它警告你,你将删除配置文件…所以它起作用了!因为他们就是捣乱的人!

sudo autoremove

删除所有遗留的包。

然后重启(也许这是可选的,但我做到了)。 另外,我从MySQL官方网页下载了MySQL -server-8.0:

sudo apt install mysql-server

它正在工作的信号是,当您输入上面的命令时,系统要求您输入根密码。

最后:

mysql -u root -p

还有你之前输入的密码。

在我的情况下,我试图传递一个shell命令到docker容器。在这种情况下,只有第一个词被解释。确保你没有在运行:

mysql

相对于:

mysql -uroot -ppassword schemaname

也许可以试着引用:

'mysql -uroot -ppassword schemaname'