6.下载mysql镜像
1.从阿里云拉去镜像
[root@wmd03 ~]# docker pull mysql:5.6
2. 查看拉取到的镜像
[root@wmd03 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest e1d7dc9731da 4 weeks ago 544MB
3.启动mysql镜像
[root@wmd03 ~]#
docker run -p 3306:3306 --name mysql -v /wmduse/mysql/conf:/etc/mysql/conf.d -v /wmduse/mysql/logs:/logs -v /wmduse/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=521521 -d mysql
-p 3306:3306端口映射,宿主机的3306端口对应docker的mysql镜像的3306端口
--name mysql:运行服务名字
-v /wmduse/mysql/conf:/etc/mysql/conf.d 宿主机的/wmduse/mysql/conf文件夹和容器的/etc/mysql/conf.d文件共享
-v /wmduse/mysql/logs:/logs 同理
-v /wmduse/mysql/data:/var/lib/mysql 听筒里
-e MYSQL_ROOT_PASSWORD=521521 初始化root的密码
-d mysql 后台运行mysql
4.进入交互式页面
[root@wmd03 mydocker]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
45bd91671787 mysql "docker-entrypoint.s…" 8 minutes ago Up 8 minutes 0.0.0.0:3306->3306/tcp, 33060/tcp mysql
[root@wmd03 mydocker]# docker exec -it 45bd91671787 /bin/bash(使用该命令,不能使用attach,会卡住)
5.进入mysql命令
root@dd2be772734d:/# mysql -uroot -p
Enter password:
mysql> create database db01;
-> show database
mysql> show databases
-> ;
+--------------------+
| Database |
+--------------------+
| db01 |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
mysql> use db01
Database changed
//创建表
mysql> create table user(id int not null primary key,name varchar(20))
-> ;
mysql> show tables
-> ;
+----------------+
| Tables_in_db01 |
+----------------+
| user |
+----------------+
mysql> insert into user values(1,'wmd');
Query OK, 1 row affected (0.11 sec)
mysql> select * from user;
+----+------+
| id | name |
+----+------+
| 1 | wmd |
+----+------+
6.这时连接用navicat连接会报2059:参考如下:https://blog.csdn.net/qq_40389276/article/details/98871405
1.
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'WITH GRANT OPTION;
Query OK, 0 rows affected (0.01 sec)
2.
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.41 sec)
3.
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '521521' PASSWORD EXPIRE NEVER;
Query OK, 0 rows affected (0.02 sec)
4.
mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '521521';
Query OK, 0 rows affected (0.01 sec)
5.
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec)
执行上述五步就可以了