# Ubuntu部署Django 4專案系列-MySQL 8.0空資料庫創建 > [name=Sharon Mai] [time=20230508, Mon] [color=#907bf7] ## ↪️總目錄:[Ubuntu部署Django 4專案全系列](https://hackmd.io/@ppp300a/ubuntu-django-4/%2FCJjUaV-TQYyf6DSntZiFsA) {%hackmd a0ktF64jQXalgAyZHtiXbQ %} ## 安裝 ``` sudo apt install mysql-server ``` ``` sudo apt install mysql-client ``` - 相關庫安裝 ``` sudo apt install libmysqlclient-dev ``` ## 常用命令 - 啟動MySQL ``` sudo /etc/init.d/mysql start ``` - 停止運行MySQL ``` sudo /etc/init.d/mysql stop ``` - 重新運行MySQL ``` sudo /etc/init.d/mysql restart ``` - 狀態顯示 ``` systemctl status mysql.service ``` 正常運行如下圖: ![](https://hackmd.io/_uploads/SJ5m9FLE3.png) ## 登入MySQL ``` sudo mysql -u root -p ``` ## Django專案接上MySQL 1. 進入虛擬環境⏩[詳細步驟可查看此文](https://hackmd.io/@ppp300a/ubuntu-django-4/%2Ft1KY6gxtT1GaZ8yY3SqP1g#%E9%80%B2%E5%85%A5%E3%80%81%E5%95%9F%E5%8B%95%E8%99%9B%E6%93%AC%E7%92%B0%E5%A2%83) 2. 進到manage.py同層級 3. 執行ORM命令確認是否接上MySQL ``` python3 manage.py makemigrations yingmiai_app ``` ``` python3 manage.py migrate ``` 4. 沒有任何錯誤即恭喜成功!!! ## Error錯誤處置 ### 完全刪除再次安裝 1. 查看mysql相關服務 ``` dpkg --list | grep mysql ``` 2. 複製並使用apt remove 刪除 ![](https://hackmd.io/_uploads/H1wYy5I4h.png) ``` sudo apt-get remove libmysqlclient-dev ``` 3. 繼續刪除 ``` sudo apt autoremove --purge mysql-apt-config ``` 4. 刪除設定檔 ``` rm -rf /etc/mysql rm -rf /var/lib/mysql ``` 5. 刪除使用者 ``` killall -9 mysql userdel mysql ``` 參考連結:https://www.gushiciku.cn/pl/pw03/zh-tw、https://noob.tw/remove-mysql-completely/ ### 設定密碼時錯誤 >MySQL Failed! Error: SET PASSWORD has no significance for user ‘root’@’localhost’ as the authentication method used doesn’t store authentication data in the MySQL server. 原先終端開著不要關,另開新的終端視窗處理安全性設定: 1. 使用安全性設定 ``` killall -9 mysql userdel mysql ``` 2. 將會出現詢問,相關設定如下: ``` Press y|Y for Yes, any other key for No: y There are three levels of password validation policy.....(使用密碼強度校驗元件) 輸入: 0 New Password:(設定新密碼,並重復一遍) Remove anonymous users (刪除匿名使用者) n Disallow root login remotely(拒絕遠端root賬號登入) n Remove test database and access to it(移除test資料庫) n Reload privilege tables now (現在就重新載入許可權表) y ``` 參考連結:https://www.nixcraft.com/t/mysql-failed-error-set-password-has-no-significance-for-user-root-localhost-as-the-authentication-method-used-doesnt-store-authentication-data-in-the-mysql-server-please-consider-using-alter-user/4233 參考連結:https://www.gushiciku.cn/pl/pw03/zh-tw ### 需要跳過授權表 >ERROR 1064 (42000): You have an error in your SQL syntax; Want to configure a password as root being the user 1. 關閉mysql運行 ``` sudo /etc/init.d/mysql start ``` 2. 到etc/mysql/my.cnfd/mysql.cnf 3. 改文件權限 mod 777 改權限可參考此文⏩[可能出現的錯誤訊息-權限](/EewUJQYrStyx7gFSNRvWDw) ``` sudo chmod -R 777 工作目錄 ``` 4. 編輯文件 ``` nano mysql.cnf ``` 5. 貼上"跳過授權表"命令 ``` skip-grant-tables; ``` 6. 把權限改回 644 7. 設定新密碼 [查看上方的密碼相關](#設定密碼時錯誤) 8. 啟動mysql > 確認成功 ``` sudo /etc/init.d/mysql start ``` 參考網站:https://stackoverflow.com/questions/36099028/error-1064-42000-you-have-an-error-in-your-sql-syntax-want-to-configure-a-pa ### 遷移出現錯誤 >django.db.utils.OperationalError: no such table: auth_test_usertranslatorprofile 出現找不到路徑的錯誤提示 - 確認遷移時路徑是否在manage.py同層級 - 刪除djanog專案下所有子層級的pychech資料夾 - 將migrations資料夾內的所有遷移資料刪除 參考網站:https://stackoverflow.com/questions/34548768/no-such-table-exception