我一直在按照手册在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 -uroot。这对我很管用。

其他回答

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

忘记密码时的修改步骤:

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 OS X(已测试并正在运行)和Ubuntu的步骤:

停止使用MySQL

sudo service mysql stop

or

sudo /usr/local/mysql/support-files/mysql.server stop

以安全模式启动:

sudo mysqld_safe --skip-grant-tables --skip-networking

(上面一行是整个命令)

这将是一个持续的命令,直到进程结束,所以打开另一个shell/终端窗口,登录没有密码:

mysql -u root

mysql> UPDATE mysql.user SET Password=PASSWORD('password') WHERE User='root';

根据@IberoMedia的评论,对于新版本的MySQL,该字段被称为authentication_string:

mysql> UPDATE mysql.user SET authentication_string =PASSWORD('password') WHERE User='root';

启动MySQL使用:

sudo service mysql start

or

sudo /usr/local/mysql/support-files/mysql.server start

你的新密码是“password”。

注意:对于MySQL > 5.7版本,请尝试以下操作:

update mysql.user set authentication_string='password' where user='root';

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

对于MySQL < 5.7:

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

mysql -u root

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

mysqladmin -u root password [newpassword]

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

我遇到了这个非常烦人的问题,并且找到了许多不管用的答案。我遇到的最好的解决方案是完全卸载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

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

mysql

相对于:

mysql -uroot -ppassword schemaname

也许可以试着引用:

'mysql -uroot -ppassword schemaname'