当前位置: 首页>>技术教程>>正文


mysql 安装时不要求输入 root 密码

, , ,

问题描述

我使用 apt install mysql-server 在 Ubuntu 16.04 上安装 MySQL,但在安装过程中,它没有要求输入 root 密码。

安装后,当我尝试以 root 身份登录时,出现 ERROR 1045,而 mysql_secure_installation 也出现相同错误。我重新安装了 purge d 和 autoremove d,但不起作用。

我该如何解决这个问题?如果我在安装期间没有设置 root 密码,我可以设置吗?

这是我的安装日志:

\\n

The following additional packages will be installed:\\n  libaio1 mysql-client-5.7 mysql-client-core-5.7 mysql-server-5.7\\n  mysql-server-core-5.7\\nSuggested packages:\\n  mailx tinyca\\nThe following NEW packages will be installed:\\n  libaio1 mysql-client-5.7 mysql-client-core-5.7 mysql-server mysql-server-5.7\\n  mysql-server-core-5.7\\n0 upgraded, 6 newly installed, 0 to remove and 0 not upgraded.\\nNeed to get 0 B/17,9 MB of archives.\\nAfter this operation, 160 MB of additional disk space will be used.\\nDo you want to continue? [Y/n] y\\nPreconfiguring packages ...\\nSelecting previously unselected package libaio1:amd64.\\n(Reading database ... 227144 files and directories currently installed.)\\nPreparing to unpack .../libaio1_0.3.110-2_amd64.deb ...\\nUnpacking libaio1:amd64 (0.3.110-2) ...\\nSelecting previously unselected package mysql-client-core-5.7.\\nPreparing to unpack .../mysql-client-core-5.7_5.7.12-0ubuntu1_amd64.deb ...\\nUnpacking mysql-client-core-5.7 (5.7.12-0ubuntu1) ...\\nSelecting previously unselected package mysql-client-5.7.\\nPreparing to unpack .../mysql-client-5.7_5.7.12-0ubuntu1_amd64.deb ...\\nUnpacking mysql-client-5.7 (5.7.12-0ubuntu1) ...\\nSelecting previously unselected package mysql-server-core-5.7.\\nPreparing to unpack .../mysql-server-core-5.7_5.7.12-0ubuntu1_amd64.deb ...\\nUnpacking mysql-server-core-5.7 (5.7.12-0ubuntu1) ...\\nSelecting previously unselected package mysql-server-5.7.\\nPreparing to unpack .../mysql-server-5.7_5.7.12-0ubuntu1_amd64.deb ...\\nUnpacking mysql-server-5.7 (5.7.12-0ubuntu1) ...\\nSelecting previously unselected package mysql-server.\\nPreparing to unpack .../mysql-server_5.7.12-0ubuntu1_all.deb ...\\nUnpacking mysql-server (5.7.12-0ubuntu1) ...\\nProcessing triggers for libc-bin (2.23-0ubuntu3) ...\\nProcessing triggers for man-db (2.7.5-1) ...\\nProcessing triggers for systemd (229-4ubuntu4) ...\\nProcessing triggers for ureadahead (0.100.0-19) ...\\nSetting up libaio1:amd64 (0.3.110-2) ...\\nSetting up mysql-client-core-5.7 (5.7.12-0ubuntu1) ...\\nSetting up mysql-client-5.7 (5.7.12-0ubuntu1) ...\\nSetting up mysql-server-core-5.7 (5.7.12-0ubuntu1) ...\\nSetting up mysql-server-5.7 (5.7.12-0ubuntu1) ...\\nupdate-alternatives: using /etc/mysql/mysql.cnf to provide /etc/mysql/my.cnf (my.cnf) in auto mode\\nChecking if update is needed.\\nThis installation of MySQL is already upgraded to 5.7.12, use --force if you still need to run mysql_upgrade\\nSetting up mysql-server (5.7.12-0ubuntu1) ...\\nProcessing triggers for libc-bin (2.23-0ubuntu3) ...\\n

\\n

最佳思路

您可以在不加载 grant-tables 的情况下启动 mysql,从而恢复或设置 root 密码,而无需知道当前密码。

请注意命令中的 $。这指定了您在输入命令时看到的终端提示符。它显示它是一行文本,但实际输入的是终端命令。“ mysql> ”前缀也是一个提示符。这是您在交互运行 mysql 时获得的提示符。

