Editing
排除 MySQL 技術疑難問題
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== 訪問和認證錯誤 == === [Warning] Using a password on the command line interface can be insecure === 問題描述: 使用 bash 命令匯出或匯入 MySQL 資料庫時,可能會遇到以下警告:[警告] 在命令行介面上使用密碼可能不安全。 <pre> mysql: [Warning] Using a password on the command line interface can be insecure. or mysqldump: [Warning] Using a password on the command line interface can be insecure. </pre> 這通常發生在使用如下命令時: <pre> mysqldump -u USERNAME -p -h 127.0.0.1 --default-character-set=utf8 DATABASE > DATABASE.sql </pre> 解決方案:要解決此警告並提高安全性,請按照以下步驟操作: # 使用 [https://dev.mysql.com/doc/refman/8.4/en/mysql-config-editor.html mysql_config_editor] 設定登入路徑 (login path):{{kbd | key=<nowiki>mysql_config_editor set --login-path=local --host=localhost --user=root --password</nowiki>}} # 匯出或匯入 MySQL 資料庫時使用登入路徑 (1) 匯出時:{{kbd | key=<nowiki>mysqldump --login-path=local --default-character-set=utf8 資料庫名稱 > 資料庫名稱.sql</nowiki>}} (2) 匯入時:{{kbd | key=<nowiki>pv 資料庫名稱.sql.gz | gunzip | mysql --login-path=local --default_character_set utf8 --force 資料庫名稱</nowiki>}} # 查看所有已設定的登入路徑 {{kbd | key=<nowiki>mysql_config_editor print --all</nowiki>}} 透過使用登入路徑,可以避免在命令行中暴露密碼,解決安全警告。 === 錯誤 1044 (42000): 拒絕使用者 'USER'@'localhost' 存取資料庫 'DATABASE_NAME' === 訊息:{{kbd | key=<nowiki>錯誤 1044 (42000): 拒絕使用者 'USER'@'localhost' 存取資料庫 'DATABASE_NAME'</nowiki>}} <pre> pv file.sql.gz | gunzip | mysql -u USER -p --host=127.0.0.1 --default_character_set utf8 DATABASE_NAME </pre> 解決方案: * 檢查指定使用者名稱和資料庫名稱的權限 <pre> $ mysql -u USER -p --host=127.0.0.1 輸入密碼: mysql> use DATABASE_NAME </pre> 訊息:{{kbd | key=<nowiki>mysqldump: 執行 LOCK TABLES 時出錯: 1044: 拒絕使用者 'USER'@'localhost' 存取資料庫 'DATABASE_NAME'</nowiki>}} <pre> $ mysqldump -h 127.0.0.1 -u USER -p DATABASE_NAME TABLE_NAME > TABLE_NAME.sql </pre> 解決方案: * 如果無法給予使用者權限,添加 mysqldump 選項 {{kbd | key=<nowiki>--skip-lock-tables</nowiki>}} <ref>[https://stackoverflow.com/questions/7415698/skip-lock-tables-and-mysqldump mysql - 跳過鎖定表和 mysqldump - Stack Overflow]</ref>。 <pre> $ mysqldump -h 127.0.0.1 -u USER -p --skip-lock-tables DATABASE_NAME TABLE_NAME > TABLE_NAME.sql </pre> === 錯誤 1827 (HY000): 密碼哈希不符合預期格式。檢查是否使用了正確的密碼算法與 PASSWORD() 函數。 === 錯誤的 SQL 查詢如下: <pre> CREATE USER 'test'@'localhost' IDENTIFIED BY PASSWORD 'my_password'; </pre> 解決方案: (1) 檢查帳戶是否已創建 <pre> SELECT User,Host FROM mysql.user; </pre> (2a) 如果帳戶已創建,為該帳戶設定密碼。 <pre> SET PASSWORD FOR 'test'@'localhost' = PASSWORD('my_password'); </pre> (2b) 如果帳戶未創建,重新創建帳戶。 <pre> CREATE USER 'test'@'localhost' IDENTIFIED BY 'my_password'; </pre> 參考資料 * [http://dev.mysql.com/doc/refman/5.7/en/create-user.html MySQL :: MySQL 5.7 參考手冊 :: 14.7.1.2 CREATE USER 語法] * [http://dev.mysql.com/doc/refman/5.7/en/set-password.html MySQL :: MySQL 5.7 參考手冊 :: 14.7.1.7 SET PASSWORD 語法] * [http://dev.mysql.com/doc/refman/5.7/en/drop-user.html MySQL :: MySQL 5.7 參考手冊 :: 14.7.1.3 DROP USER 語法] === 錯誤 1045 (28000): 拒絕使用者存取 === 訊息:錯誤 1045 (28000): 拒絕使用者 'user'@'localhost' 存取(使用密碼:YES) 解決方案: * 檢查使用者名稱的打字錯誤。 * 檢查密碼的打字錯誤。 * 如果您正在使用控制台命令,如果密碼包含特殊字元,請轉義密碼,例如 {{kbd | key=<nowiki>mysql -u root -p'PASSWORD'</nowiki>}}<ref>[http://superuser.com/questions/123928/escaping-a-password-using-mysqldump-console 使用 mysqldump 控制台轉義密碼 - Super User]</ref> * 您可能需要刪除現有的帳戶設定並重新配置。 === 錯誤!: SQLSTATE[28000]: 無效的授權規格:1045 拒絕訪問 === 訊息:錯誤!: {{kbd | key=<nowiki>SQLSTATE[28000]: 無效的授權規格:1045 拒絕使用者 'user'@'localhost' 存取(使用密碼:YES)</nowiki>}} 解決方案: * 如果您執行了查詢 {{kbd | key=INTO OUTFILE}},您需要授予文件權限,例如:{{kbd | key=GRANT FILE ON *.* TO 'user'@'localhost';}}<ref>[https://dev.mysql.com/doc/refman/5.7/en/privileges-provided.html#priv_file MySQL :: MySQL 5.7 參考手冊 :: 6.2.1 提供的權限]</ref><ref>[https://stackoverflow.com/questions/6091427/mysql-into-outfile-access-denied-but-my-user-has-all-access-and-the-fold MYSQL into outfile "拒絕訪問" - 但我的使用者有 "全部" 訪問權限.. 且文件夾是 CHMOD 777 - Stack Overflow]</ref>。 相關問題:"錯誤!: SQLSTATE[HY000]: 一般錯誤:1290 MySQL 伺服器正在使用 --secure-file-priv 選項運行,所以它不能執行這個聲明"<ref>[https://stackoverflow.com/questions/32737478/how-should-i-tackle-secure-file-priv-in-mysql 資料庫 - 如何處理 MySQL 中的 --secure-file-priv? - Stack Overflow]</ref>
Summary:
Please note that all contributions to LemonWiki共筆 are considered to be released under the Creative Commons Attribution-NonCommercial-ShareAlike (see
LemonWiki:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Current events
Recent changes
Random page
Help
Categories
Tools
What links here
Related changes
Special pages
Page information