ubuntu安装mysql 密码忘了怎么办_Ubuntu安装MySQL和忘记密码解决方案

今天远程帮一个朋友在 Ubuntu 系统上(版本应该是16或18,没问)安装 MySQL,遇到很多坑,记录下。

一、安装MySQL

1、使用 apt-get 安装

sudo apt-get update

sudo apt-get install mysql-server

中间提示是否下载包,输入Y即可 然后等它下载,默认下载最新版的 MySQL ,也就是 8.0 版本。 不会提示输入密码

2、初始化配置,设置密码

sudo mysql_secure_installation

然后下面步骤有点多,注意认真看

#1

VALIDATE PASSWORD PLUGIN can be used to test passwords...

Press y|Y for Yes, any other key for No: N (我的选项)

#2

Please set the password for root here...

New password: (输入密码)

Re-enter new password: (重复输入)

#3

By default, a MySQL installation has an anonymous user,

allowing anyone to log into MySQL without having to have

a user account created for them...

Remove anonymous users? (Press y|Y for Yes, any other key for No) : N (我的选项)

#4

Normally, root should only be allowed to connect from

'localhost'. This ensures that someone cannot guess at

the root password from the network...

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y (我的选项)

#5

By default, MySQL comes with a database named 'test' that

anyone can access...

Remove test database and access to it? (Press y|Y for Yes, any other key for No) : N (我的选项)

#6

Reloading the privilege tables will ensure that all changes

made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y (我的选项)

3、检查MySQL状态

systemctl status mysql.service

4、在终端连接

mysql -uroot -p;

输入密码

会出现两种情况下,一个是连接成功;一个是连接失败,要么是密码输错了,要么是加密方式有问题,需要修改,解决办法后面会讲   5、连接成功后,设置允许远程访问 默认localhost主机只允许使用终端访问,使用 navicat 访问不了,很奇怪。 可以尝试一下命令解决

GRANT ALL PRIVILEGES ON *.* TO root@localhost IDENTIFIED BY "123456";

如果你的 navicat 还是连接不了,如 提示没有权限访问 mysql.sock 可以尝试输入主机地址为 127.0.0.1 我试了,成功解决。

二、忘记密码解决方案

忘记密码索性也在这篇里一起写了,因为我这里是有关联的。 输入密码半天连接不了,也不知道是当时设置密码的时候打错了,两次都打错不应该啊。 不管了,还是按照老方法,在配置文件里先加上  skip-grant-tables 来临时关闭密码验证,登进去直接改 root 密码吧。 具体操作如下 1、在 mysqld.cnf 里添加 skip-grant-tables

sudo gedit /etc/mysql/mysql.conf.d/mysqld.cnf

当然你也可以使用 vim 命令编辑 在最下面添加

skip-grant-tables

2、重启MySQL

sudo service mysql restart

3、连接MySQL

mysql

只需要输入一个 mysql 即可,不需要指定用户名   4、切换到 mysql 数据库

use mysql;

5、查看一下加密方式

select user, plugin from user;

90b88268539437a23c6f6f1100d06ddf.png 如果是如图 auth_socket 需要将其修改为 mysql_native_password 这就是为什么我改了几次密码,都还是无法连接成功的原因:身份验证的插件是错的。 执行以下命令可以修改

update user set plugin='mysql_native_password' where user='root';

flush privileges;

6、修改密码 执以下命令

ALTER user 'root'@'localhost' IDENTIFIED BY '123456'; //123456是新密码

注意 MySQL8.0是不支持 password() 函数的,所以以下命令是无效的 如果你安装的是MySQL5.x可以执行下面命令

update user set authentication_string=password("123456"),plugin='mysql_native_password' where user='root';

7、退出

exit;

8、去掉 skip-grant-tables 现在我们需要重启了,但是重启前我们可以把之前设置的 skip-grant-tables 去掉了

sudo gedit /etc/mysql/mysql.conf.d/mysqld.cnf

注释掉或者去掉

# skip-grant-tables

9、重启MySQL

sudo service mysql restart

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值