MySQL 5.7 的默认 root 密码是什么

What is the default root pasword for MySQL 5.7

在使用 root ID 和 empty/no 密码全新安装后无法像其他旧 MySQL 版本那样登录 MySQL 数据库

在 linux 上全新安装 MySQL-community-server 5.7 后,您需要从 /var/log/mysqld.log 中找到临时密码才能以 root 身份登录。

  1. grep 'temporary password' /var/log/mysqld.log
  2. 运行 mysql_secure_installation 更改新密码

参考:http://dev.mysql.com/doc/refman/5.7/en/linux-installation-yum-repo.html

如果您想安装 mysql 或 percona 无人值守(就像我的情况一样),您可以使用以下脚本:

# first part opens mysql log
# second part greps lines with temporary password
# third part picks last line (most recent one)
# last part removes all the line except the password
# the result goes into password variable

password=$(cat /var/log/mysqld.log | grep "A temporary password is generated for" | tail -1 | sed -n 's/.*root@localhost: //p')

# setting new password, you can use  and run this script as a file and pass the argument through the script

newPassword="wh@teverYouLikE"

# resetting temporary password

mysql -uroot -p$password -Bse "ALTER USER 'root'@'localhost' IDENTIFIED BY '$newPassword';"

那里有很多答案说要重新安装 mysql 或使用

的一些组合
mysqld_safe --skip-grant-tables

和/或

UPDATE mysql.user SET Password=PASSWORD('password')

和/或其他...

...None 对我有用


这是对我有用的,在 Ubuntu 18.04,从顶部开始

特别感谢 让我摆脱了对这件事的沮丧...

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

注意以下行:

user     = debian-sys-maint
password = blahblahblah

然后:

$ mysql -u debian-sys-maint -p
Enter password: // type 'blahblahblah', ie. password from 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;  // When you don't have auto-commit switched on

或者:

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

或:

// For MySQL 5.7+
UPDATE mysql.user SET authentication_string=PASSWORD('new_password') where user='root';

然后:

mysql> FLUSH PRIVILEGES;
mysql> COMMIT;  // When you don't have auto-commit switched on
mysql> EXIT

$ sudo service mysql restart
$ mysql -u root -p
Enter password: // Yay! 'new_password' now works!

我刚刚在我的机器上安装了 Linux Mint 19(基于 Ubuntu 18.04)。我从 repo (sudo apt install mysql-server) 安装了 MySQL 5.7,令人惊讶的是,在安装过程中,安装程序没有提示输入 root密码。结果我无法登录 MySQL。我在这里和那里搜索并尝试了我在网上找到的各种答案,包括上面接受的答案。我卸载了(清除名称中带有 mysql 的所有 dpkg)并从默认的 Linux Mint 存储库重新安装。 NONE 有效。

