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

Use mysql

mysql> update user set password=PASSWORD('root') where User='root';

最后,重新加载特权:

mysql> flush privileges;
mysql> quit

其他回答

请阅读官方文档: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');

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

忘记密码时的修改步骤:

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

我使用的是mysql-5.7.12-osx10.11-x86_64。dmg在Mac OS X上。

安装过程会自动为root用户设置一个临时密码。你应该保存密码。密码不可恢复。

按照说明操作:

Go to cd /usr/local/mysql/bin/ Enter the temporary password (which would look something like, "tsO07JF1=>3") You should get the mysql> prompt. Run, SET PASSWORD FOR 'root'@'localhost' = PASSWORD('{YOUR_PASSWORD}'); If you wish to set your password: "root" then the command would be, SET PASSWORD FOR 'root'@'localhost' = PASSWORD('root'); Run ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER; Run exit Run ./mysql -u root -p Type your password. In my case I would type, "root" (without quote) That's all.

为了方便起见,您应该在PATH环境变量中添加“/usr/local/mysql/bin”。

现在你可以在任何地方输入。/mysql -u root -p,然后输入密码,你会得到mysql>提示。

如果你还没有设置密码,那么运行mysql -uroot。这对我很管用。

对于那些目前的答案不工作的人,可以试试这个(在macOS上测试):

mysql -h localhost -u root -p --protocol=TCP

在此之后,将要求您输入密码,您应该使用您的操作系统用户密码。然后当你进入MySQL时,你可以运行:

select Host, User from mysql.user;

你应该看到:

MySQL [(none)]> select Host, User from mysql.user;
+-----------+------------------+
| Host      | User             |
+-----------+------------------+
| localhost | mysql.infoschema |
| localhost | mysql.session    |
| localhost | mysql.sys        |
| localhost | root             |
+-----------+------------------+

从这里你可以改变配置,编辑密码或修改拨款。