連接mysql報錯 1045-Access denied for user ‘root’@‘localhost’(using password: YES) mysql 5.7修改密碼 Linux

最近準備學習web項目需要用到數據庫,正好之前用公司的電腦裝過MySQL,試了一下發現mysql本機用localhost 能連接,但是其他機器用IP卻連接不上,在網上看了一下解決方案入下:

navicat連接數據庫錯誤提示:1045-Access denied for user ‘root’@‘localhost’(using password: YES)

原因:可能有以下原因

1.root帳戶默認不開放遠程訪問權限
2.密碼錯誤

解決方案:

開放mysql的訪問

  • 用命令後方式進入MySQL ;Linux是用命令:mysql -u root -p (如果本機都無法進入mysql則說明是密碼錯誤,查看第二步)
  • 輸入密碼在Enter password: ******(自己的密碼)
  • 打開 mysql 數據庫:use mysql(因爲MySQL的權限存儲在這個裏面)
  • 將user='root'的用戶訪問權限爲all:update user set host='%' where user='root';
  • 讓賦予的權限立即生效:flush privileges

完整流程入下:

[root@VM_0_4_centos ~]# mysql -u root -p
Enter password: ******
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.15 MySQL Community Server - GPL

Copyright (c) 2000, 2019, 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> use mysql
Database changed

mysql> update user set host='%' where host= 'localhost';
Query OK, 4 rows affected (0.01 sec)
Rows matched: 4  Changed: 4  Warnings: 0

mysql> select host,user from user;
+------+------------------+
| host | user             |
+------+------------------+
| %    | mysql.infoschema |
| %    | mysql.session    |
| %    | mysql.sys        |
| %    | root             |
+------+------------------+
4 rows in set (0.00 sec)


mysql> flush   privileges ;
Query OK, 0 rows affected (0.01 sec)

注意最後一步如果沒執行可能會導致修改後成功但還是訪問不了

如果確認以上操作完成了,還是拒絕訪問
則嘗試修改MySQL密碼:

2.密碼錯誤


[root@VM_0_4_centos ~]# vim /etc/my.cnf ## 編輯my.cnf 文件,設置免密登陸
[root@VM_0_4_centos ~]# cat /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

[mysqld]
#
# 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

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
# skip-grant-tables # 跳過權限驗證 (登陸時不需要密碼)
max_allowed_packet=20M #server接受的數據包大小 解決錯誤: django.db.utils.InternalError: (1153, "Got a packet bigger than 'max_allowed_packet' bytes")
[root@VM_0_4_centos ~]# service mysqld restart # 重啓服務
Redirecting to /bin/systemctl restart mysqld.service

[root@VM_0_4_centos ~]# mysql -u root -p # 進入MySQL
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.28 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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> use mysql; # 進入 mysql庫
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A


mysql> update user set authentication_string=password('123456') where user='root'; #更新密碼
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 1

mysql> flush privileges; # 刷新權限
Query OK, 0 rows affected (0.00 sec)

#重新編輯 my.cnf 文件,取消密碼登陸。然後重啓服務

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章