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

我该如何修复它并继续?


当前回答

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

忘记密码时的修改步骤:

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

其他回答

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

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

这对我很管用。

因为你的错误信息显示“PASSWORD: YES”,这意味着你正在使用错误的密码。这种事也发生在我身上。幸运的是,我记得我的正确密码,并能够使数据库连接工作。

我也遇到过类似的问题:

错误1045(28000):用户“root”@“localhost”(正在使用)被拒绝访问 密码:没有)

但对我来说,这个原因真的很愚蠢。 我从Word文档复制了该命令,问题是连字符没有ASCII 0x2D代码,而是Unicode 0xE2 0x80 0x93 UTF-8序列(代码点U+2013 EN DASH)。

错误的方式:

mysql -u root –pxxxx

正确的方式:

mysql -u root -pxxxx

两者看起来一样,但并不相同(尝试一下,复制和粘贴替换密码)。

面对这种类型的错误,建议尝试输入命令,而不是复制和粘贴。

注意:对于MySQL 5.7+,请参见Lahiru对这个问题的回答。这包含了更多的最新信息。

对于MySQL < 5.7:

默认root密码为空(即空字符串),而不是root。所以你可以登录为:

mysql -u root

安装完成后,您显然应该更改root密码:

mysqladmin -u root password [newpassword]

在大多数情况下,您还应该在广泛使用数据库之前设置个人用户帐户。

如果其他答案都不适合您,并且您收到了以下错误:

mysqld_safe Logging to '/var/log/mysql/error.log'.
mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exists.
[1]+  Exit 1                  sudo mysqld_safe --skip-grant-tables

按照以下命令依次执行,直到重置密码:

# Stop your server first
sudo service mysql stop

# Make the MySQL service directory.
sudo mkdir /var/run/mysqld

# Give MySQL permission to work with the created directory
sudo chown mysql: /var/run/mysqld

# Start MySQL, without permission and network checking
sudo mysqld_safe --skip-grant-tables --skip-networking &

# Log in to your server without any password.
mysql -u root mysql


# Update the password for the root user:
UPDATE mysql.user SET authentication_string=PASSWORD('YourNewPasswordBuddy'), plugin='mysql_native_password' WHERE User='root' AND Host='localhost';

# If you omit (AND Host='localhost') section, it updates
# the root password regardless of its host

FLUSH PRIVILEGES;
EXIT;

# Kill the mysqld_safe process
sudo service mysql restart

# Now you can use your new password to log in to your server
mysql -u root -p

# Take note for remote access. You should create a remote
# user and then grant all privileges to that remote user