在 cPanel 上,當我以 root 身分登入並鍵入「mysql」(不含主機名稱和密碼)時,它使我可以直接存取 mysql root 使用者。
我想為我的一台非 cpanel 伺服器執行此操作,其中 linux root 用戶以與 cPanel 上相同的方式獲得 mysql root 用戶的無密碼登入。
這可能嗎 ?
答案1
最簡單的方法是使用 ~/.my.cnf 檔案的客戶端部分,並在其中新增憑證。
[client]
user=root
password=somepassword
...
最好也讓該檔案只能由 root 讀取。
答案2
對於mysql 5.7+,如果您在初始設定時設定空密碼,mysql將自動使用auth_socket
作為政策。嘗試在不更改策略的情況下更新密碼將不會有結果。如果您的使用者是 ,您始終可以不使用密碼登入root
。
解決方案 執行以下命令更改身份驗證策略並設定密碼
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY ''
參考:https://www.percona.com/blog/2016/03/16/change-user-password-in-mysql-5-7-with-plugin-auth_socket/
答案3
從 MySQL 5.6.6 開始,您可以使用 mysql_config_editor 建立一個加密文件,該文件將自動登入:
mysql_config_editor set --login-path=client --host=localhost --user=root --password
然後在出現提示時輸入密碼。
注意:如果您的密碼中包含“#”,並且可能包含其他字符,請在輸入密碼時使用單引號。
答案4
建立一個僅包含 mysql root 密碼且只有 root 可以讀取的檔案。
# ls -l /root/.mysqlpw
-rw------- 1 root root 7 2013-08-19 13:24 /root/.mysqlpw
您可以使用以下命令導入資料庫
# mysql -u root -p`cat /root/.mysqlpw ` yourdatabase < databasedump.sql
或連接到您的資料庫並發出 mysql 命令
# mysql -u root -p`cat /root/.mysqlpw ` yourdatabase
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 20460
Server version: 5.1.63-0ubuntu0.10.04.1 (Ubuntu)
Copyright (c) 2000, 2011, 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| yourdatabase |
+--------------------+
3 rows in set (0.00 sec)
mysql> show tables;
...