由於需要在Windows server上的Docker中部署mysql服務,為了方便起見所以在Docker hub找到了nanoserver/mysql (https://hub.docker.com/r/nanoserver/mysql)
pull下鏡像,啟動container
1 docker pull nanoserver/mysql 2 docker run --name mysqltest --hostname mysqltest1 --expose=3306 --network=my-transparent-network --ip="192.168.1.50" -dit nanoserver/mysql:latest powershell
不過在實際使用中發現,數據庫的密碼和作者寫的並不一致,使用下面的用戶和密碼沒法登入到mysql中。。。
為今之計只好跳過用戶驗證登入mysql了
1 docker attach mysqltest 2 powershell 3 Service-Stop mysql 4 Start-Job -Command {mysqld -nt --skip-grant-tables} #由於 mysqld -nt --skip-grant-tables會啟動一個mysql服務並且掛起當前的session因此需要啟動一個后台Job來執行,否則就沒辦法在當前session登入mysql了 5 mysql -uroot -p
這樣就跳過密碼驗證登入到mysql中了。
接下來修改root用戶的密碼
1 -- 切換到mysql數據庫 2 use mysql; 3 -- user表中password字段已被authentication_string代替 4 update user set authentication_string=password('1111') where user='root'; 5 -- 刷新權限 6 flush privileges;
這樣root用戶的密碼就修改好了。退出mysql終端,停止剛才啟動的mysqld Job。重啟mysql service。即可使用root身份正常登入到mysql了。
附Mysql 新建、修改用戶以及權限分配相關命令:
1 ALTER USER testuser IDENTIFIED BY '123456' PASSWORD EXPIRE NEVER; 2 -- 查看系統用戶 3 select Host,User,Password from mysql.user; 4 -- 創建一個遠程用戶 5 create user test identified by '123456'; 6 -- 分配權限 7 grant all privileges on *.* to 'test'@'%'identified by '123456' with grant option; 8 -- 刷新mysql用戶權限 9 flush privileges ; 10 -- 修改指定用戶密碼 11 update mysql.user set password=password('新密碼') where User="test" and Host="localhost"; 12 -- 刪除用戶 13 delete from user where User='test' and Host='localhost';