MySQL 5.7的默认root密码是什么

问题描述 投票:46回答:11

在使用root ID进行全新安装后无法登录到MySQL数据库,并且像其他较旧的MySQL版本一样无法登录到无/无密码

mysql linux passwords password-recovery mysql-5.7
11个回答
90
投票

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

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

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


0
投票

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

[警告]使用空密码创建root @ localhost!请考虑关闭--initialize-insecure选项。

有用的是:

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


0
投票

在Ubuntu Server 18.04.1和MySQL 5.7.23上,这些答案都不适用于我。我花了很多时间尝试并且无法手动设置密码和auth插件,在日志中找到密码(它不在那里)等。

解决方案实际上非常简单:

sudo mysql_secure_installation

使用sudo做这件事非常重要。如果你没有提升尝试,你会被要求输入root密码,你显然没有。


77
投票

有很多答案说重新安装mysql或使用一些组合

mysqld_safe --skip-grant-tables

和/或

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

和/或别的......

......这些都不适合我


这是我在Ubuntu 18.04上从顶部起作用的

特别感谢this answer让我摆脱了对此的挫败感......

$ 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!

15
投票

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


6
投票

如果你想安装无人值守的mysql或percona(比如我的ansible),你可以使用以下脚本:

# 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 $1 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';"

1
投票

似乎事情的目的是避免开发人员选择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

繁荣!


1
投票

Mysql会在安装时生成默认临时密码,因此首先要使用mysql,您需要从/var/log/mysqld.log中的日志文件中获取该密码。请遵循以下流程 -

grep'临时密码'/var/log/mysqld.log

mysql_secure_installation - 这是更改mysql密码所必需的,也可以进行某些其他更改,例如删除临时数据库,允许或禁止远程访问root用户,删除匿名用户等。


1
投票

经过大量的尝试,我可以使用以下命令(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

有时不起作用。仅在重新启动计算机后。


0
投票

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

经过几个小时的非生产性工作,我决定从官方页面重新安装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密码!希望通过同样的经验帮助其他人。


0
投票

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

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

这允许我重新创建用户名并输入用户名的密码

© www.soinside.com 2019 - 2024. All rights reserved.