Mysql修改密码以及权限问题

本文详细介绍了在CentOS7环境下解决MySQL登录失败及权限问题的步骤,包括使用skip-grant-tables临时绕过权限检查,修改root用户密码,删除匿名账户,以及如何正确重启MySQL服务。

mysql修改密码小步骤

错误分析:
一开始是密码错误导致,先添加skip-grant-tables(这个配置无视权限的,添加直接回车登录即可),尽心修改密码,发现错误照旧
百度了一下,发现是mysql数据权限问题
因为mysql数据库的user表里,存在用户名为空的账户即匿名账户,导致登录的时候 是虽然用的是root,
但实际是匿名登录的,通过错误提示里的''@'localhost'可以看出来

Centos 7 系统为例:

在 [mysqld] 添加 skip-grant-tables
[root@centos001 install]# vim /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html

[mysqld]
skip-grant-tables
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

# Recommended in standard MySQL setup
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

重启命令:service mysqld restart 重启Mysql服务
mysql -uroot -p 回车即可
[root@centos001 install]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

修改密码:
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| event                     |
| func                      |
| general_log               |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| innodb_index_stats        |
| innodb_table_stats        |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| servers                   |
| slave_master_info         |
| slave_relay_log_info      |
| slave_worker_info         |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
28 rows in set (0.00 sec)

mysql> UPDATE user SET password=PASSWORD('badaokeji') WHERE user='root';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 5  Changed: 0  Warnings: 0

修改密码成功!
刷新数据裤:
FLUSH PRIVILEGES;

登录Mysql

删除我mysql的skip-grant-tables配置

[root@centos001 install]# vim /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html

[mysqld]
skip-grant-tables
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0



重启mysql:
service mysqld restart
    
    
mysql -uroot -pbadaokeji
登录成功!!




登录mysql发现错误:
        ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
        密码正确还是无法登录

mysql数据权限问题

[root@centos001 install]# mysql -uroot -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[root@centos001 install]# 

那么就是我们的Mysql数据库问题
我们继续在/etc/my.con添加:
    [mysqld]
    skip-grant-tables
    
   
重启mysql:
    service mysqld restart

重新登陆Mysql
[root@centos001 ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
+--------------------+
1 row in set (0.00 sec)

mysql> exit;



百度了一下,发现是mysql数据权限问题
因为mysql数据库的user表里,存在用户名为空的账户即匿名账户,导致登录的时候 是虽然用的是root, 
但实际是匿名登录的,通过错误提示里的''@'localhost'可以看出来

修改Mysql权限
 
添加无视权限配置: /etc/my.cof
        [root@centos001 ~]# vim /etc/my.cnf
        # For advice on how to change settings please see
        # http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
        
        [mysqld]
        skip-grant-table
        datadir=/var/lib/mysql
        socket=/var/lib/mysql/mysql.sock
        


登录Mysql:
    [root@centos001 ~]# mysql -uroot -p
    Enter password: 
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 2
    Server version: 5.6.36 MySQL Community Server (GPL)
    
    Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | ambari             |
    | mysql              |
    | performance_schema |
    +--------------------+
    4 rows in set (0.00 sec)
    
    mysql> use msyq;
    ERROR 1049 (42000): Unknown database 'msyq'
    mysql> use mysql;
    Reading table information for completion of table and column names
    You can turn off this feature to get a quicker startup with -A
    
    Database change
    mysql> delete from user where USER='';
    Query OK, 2 rows affected (0.00 sec)
    
    mysql> flush privileges;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> \q
    Bye
    



删除 skip-grant-table 配置
[root@centos001 ~]# vim /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock



重启Mysql
[root@centos001 ~]# service mysqld restart
Redirecting to /bin/systemctl restart  mysqld.service
[root@centos001 ~]# 




重新登录Mysql:
    root@centos001 ~]# mysql -uroot -p
    Enter password: 
    ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
    [root@centos001 ~]# mysql -uroot -p
    Enter password: 
    [root@centos001 ~]# mysql -uroot -p
    Enter password: 
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 5
    Server version: 5.6.36 MySQL Community Server (GPL)
    
    Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | ambari             |
    | mysql              |
    | performance_schema |
    +--------------------+
    4 rows in set (0.00 sec)
    
    mysql>

转载于:https://www.cnblogs.com/fingertip/p/9443385.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值