MySQL ubuntu server 16.04中mysql的默认密码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42421585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 22:13:44  来源:igfitidea点击:

Default password of mysql in ubuntu server 16.04

mysqlubuntupasswordsubuntu-16.04ubuntu-server

提问by Debashisenator

I have installed ubuntu 16.04 server. Mysql server was installed by default in it. When I am trying to access the mysql with mysql -u root -p, I am unable to log in to mysql because I dont have the password. Is there any default password?

我已经安装了 ubuntu 16.04 服务器。Mysql 服务器默认安装在其中。当我尝试使用 访问 mysql 时mysql -u root -p,我无法登录 mysql,因为我没有密码。有没有默认密码?

I have also tried with --skip-grant-tables, even this does not work. Even trying to log in with just mysql -u rootis a failure.

我也试过--skip-grant-tables,即使这也不起作用。即使尝试使用 just 登录mysql -u root也是失败的。

回答by Maoz Zadok

This is what you are looking for:
sudo mysql --defaults-file=/etc/mysql/debian.cnf
MySql on Debian-base Linux usually use a configuration file with the credentials.

这就是您要查找的内容:
sudo mysql --defaults-file=/etc/mysql/debian.cnf
基于 Debian 的 Linux 上的 MySql 通常使用带有凭据的配置文件。

回答by Yasii

I had a fresh installation of mysql-serveron Ubuntu 18.10 and couldn't login with default password. Then only I got to know that by default root user is authenticated using auth_socket. So as in the answer when the plugin changed to mysql_native_password, we can use mysql default password

mysql-server在 Ubuntu 18.10 上进行了全新安装,但无法使用默认密码登录。然后我才知道默认情况下 root 用户是使用auth_socket. 所以在插件更改为的答案中mysql_native_password,我们可以使用mysql默认密码

$ sudo apt install mysql-server
$ sudo cat /etc/mysql/debian.cnf

You can find the following lines in there

你可以在那里找到以下几行

user     = debian-sys-maint
password = password_for_the_user

Then:

然后:

$ mysql -u debian-sys-maint -p
Enter password: 

type the password from debian.cnf

从 debian.cnf 输入密码

mysql> USE mysql
mysql> SELECT User, Host, plugin FROM mysql.user;

+------------------+-----------+-----------------------+
| User             | Host      | plugin                |
+------------------+-----------+-----------------------+
| root             | localhost | auth_socket           |
| mysql.session    | localhost | mysql_native_password |
| mysql.sys        | localhost | mysql_native_password |
| debian-sys-maint | localhost | mysql_native_password |
+------------------+-----------+-----------------------+
4 rows in set (0.00 sec)

mysql> UPDATE user SET plugin='mysql_native_password' WHERE User='root';
mysql> COMMIT; 

Either:

任何一个:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';

Or:

或者:

// For MySQL 5.7+

// 对于 MySQL 5.7+

mysql>UPDATE mysql.user SET authentication_string=PASSWORD('new_password') where user='root';

--Update--

- 更新 -

Sometimes you will need to restart your mysql server.

有时您需要重新启动 mysql 服务器。

sudo service mysql restart

or

或者

sudo systemctl restart mysql

回答by Harper

Mysql by default has root user's authentication plugin as auth_socket, which requires the system user name and db user name to be the same.

mysql默认有root用户的认证插件as auth_socket,要求system用户名和db用户名相同。

Specifically, log in as root or sudo -iand just type mysqland you will be logged in as mysql root, you can then create other operating users.

具体来说,以 root 身份登录或sudo -i直接输入mysql,您将以 mysql 身份登录,root然后您可以创建其他操作用户。

If you do not have a root on host, I guess you should not be allowed to login to mysql as root?

如果你在主机上没有 root,我猜你不应该被允许以 root 身份登录到 mysql?

回答by Shakti Phartiyal

You can simply reset the root password by running the server with --skip-grant-tables and logging in without a password by running the following as root or with sudo:

您可以通过使用 --skip-grant-tables 运行服务器并通过以 root 用户身份或使用 sudo 运行以下命令而无需密码登录来简单地重置 root 密码:

service mysql stop
mysqld_safe --skip-grant-tables &
mysql -u root

mysql> use mysql;
mysql> update user set authentication_string=PASSWORD("YOUR-NEW-ROOT-PASSWORD") where User='root';
mysql> flush privileges;
mysql> quit

# service mysql stop
# service mysql start
$ mysql -u root -p

回答by BeNiza

Although this is an old question, there are several of us still struggle to find an answer. At least I did. Please don't follow all the lengthy solutions. You could simply login to your mysql as root without providing any password (provided it is a fresh installation or you haven't changed the password since your installation) by adding sudo before your mysql command. $sudo mysql -uroot -p mysql> This is because mysql changed the security model in one of the latest versions.

尽管这是一个古老的问题,但我们中的许多人仍在努力寻找答案。至少我做到了。请不要遵循所有冗长的解决方案。您可以通过在 mysql 命令之前添加 sudo 来简单地以 root 身份登录到您的 mysql,而无需提供任何密码(前提是它是全新安装或自安装以来您没有更改密码)。 $sudo mysql -uroot -p mysql> 这是因为 mysql 在最新版本之一中更改了安全模型。

Hope this helps

希望这可以帮助

回答by AmirRezaM75

Note that in Ubuntu systems running MySQL 5.7 (and later versions), the root MySQL user is set to authenticate using the auth_socket plugin by default rather than with a password. you will need to switch its authentication method from auth_socket to mysql_native_password

请注意,在运行 MySQL 5.7(及更高版本)的 Ubuntu 系统中,root MySQL 用户默认设置为使用 auth_socket 插件而不是密码进行身份验证。您需要将其身份验证方法从 auth_socket 切换到 mysql_native_password

as @BeNiza said, they changed the security model. I did following steps and it works for mysql 5.7.27 on ubuntu 18.04

正如@BeNiza 所说,他们改变了安全模型。我做了以下步骤,它适用于 ubuntu 18.04 上的 mysql 5.7.27

sudo apt install mysql-server

sudo apt install mysql-server

The MySQL database software is now installed, but its configuration is not yet complete.

MySQL数据库软件现已安装,但其配置尚未完成。

To secure the installation, MySQL comes with a script that will ask whether we want to modify some insecure defaults. Initiate the script by typing:

为了确保安装安全,MySQL 附带了一个脚本,该脚本会询问我们是否要修改一些不安全的默认值。通过键入以下内容启动脚本:

sudo mysql_secure_installation

sudo mysql_secure_installation

you should press Yand hit the ENTERkey at each prompt.

您应该在每次提示时按下Y并按下该ENTER键。

This will cause issues if you use a weak password

如果您使用弱密码,这将导致问题

You can simply login to your mysql as root without providing any password by adding sudo before your mysql command.

您可以通过在 mysql 命令之前添加 sudo 来简单地以 root 身份登录到您的 mysql,而无需提供任何密码。

sudo mysql

sudo mysql

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your-password';

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your-password';

If you set a weak password you would see the following error:

如果您设置弱密码,您将看到以下错误:

ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

ERROR 1819 (HY000):您的密码不符合当前的策略要求

mysql> FLUSH PRIVILEGES;

mysql> FLUSH PRIVILEGES;

mysql> exit

mysql> exit

Note: After configuring your root MySQL user to authenticate with a password, you'll no longer be able to access MySQL with the sudo mysql command used previously. Instead, you must run the following: mysql -u root -p

注意:将您的 MySQL 根用户配置为使用密码进行身份验证后,您将无法再使用之前使用的 sudo mysql 命令访问 MySQL。相反,您必须运行以下命令: mysql -u root -p

After entering the password you just set, you will see the MySQL prompt.

输入刚才设置的密码后,会看到MySQL提示。

回答by Anand Raja

In latest version, mySQL uses auth_socket, so to login you've to know about the auto generated user credentials. or if you download binary version, while installation process, you can choose lagacy password.

在最新版本中,mySQL 使用auth_socket,因此要登录,您必须了解自动生成的用户凭据。或者如果您下载二进制版本,在安装过程中,您可以选择lagacy password.

To install SQL in linux debian versions

在 linux debian 版本中安装 SQL

sudo apt install mysql-server

to know about the password

知道密码

sudo cat /etc/mysql/debian.cnf

Now login

现在登录

mysql -u debian-sys-maint -p

use the password from debian.cnf

使用密码来自 debian.cnf

How to see available user records:

如何查看可用的用户记录:

USE mysql
SELECT User, Host, plugin FROM mysql.user;

Now you can create a new user. Use the below commands:

现在您可以创建一个新用户。使用以下命令:

use mysql;
CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
GRANT ALL ON *.* TO 'username'@'localhost';
flush privileges;

To list the grants for the particular mysql user

列出特定 mysql 用户的授权

SHOW GRANTS FOR 'username'@'localhost';

How to revoke all the grants for the particular mysql user

如何撤销特定 mysql 用户的所有授权

REVOKE ALL PRIVILEGES, GRANT OPTION FROM 'username'@'localhost';

To delete/remove particular user from user account list

从用户帐户列表中删除/移除特定用户

DROP USER 'username'@'localhost';

For more commands:

更多命令:

$ man 1 mysql

Please don't reset the password for root, instead create a new user and grant rights. This is the best practice.

请不要重置 root 的密码,而是创建一个新用户并授予权限。这是最佳做法。

回答by qingliu

  1. the first you should stop mysql
  2. use this command sudo mysqld_safe --skip-grant-tables --skip-networking &
  3. and then input mysql -u roottry this way,I have been solved my problem with this method.
  1. 首先你应该停止mysql
  2. 使用这个命令 sudo mysqld_safe --skip-grant-tables --skip-networking &
  3. 然后输入mysql -u root试试这种方式,我已经用这种方法解决了我的问题。

回答by Leo Skhrnkv

I think another place to look is /var/lib. If you go there you can see three mysql folders with 'interesting' permissions:

我认为另一个值得关注的地方是/var/lib. 如果你去那里,你可以看到三个具有“有趣”权限的 mysql 文件夹:

user   group 
mysql  mysql

Here is what I did to solve my problem with root password:

这是我为解决 root 密码问题所做的工作:

after running

跑完后

sudo apt-get purge mysql*
sudo rm -rf /etc/mysql

I also ran the following (instead of my_username put yours):

我还运行了以下(而不是 my_username 把你的):

cd /var/lib
sudo chown --from=mysql <my_username> mysql* -R
sudo rm -rf mysql*

And then:

进而:

sudo apt-get install mysql-server

which prompted me to select a new root password. I hope it helps

这提示我选择一个新的 root 密码。我希望它有帮助

回答by rynop

Simply run sudo dpkg-reconfigure mysql-server-5.7

只需运行 sudo dpkg-reconfigure mysql-server-5.7

You can find the version you have installed by running dpkg --get-selections | grep mysql-server

您可以通过运行找到您安装的版本 dpkg --get-selections | grep mysql-server