當前主機復制遠程mysql庫數據時報錯:
#mysqldump -h遠程主機 -uroot -p遠程mysql密碼 -P3306 --default-character-set=utf8 --all-databases | mysql -h127.0.0.1 -uroot -p -P3306
ERROR 1044 (42000): Access denied for user 'root'@'localhost'
解決辦法: 給localhost開放權限
#mysql -uroot -p
輸入密碼
#查看各root賬號的權限:
mysql> select current_user() from dual;
+----------------+
| current_user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
mysql> select host,user from user where user='root';
ERROR 1046 (3D000): No database selected
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> select host,user from user where user='root';
+-----------+------+
| host | user |
+-----------+------+
| localhost | root |
+-----------+------+
1 row in set (0.00 sec)
mysql> show grants for root@'localhost';
+---------------------------------------------------------------------+
| Grants for root@localhost |
+---------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION |
+---------------------------------------------------------------------+
2 rows in set (0.00 sec)
mysql> show grants for root@'127.0.0.1';
ERROR 1141 (42000): There is no such grant defined for user 'root' on host '127.0.0.1'
mysql> show grants for root@'%';
ERROR 1141 (42000): There is no such grant defined for user 'root' on host '%'
mysql> show grants for root@'xxx.xxx.xxx.xxx';
ERROR 1141 (42000): There is no such grant defined for user 'root' on host 'xxx.xxx.xxx.xxx'
如上所示,root@localhost賬號沒有訪問權限及WITH GRANT OPTION選項,關於WITH GRANT OPTION選項,如果想讓授權的用戶,也可以將這些權限授予給其他用戶,需要選項 “WITH GRANT OPTION“ 。也就是說有這個選項就可以將權限傳遞給第三方。這也是上面root@localhost用戶給其它用后授權報錯的原因,如果以 root@127.0.0.1登錄(此賬號擁有WITHGRANT OPTION選項),創建用戶並授權就不會有這個錯誤,如下所示:
#給IP-xxx.xxx.xxx.xxx賦予遠程訪問權限:
# mysql -h127.0.0.1 -u root
(需先設置密碼:
mysql> set global validate_password_length=6;
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)
mysql> set password for root@localhost = password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec) )
設置密碼的直接輸入:
# mysql -h127.0.0.1 -u root -p
>grant all privileges on *.* to root@"127.0.0.1" identified by "密碼" WITH GRANT OPTION;
#生效設置:
>FLUSH PRIVILEGES;