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

我该如何修复它并继续?


当前回答

对于WSL2,我输入以下命令

sudo mysql -u root -p

它提示我输入密码,我假设这一步是设置密码。我输入了一个随机字符串,然后就可以访问MySQL提示符了。 这可能不是正确的答案,但至少可以帮助你开始

其他回答

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

停止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

一句话就解决了我的问题。

sudo dpkg-reconfigure mysql-server-5.5

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

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

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

完成服务器的初始化。 在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!

我尝试了Lahiru的正确答案,但它不能与macOS v10.14 (Mojave)上的MySQL服务器版本8.0.16 (Community)一起工作。

我遵循Sameer Choudhary的指示,并进行了一些调整,我能够更改根密码,并从本地主机启用根访问。

所有这些都不是必需的。如果您正在使用Homebrew在Mac OS上安装:

brew install mysql