若是不指定mysql的版本默認下載mysql8,mysql8的變化比較大,因此仍是用mysql5.7吧。html
查看可下載版本mysql
docker pull mysql:5.7sql |
查看鏡像:docker
docker imagesvim
2建立MySQL容器bash
docker run -p 3306:3306 --name mysql -v mysql/conf:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7 |
-p 表明端口映射,格式爲 宿主機映射端口:容器運行端口ui
-e 表明添加環境變量 MYSQL_ROOT_PASSWORD是root用戶的登錄密碼編碼
修改字MySQL字符集 vim mysql/conf/my.cnfspa
添加下面代碼:
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
進入mysql容器
docker exec -it mysql /bin/bash |
登錄mysql
mysql -u root -p |
mysql> show variables like'character%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
在電腦上用工具鏈接Docker裏的mysql,和日常的mysql同樣鏈接(接口和密碼看建立容器時的設定,ip就使用Docker所在Linux的ip就好)。