连接mysql 报 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
;一般这个错误是由密码错误引起,重置密码即可。
例如mysql重新初始化 后如果没有记录密码,就要重置密码才能连接:
- 记录一下mysql重新初始化步骤:先删除datadir 目录下的文件
[root@localhost ~]# whereis my.cnf ##查看my.cnf的具体路径
my: /etc/my.cnf
查看mysql的数据存放路径: more /etc/my.cnf
[mysqld]
datadir = /usr/mysql/data
- 然后执行mysql初始化操作:
/usr/local/mysql/bin/mysqld --initialize --user=mysql
修改 vi /etc/my.cnf
配置文件 连接时跳过权限检查,然后重启mysql
[mysqld]
skip-grant-tables ## 登录时跳过权限检查
在mysql 服务器上跳过密码 ,直接连接数据库:
执行sql 修改密码:
sql语句中 password(‘密码’) 函数中填想要改的密码
mysql> update mysql.user set authentication_string=password('******') where user = 'root'; -- ****** 是想要设置的密码
mysql> flush privileges;
然后执行SQL select User,authentication_string from mysql.user
查到root用户的authentication_string
这项值:*B7E2479CF6FD9FAC6534DF72AD0D5A0096D2C1B0
MySQL会给密码进行加密,设置的密码进行加密后就等于authentication_string的值
mysql> update mysql.user set authentication_string=password('******') where user = 'root';
mysql> flush privileges;
INSERT INTO user (Host, User, Select_priv, Insert_priv, Update_priv, Delete_priv, Create_priv, Drop_priv, Reload_priv, Shutdown_priv, Process_priv, File_priv, Grant_priv, References_priv, Index_priv, Alter_priv, Show_db_priv, Super_priv, Create_tmp_table_priv, Lock_tables_priv, Execute_priv, Repl_slave_priv, Repl_client_priv, Create_view_priv, Show_view_priv, Create_routine_priv, Alter_routine_priv, Create_user_priv, Event_priv, Trigger_priv, Create_tablespace_priv, ssl_type, ssl_cipher, x509_issuer, x509_subject, max_questions, max_updates, max_connections, max_user_connections, plugin, authentication_string, password_expired, password_last_changed, password_lifetime, account_locked) VALUES ('%', 'root', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', '', '', '', '', 0, 0, 0, 0, 'mysql_native_password', '*B7E2479CF6FD9FAC6534DF72AD0D5A0096D2C1B0', 'N', '2020-06-04 15:23:29', null, 'N');
如果 mysql.usr
里有password
列,可以直接执行SQL:update mysql.user set password=password("要设置的密码") where user="root";
or 接着上面更新 authentication_string后再执行 update mysql.user set password = '*B7E2479CF6FD9FAC6534DF72AD0D5A0096D2C1B0' where user = 'root';