这是执行此操作的 CLI(命令行):\n(执行这些步骤之前,请务必停止当前服务器。一次只能运行一个服务器。)

$ sudo mkdir /var/run/mysqld; sudo chown mysql /var/run/mysqld
$ sudo mysqld_safe --skip-grant-tables&

现在您可以无需密码以root身份登录并执行所有命令,如在本例中,将root密码设置为root。

$ sudo mysql --user=root mysql

如果您使用的是 MySQL 5.6 或更低版本,则这是您将在 mysql 内部执行的设置 root 密码:

mysql> update user set Password=PASSWORD('new-password') where user='root';
flush privileges;

在 MySQL 5.7 或更高版本中

mysql> update user set authentication_string=PASSWORD('new-password') where user='root';
flush privileges;

从那里,退出(终止正在运行的 msqld)mysql 并正常启动它。

启动和停止mysql服务的注意事项:

停止mysql:

$ sudo service mysql stop

启动mysql(正常):

$ sudo service mysql start

终止临时的 mysql 安全模式会话:

$ sudo mysqladmin shutdown

次佳思路

在 Ubuntu 16.04 中安装 mysql 时不会要求输入密码,但您可以在安装成功后通过以下方式设置密码:

mysql安装完成后,运行命令:

sudo mysql_secure_installation

它将显示:

\\n

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB\\n SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!

\\n

In order to log into MariaDB to secure it, we’ll need the current\\n password for the root user. If you’ve just installed MariaDB, and you\\n haven’t set the root password yet, the password will be blank, so you\\n should just press enter here.

\\n

Enter current password for root (enter for none):(此处按 Enter 键)

\\n

OK, successfully used password, moving on…

\\n

Setting the root password ensures that nobody can log into the MariaDB\\n root user without the proper authorisation.

\\n

Set root password? [Y/n] y   (press 'y' to set new password)\\nNew password: \\nRe-enter new password:\\n

\\n

Password updated successfully! Reloading privilege tables.. …\\n Success!

\\n


对于 Ubuntu 18.04 或 mysql-server 版本 5.7.22,此方法无效

要在 Ubuntu 18.04 中设置 root 密码,首先按照 L.D. James’s answer 的前三个命令或前两个步骤,然后运行,

mysql> alter user 'root'@'localhost' identified by '<password>';

root用户的密码已设置!

OR

按照以下步骤在 18.04 中设置 root 密码:

由于没有为 root 用户设置密码,因此只需使用 blank password 登录

sudo mysql -u root -p
Enter password: (press enter as no password is set)

之后可以轻松运行查询

ALTER USER 'root'@'localhost' IDENTIFIED BY '<password>';

第三种思路

安装后只需:

  1. sudo mysql \n(是,无用户无通行证)\n

  2. 您现在位于 mysql 控制台。\n执行:\n ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'my_new_pass'; \n

  3. 刷新权限;\n

就这样。你下次就可以像平常一样进入控制台了:

mysql -u root -pmy_new_pass

第四种思路

显然,16.04 上的 mysql-server 安装(或任何 5.7 安装?)允许 root 访问,但不是使用密码,而是通过 auth_socket 插件。运行 sudo mysql -u root(注意:没有密码)将为您提供 mysql 控制台,而以非 root 身份运行该命令会提示您输入密码。

看起来更改密码并没有太大区别,因为身份验证后端甚至不检查密码。有一篇关于如何使用 change the plugin and switch to password authentication 的非常全面的文章。

第五种思路

  1. 有一个默认用户帐户。

    \n

    $ vim /etc/mysql/debian.cnf \n

    \n

    您将从中获取此帐户,如下所示:

    \n

    user     = debian-sys-maint\npassword = nUZTARYslBsASzpw\n
  2. 您使用此账户登录

    \n

    $ mysql -u debian-sys-maint -p\n
  3. 编辑你的 root 密码

    \n

    mysql> show databases\uff1b\n\nmysql> use mysql;\n\nmysql> update user set authentication_string=PASSWORD("new_password") where user='root';\n\nmysql> update user set plugin="mysql_native_password";\n\nmysql> flush privileges;\n\nmysql> quit;\n
  4. 重启 mysql

    \n

    $ /etc/init.d/mysql restart;\n

然后您可以使用之前的设置 mysql -u root -p

参考资料

本文由Ubuntu问答整理, 博文地址: https://ubuntuqa.com/article/14499.html,未经允许,请勿转载。