使用Docker安裝MySQL

  • 752
  • 0
  • 2020-02-10

使用Docker安裝MySQL 

1.使用指令抓取mysql image   docker pull mysql/mysql-server

2.使用指令確認image  docker images

3.啟動container 設定名稱及密碼並指定port為3306,輸入完用docker ps -a確認

docker run --name mysql20200208 -e MYSQL_ROOT_PASSWORD=密碼 -d -p 3306:3306 mysql/mysql-server

4.使用Navicat設定連線卻顯示error,推測為權限問題

5.使用指令 docker exec -it mysql20200208 mysql -uroot -p 並輸入上一步設定的密碼

參考:Connecting to MySQL Server from within the Container遠端連線mysql失敗flush privileges刷新MySQL的系統權限相關表

輸入下列SQL

ALTER USER 'root'@'localhost' IDENTIFIED BY '密碼';
flush privileges;

update user set host = '%' where user = 'root';
flush privileges;

再次連線仍有error

或出現此error:Connection failed: SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client

解法:https://github.com/laradock/laradock/issues/1390

6.輸入SQL,參考:https://stackoverflow.com/questions/49194719/authentication-plugin-caching-sha2-password-cannot-be-loaded

ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '20200208';

再次測試連線OK

參考:

https://hub.docker.com/r/mysql/mysql-server/

https://dev.mysql.com/doc/mysql-linuxunix-excerpt/5.6/en/docker-mysql-getting-started.html

https://chartio.com/resources/tutorials/how-to-grant-all-privileges-on-a-database-in-mysql/