1.安装MysqL5.6
docker run MysqL:5.6
等所有项目都是Download complet 安装完毕
5.6: Pulling from library/MysqL
10a267c67f42: Pull complete
c2dcc7bb2a88: Pull complete
17e7a0445698: Pull complete
9a61839a176f: Pull complete
d4657fda01d9: Pull complete
c5278a445e5d: Pull complete
197c0b76f238: Pull complete
f54c5432bac4: Pull complete
c26b15abee8a: Pull complete
80dbe6022c18: Pull complete
435678c09612: Pull complete
Digest: sha256:84372107beb1de34bd4673049ea61bc35f374a2b4f19fd5a3abd4d0a2a50b715
Status: Downloaded newer image for MysqL:5.6
error: database is uninitialized and password option is not specified
You need to specify one of MysqL_ROOT_PASSWORD,MysqL_ALLOW_EMPTY_PASSWORD and MysqL_RANDOM_ROOT_PASSWORD
2.使用本地账号创建一个文件夹用来映射docker中MysqL的配置文件
其中继续创建conf、data、logs 3个文件夹
我的配置如下
val@val-linux:~/docker/MysqL1$ pwd
/home/val/docker/MysqL1
val@val-linux:~/docker/MysqL1$ ls
conf data logs
3.创建MysqL容器
切换到root账号进入到刚才创建的MysqL1文件夹中
root@val-linux:/home/val# cd /home/val/docker/MysqL1/
root@val-linux:/home/val/docker/MysqL1#
创建并启动容器
docker run -p 33061:3306 --name MysqL1 -v $PWD/conf:/etc/MysqL -v $PWD/logs:/logs -v $PWD/data:/MysqL_data -e MysqL_ROOT_PASSWORD=123456 -d MysqL:5.6
用本地33061端口映射docker的3306端口
给容器命名MysqL1
用本地/home/val/docker/MysqL1/conf映射MysqL的conf
用本地/home/val/docker/MysqL1/logs映射MysqL的logs
用本地/home/val/docker/MysqL1/data映射MysqL的MysqL_data
给root设置密码123456
4.启动MysqL容器
docker start MysqL1
5.进入MysqL的bash
docker exec -it MysqL1 env LANG=C.UTF-8 bash
其中 env LANG=C.UTF-8 bash 让docker命令行支持中文
6.复制配置文件
cp /usr/my.cnf /etc/MysqL/my.cnf
MysqL -u root -p
输入密码进入
8.查看MysqL编码
MysqL> show variables like '%character%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/MysqL/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.01 sec)
发现不是utf-8编码
MysqL> exit
Bye
root@8b83a30acbf8:/# exit
exit
root@val-linux:/home/val/docker/MysqL1#
root@val-linux:/home/val/docker/MysqL1# vim conf/my.cnf
按i进入编辑模式
加入
[client] default-character-set=utf8 [MysqL] default-character-set=utf8 [MysqLd] character-set-server=utf8
esc :wq! 保存退出
10.重启docker的MysqL再次查看编码
root@val-linux:/home/val/docker/MysqL1# docker stop MysqL1
MysqL1
root@val-linux:/home/val/docker/MysqL1# docker start MysqL1
MysqL1
root@val-linux:/home/val/docker/MysqL1# docker exec -it MysqL1 env LANG=C.UTF-8 bash
root@8b83a30acbf8:/# MysqL -u root -p
Enter password:
Welcome to the MysqL monitor. Commands end with ; or \g.
Your MysqL connection id is 1
Server version: 5.6.36 MysqL Community Server (GPL)
Copyright (c) 2000,2017,Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MysqL> show variables like '%character%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/MysqL/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
11.创建数据库
MysqL> create database rise;
Query OK,1 row affected (0.00 sec)
MysqL> use rise;
Database changed
12.创建用户并授权
MysqL> grant all privileges on rise.* to val@'%' identified by '123456';
Query OK,0 rows affected (0.00 sec)
MysqL> flush privileges;
Query OK,0 rows affected (0.00 sec)
%表示任意IP(可以指定IP)、val是用户名、123456是密码
ok完工