MySQL 'root'@'localhost'无法登录

localhost无法登陆的问题:

由于公司的mysql是用rpm包安装的,所以会引起很多小疏忽;

问题解决一:

今天早上同事说MySQL root账号登录不上了。我试了一下 
#mysql -u root -p 
提示”Access denied for user ‘root’@’localhost’ (using password: YES)”

因为年后有同事离职,我第一反应是谁修改了root密码?按照忘记root密码来重置一下密码: 
#/etc/init.d/mysql stop 
#mysqld_safe –skip-grant-tables & 
#mysql -uroot -p 
mysql>update mysql.user set password=password(‘mypassword’) where user=’root’; 
mysql>flush privileges; 
mysql>quit

用新密码还是无法登录,提示跟上面一样。换一个非root账号登录,查看一下user表: 
mysql> select user,host from user; 
+———–+———+ 
| user   | host | 
+———–+———+ 
| root  | 127.0.0.1 | 
| night | % | 
+———–+———+

怀疑默认的localhost没有映射到127.0.0.1?试试#mysql -u root -p xxxx -h 127.0.0.1,果然可以登录。 
之前配置数据库的同学没有给’root’@’localhost’和’root’@’ip’授权。 
grant all privileges on . to ‘root’@’localhost’ identified by ‘mypassword’ with grant option; 
grant all privileges on . to ‘root’@’118.192.91.xxx’ identified by ‘mypassword’ with grant option;


问题解决二:

    

提示ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO),阻止进入MySQL环境,原因是之前用phpmyadmin,设置了root帐号密码,解决办法是:

找到/etc/mysql/my.cnf,在里面添加几行代码,如下:

原来是这样:

[html]  view plain  copy
  1. [client]  
  2. port            = 3306  
  3. socket          = /var/lib/mysql/mysql.sock  

添加三行代码,变为:

[html]  view plain  copy
  1. [client]  
  2. host            = localhost        //添加host  
  3. user            = root               //添加用户名  
  4. password        = 123456      //添加密码  
  5. port            = 3306  
  6. socket          = /var/lib/mysql/mysql.sock  

不用 重启mysql(如果不行,还是重启一下比较好,),直接在终端再次输入sudo mysql,就直接进入mysql.


以上时博主我从网上收集过来的


[client]
#password       = your_password
port            = 3306
socket          = /tmp/mysql.sock


[mysqld]
port            = 3306
socket          = /tmp/mysql.sock
datadir = /usr/local/mysql/var
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
thread_cache_size = 8
query_cache_size = 8M
tmp_table_size = 16M


#skip-networking
max_connections = 500
max_connect_errors = 100
open_files_limit = 65535


log-bin=mysql-bin
binlog_format=mixed
server-id       = 1
expire_logs_days = 10


default_storage_engine = InnoDB
innodb_data_home_dir = /usr/local/mysql/var
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = /usr/local/mysql/var
innodb_buffer_pool_size = 16M
innodb_additional_mem_pool_size = 2M
innodb_log_file_size = 5M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50


[mysqldump]
quick
max_allowed_packet = 16M


[mysql]
no-auto-rehash


[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M


[mysqlhotcopy]
interactive-timeout

你可能感兴趣的:(数据库,mysql)