36

Mariadb10.1.37 ERROR 1698 (28000): Access denied for user 'root'@'localhost' -...

 4 years ago
source link: https://blog.cocktail1024.top/archives/128.html?
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

最近本地环境装了个 MariaDB - 10.1.37, 发现了这个坑

基于 能不折腾就不折腾的原则,直接使用 apt 安装

sudo apt install mariadb-server

安装完之后,初始化一下

sudo mysql_secure_installation
mysql -uroot -hlocalhost -p123456
ERROR 1698 (28000): Access denied for user 'root'@'localhost'

what the f**k?

凭借多年的debug经验,熟悉的打开 google,然后输入 ERROR 1698 (28000): Access denied for user 'root'@'localhost' 点击搜索。

然后,根据搜索结果,排查,尝试

首先出现最多的结果是:

update user set password=PASSWORD("123456") where user='root';  // 就是修改下密码

这里怀疑了下自己,是不是设置密码的时候弄错了,

这里还是写下步奏

  1. 修改 my.cnf 在 [mysqld] 下面增加 skip-grant-tables // 登录的时候忽略mysql 密码

    注意 Mariadb 是/etc/mysql/mariadb.conf.d/50-server.cnf 
  2. sudo service restart mysql // 重启一下mysql
  3. mysql -uroot // 这样就可以root登录了
  4. update mysql.user set password=PASSWORD("123456") where mysql.user='root'; // 修改下root密码
  5. 将第一步的 skip-grant-tables 去掉
  6. sudo service restart mysql // 重启一下mysql

尝试了一把后发现,没有卵用(意料之中,多年码农的经验告诉我密码没有错!)。。。

看到了 这个 blog https://blog.csdn.net/dream361/article/details/65445168

不得不说 csdn 还是有好文章的

与之前版本mysql不同,需要获得操作系统管理员权限,才能登录MariaDBroot用户,普通操作系统用户不能登录MariaDB数据库root用户
dream361

sudo mysql -uroot -hlocalhost -p123456

没错,就是加个sudo,用 root 权限去登录就可以了。。。

直接密码写到命令行是不安全的,例如:

sudo mysql -uroot -hlocalhost -p123456

黑客攻克了你的电脑,然后看历史就能够知道mysql 的用户名跟密码了,不过本地环境就随意了。。

安全点的做法是:

sudo mysql -uroot -hlocalhost -p

直接用root 也是不安全的,可以创建个新用户,然后给一定的权限

  • CREATE USER 'username'@'host' IDENTIFIED BY 'password'; // 新建用户

    • username:你将创建的用户名
    • host:指定该用户在哪个主机上可以登陆,如果是本地用户可用localhost,如果想让该用户可以从任意远程主机登陆,可以使用通配符%
    • password:该用户的登陆密码,密码可以为空,如果为空则该用户可以不需要密码登陆服务器
  • GRANT privileges ON databasename.tablename TO 'username'@'host' // 授权

    • privileges:用户的操作权限,如SELECT,INSERT,UPDATE等,如果要授予所的权限则使用ALL
    • databasename:数据库名
    • tablename:表名,如果要授予该用户对所有数据库和表的相应操作权限则可用*表示,如*.*
    • 注意用以上命令授权的用户不能给其它用户授权,如果想让该用户可以授权,用 GRANT privileges ON databasename.tablename TO 'username'@'host' WITH GRANT OPTION;

更详细的操作:
https://www.jianshu.com/p/d7b9c468f20d


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK