释放双眼,带上耳机,听听看~!
语法:
- 运行一:
1
2 1docker run -p 3307:3306 --name mysql5 -e MYSQL_ROOT_PASSWORD=123456 -d hub.c.163.com/library/mysql
2
- 运行二
1
2 1docker run -p 3306:3306 --name mysql5.6 -v /usr/local/mysql/conf:/etc/mysql/ -v /usr/local/mysql/logs:/var/log/mysql -v /usr/local/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456
2
解释:
- run启动
- -p 3307:3306 映射
- –name mysql5 起一个名字
- -v 把容器内的位置映射到linux本地机上
- -e MYSQL_ROOT_PASSWORD=123456 设置密码
- -d 后台运行
- hub.c.163.com/library/mysql 镜像名字
问题:
- show variables like '%char%'
1
2
3
4
5
6
7
8
9
10
11
12
13 1+--------------------------+----------------------------+
2| Variable_name | Value |
3+--------------------------+----------------------------+
4| character_set_client | latin1 |
5| character_set_connection | latin1 |
6| character_set_database | latin1 |
7| character_set_filesystem | binary |
8| character_set_results | latin1 |
9| character_set_server | latin1 |
10| character_set_system | utf8 |
11| character_sets_dir | /usr/share/mysql/charsets/ |
12+--------------------------+----------------------------+
13
解决:docker安装mysql需要配置my.conf,需要先去容器中将文件拷贝带本地宿主机上,然后删除原来的容器重新映射启动。
- docker pull mysql:5.7
- docker run -p 3306:3306 –name mysql5.7 -e MYSQL_ROOT_PASSWORD=123456 -d images-id 启动镜像
- docker exec -it docker-id bash 进入mysql容器
- cd /etc/mysql/
- ls -al
- cat mysql.cnf 查看mysql配置文件,他把配置文件放在另外两个路径下
- 都进去查看一番,发现/etc/mysql/mysql.conf.d 路径中有个文件,如下
- cat mysqld.cnf(红色是我配置过,正常第一次进来没有)
- 需要将容器中mysql目录下的文件全部copy出去,映射启动容器
- 退出容器exit
- /home/soft/mysql/docker/mysql目录下创建三个文件夹conf、 data、 logs
- cd conf 进入conf
- docker cp 容器-name:etc/mysql . 将mysql容器中etc/mysql目录的所有文件copy到当前目录下
- cd mysql进入mysql
- mv *.* ..将当前目录的文件移动到上一层目录
- cd .. 退回到上一层目录
- rm -rf mysql 删除mysql目录
- 修改:mysql.conf.d/mysqld.cnf文件
- 启动容器:
1
2
3
4
5
6 1docker run -p 3306:3306 --name mysql5.7
2-v /home/soft/mysql/docker/mysql/conf:/etc/mysql/ //配置
3-v /home/soft/mysql/docker/mysql/logs:/var/log/mysql //日志
4-v /home/soft/mysql/docker/mysql/data:/var/lib/mysql //数据
5-e MYSQL_ROOT_PASSWORD=123456 -d 9e64176cd8a2
6
- mysql -h 120.25.236.19 -P 3306 -uroot -p123456 进入mysql
- show variables like '%char%'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 1mysql> show variables like '%char%';
2+--------------------------+----------------------------+
3| Variable_name | Value |
4+--------------------------+----------------------------+
5| character_set_client | utf8 |
6| character_set_connection | utf8 |
7| character_set_database | utf8 |
8| character_set_filesystem | binary |
9| character_set_results | utf8 |
10| character_set_server | utf8 |
11| character_set_system | utf8 |
12| character_sets_dir | /usr/share/mysql/charsets/ |
13+--------------------------+----------------------------+
14
15