centos7.0解决Access denied for user ''@'localhost' to database 'mysql错误

centos修改mysql密码或者
进入mysql后解决Access denied for user ”@’localhost’ to database ‘mysql错误

原因是MySQL的密码有问题
用mysql匿名用户可以进入数据库,但是看不见mysql数据库.
解决办法:
具体操作步骤:
关闭mysql:

service mysqld stop

然后:

mysqld_safe –skip-grant-tables

开启另一个终端并启动mysql:

service mysqld start

mysql -u root
mysql> use mysql
mysql> UPDATE user SET Password=PASSWORD(‘root’) WHERE user=’root’;
mysql> flush privileges;
mysql>\q
到这里密码已经修改成功,
mysql -u root -p

提示:ERROR 1044 (42000): Access denied for user ”@’localhost’ to database ‘mysql’。前两天也出现过这个问题,网上找了一个比较流行的方法(见方法一),搞定了。今天又用这个试了试,却搞不定,在网上找了半天,终于发现是因为mysql数据库的user表里,存在用户名为空的账户即匿名账户,导致登录的时候是虽然用的是root,但实际是匿名登录的,通过错误提示里的”@’localhost’可以看出来,于是解决办法见方法二。

方法一:
1.关闭mysql
# service mysqld stop
2.屏蔽权限
# mysqld_safe –skip-grant-table
屏幕出现: Starting demo from …..
3.新开起一个终端输入
# mysql -u root mysql
mysql> UPDATE user SET Password=PASSWORD(‘newpassword’) where USER=’root’;
mysql> FLUSH PRIVILEGES;//记得要这句话,否则如果关闭先前的终端,又会出现原来的错误
mysql> \q

方法二:
1.关闭mysql
# service mysqld stop
2.屏蔽权限
# mysqld_safe –skip-grant-table
屏幕出现: Starting demo from …..
3.新开起一个终端输入
# mysql -u root mysql
mysql> delete from user where USER=”;
mysql> FLUSH PRIVILEGES;//记得要这句话,否则如果关闭先前的终端,又会出现原来的错误
mysql> \q

/etc/my.cnf文件内容如下:

skip_grant_tables改为skip_grant_tables保存后,重启mysql服务,然后改密码和删除user=”的记录

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html

[mysqld]
character-set-server=utf8
#bind-address=0.0.0.0
port=3306

#skip_grant_tables
max_connections=200
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
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

mysql永许远程访问方法:

mysql账户是否不允许远程连接。如果无法连接可以尝试以下方法:

    mysql -u root -p    //登录MySQL 
    mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'WITH GRANT OPTION;     //任何远程主机都可以访问数据库 
    mysql> FLUSH PRIVILEGES;    //需要输入次命令使修改生效
    mysql> EXIT    //退出

    也可以通过修改表来实现远程:

    mysql -u root -p

    mysql> use mysql; 
    mysql> update user set host = '%' where user = 'root'; 
    mysql> select host, user from user;

你可能感兴趣的:(操作系统,Linux/Unix,数据库)