经过几个小时的非生产性工作,我决定从官方页面重新安装 MySQL。我打开了 apt repo 的 MySQL 下载页面 (https://dev.mysql.com/downloads/repo/apt) 并单击了右下角的 下载 按钮。

下一步,运行它与 dpkg:

sudo dpkg -i mysql-apt-config_0.8.10-1_all.deb

在安装设置中,选择您要安装的 MySQL 版本。默认选项是 8.0,但我将其更改为 5.7。单击“确定”退出。在此之后,您的软件源中有一个新的 MySQL 存储库。

更新您的存储库:

sudo apt update

最后,安装MySQL:

sudo apt install mysql-server

现在系统提示我提供 root 密码!希望对有同样经历的人有所帮助。

我遇到了同样的问题,我唯一能做的就是走这条路:

drop user admin@localhost;
flush privileges;
create user admin@localhost identified by 'admins_password'

这让我可以重新创建我的用户名并为用户名输入密码

在我的例子中,数据目录是使用 --initialize-insecure 选项自动初始化的。所以 /var/log/mysql/error.log 不包含临时密码但是:

[Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.

有效的是:

shell> mysql -u root --skip-password
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';

详情:MySQL 5.7 Reference Manual > 2.10.4 Securing the Initial MySQL Account

None 这些答案在 Ubuntu 服务器 18.04.1 和 MySQL 5.7.23 上对我有用。我花了很多时间尝试手动设置密码和 auth 插件,在日志中查找密码(它不存在)等,但都失败了。

解决方法其实超级简单:

sudo mysql_secure_installation

sudo 这样做非常重要。如果您尝试不提升权限,系统会要求您提供 root 密码,您显然没有。

似乎是为了避免开发人员设置 root 用户而设计的,更好的解决方案是:

sudo mysql -u root

然后创建普通用户,设置密码,然后使用该用户工作。

create user 'user'@'localhost' identified by 'user1234';
grant all on your_database.* to 'user'@'localhost';
select host, user from mysql.user;

然后尝试访问:

mysql -u user -p

轰!

MySQL 5.7 更改了安全模型:现在 MySQL root 登录需要 sudo

最简单(也是最安全)的解决方案是创建一个新用户并授予所需的权限。

1.连接到 mysql

sudo mysql --user=root mysql

2。为 phpMyAdmin

创建一个用户
CREATE USER 'phpmyadmin'@'localhost' IDENTIFIED BY 'some_pass';
GRANT ALL PRIVILEGES ON *.* TO 'phpmyadmin'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;

参考 - https://askubuntu.com/questions/763336/cannot-enter-phpmyadmin-as-root-mysql-5-7

MySQL 5.7 或更新版本在全新安装后生成默认临时密码。

要首先使用 MySQL,您需要从 /var/log/mysqld.log 中的日志文件中获取该密码。所以按照以下流程:

  1. grep 'temporary password' /var/log/mysqld.log

  2. mysql_secure_installation

需要第二条命令来更改 MySQL 的密码,并进行某些其他更改,例如删除临时数据库、允许或禁止远程访问 root 用户、删除匿名用户等...

经过多次尝试,我可以使用以下命令(Ubuntu 和派生词)重置默认密码:

sudo -i
mkdir -p /var/run/mysqld
chown mysql:mysql /var/run/mysqld
/etc/init.d/mysql stop
mysqld_safe --skip-grant-tables &
mysql -uroot
use mysql;
update user set authentication_string=password('YOURPASSWORD') where user='root';
update user set plugin="mysql_native_password" where User='root';  
flush privileges;
quit;
sudo /etc/init.d/mysql stop
sudo /etc/init.d/mysql start

有时,即使在终端中输入后

mkdir -p /var/run/mysqld
chown mysql:mysql /var/run/mysqld
/etc/init.d/mysql stop
mysqld_safe --skip-grant-tables &

我收到 mysqld 不存在的错误。因此,退出并再次键入相同的命令。

最后的命令

sudo /etc/init.d/mysql start

有时不起作用。重启电脑后才可以。

MySQL 服务器 5.7 已经默认安装在我的新 Linux Mint 19 上。

但是,MySQL root 密码是什么?结果是:

默认安装使用 auth_socket 代替密码进行身份验证!

它允许免密码登录,前提是使用相同的用户名登录到 Linux 系统。要以 MySQL root user 身份登录,可以使用 sudo:

sudo mysql --user=root

但是如何修改root密码呢?为了说明发生了什么,我创建了一个新用户 "me",具有完全权限,具有:

mysql> CREATE USER 'me'@'localhost' IDENTIFIED BY 'my_new_password';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'me'@'localhost' WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES;

比较 "me" 与 "root":

mysql> SELECT user, plugin, HEX(authentication_string)  FROM mysql.user WHERE user = 'me' or user = 'root';
+------+-----------------------+----------------------------------------------------------------------------+
| user | plugin                | HEX(authentication_string)                                                 |
+------+-----------------------+----------------------------------------------------------------------------+
| root | auth_socket           |                                                                            |
| me   | mysql_native_password | 2A393846353030304545453239394634323734333139354241344642413245373537313... |
+------+-----------------------+----------------------------------------------------------------------------+

因为使用的是auth_socket,root密码无法更改:SET PASSWORD命令失败,mysql_secure_installation什么也得不到...

==> 要切换此备用身份验证模式并 return MySQL root 用户使用密码:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'SOME_NEW_ROOT_PASSWORD';

一个good explanation.

更多details from the MySQL manual.

Ubuntu 20.04 到 MySql 8.0 :您可以这样设置密码:

  1. 使用sudo mysql -u root

    登录mysql
  2. 更改密码:

USE mysql;
UPDATE user set authentication_string=NULL where User='root';
FLUSH privileges;
ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'My-N7w_And.5ecure-P@s5w0rd';
FLUSH privileges;
QUIT

现在您应该可以使用 mysql -u root -p 登录(或使用用户名 root 登录 phpMyAdmin)和您选择的密码。

P,S:

也可以用用户debian-sys-maint登录,密码写在文件/etc/mysql/debian.cnf

非交互模式下(通过脚本):

systemctl start mysqld
MYSQL_ROOT_TMP_PSW=$(grep 'temporary password' $logpath/mysqld.log |sed "s|.*: ||")

## POPULATE SCHEMAS WITH ROOT USER
/usr/bin/mysql --connect-expired-password -u root -p${MYSQL_ROOT_TMP_PSW} < "$mysql_init_script"

这是初始化脚本的头部

SET GLOBAL validate_password_policy=LOW;
FLUSH privileges;

SET PASSWORD = PASSWORD('MYSQL_ROOT_PSW');
FLUSH privileges;

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%';
FLUSH privileges;

...

然后重启服务systemctl restart mysqld