基于 Docker 搭建 Mysql 环境

206 阅读2分钟

配置 Docker 环境

我这里直接安装Docker desktop

有些Mac版本和docker版本会不兼容,所以这一步骤需要根据自己的Mac版本找到对应的Docker版本,以下是各个版本地址

docs.docker.com/desktop/rel…

配置 Mysql 环境

MySQL环境我直接用docker命令来安装

首先检查一下docker是否安装成功

➜  ~ docker --version
Docker version 24.0.6, build ed223bc

Docker 创建一个数据存储空间

➜  ~ docker volume create demo-vol
demo-vol

Docker 安装mysql环境

➜  ~ docker run --name demo-mysql \
        --restart=always \
        -v demo-vol:/var/lib/mysql \
        -p 3306:3306 \
        -e MYSQL_ROOT_PASSWORD=root \
        -e MYSQL_DATABASE=demo \
        -e MYSQL_USER=admin \
        -e MYSQL_PASSWORD=123456 \
        -d mysql:8.0.18

第一次安装时间会比较久,安装完成之后docker就自动启动了这个 mysql 的容器

➜  ~ docker ps
CONTAINER ID   IMAGE          COMMAND                   CREATED          STATUS          PORTS                               NAMES
4640b067171e   mysql:8.0.18   "docker-entrypoint.s…"   25 seconds ago   Up 23 seconds   0.0.0.0:3306->3306/tcp, 33060/tcp   demo-mysql

进入mysql容器

➜  ~docker exec -it 4640b067171e /bin/bash
root@4640b067171e:/#

登录mysql

root@4640b067171e:/# mysql -uadmin -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.18 MySQL Community Server - GPL

Copyright (c) 2000, 2019, 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> 

常用的mysql命令

执行mysql命令的时候有个需要特别注意的是命令结束位置需要有分号

查看database列表

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| demo               |
+--------------------+
2 rows in set (0.01 sec)

mysql>

指明使用的数据库

mysql> use demo
Database changed

创建table

mysql> create table test(id int not null auto_increment,title varchar(100) not null,author varchar(40) not null,primary key (id));
Query OK, 0 rows affected (0.02 sec)

查看数据库

mysql> show tables from demo;
+---------------------+
| Tables_in_demo      |
+---------------------+
| test                |
+---------------------+
1 row in set (0.00 sec)

插入一条数据

mysql>  insert into test(id,title,author)values(20,"janelove","jane");
Query OK, 1 row affected (0.01 sec)

4、查看test表信息

mysql> select * from test;
+----+----------+--------+
| id | title    | author |
+----+----------+--------+
| 20 | janelove | jane   |
+----+----------+--------+
1 row in set (0.00 sec)

查看test表的数据结构

mysql> desc test;
+--------+--------------+------+-----+---------+----------------+
| Field  | Type         | Null | Key | Default | Extra          |
+--------+--------------+------+-----+---------+----------------+
| id     | int(11)      | NO   | PRI | NULL    | auto_increment |
| title  | varchar(100) | NO   |     | NULL    |                |
| author | varchar(40)  | NO   |     | NULL    |                |
+--------+--------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

修改表字段类型

mysql> alter table test modify title varchar(22);

增加表字段

mysql> alter table test add column age int(3);

删除表字段

mysql> alter table test drop column age;

删除表

mysql> drop table test;
Query OK, 0 rows affected (0.02 sec)

mysql>

删除数据库

mysql> drop database demo;