docker 创建运行MySQL容器

创建容器并运行:

docker run -p 3306:3306 --name mysql \
-v /mydata/mysql/log:/var/log/mysql \
-v /mydata/mysql/data:/var/lib/mysql \
-v /mydata/mysql/conf:/etc/mysql \
-e MYSQL_ROOT_PASSWORD=123456  \
-d mysql:5.7

# -p 3306:3306:将容器的3306端口映射到主机的3306端口
# -v /mydata/mysql/conf:/etc/mysql:将配置文件夹挂在到主机
# -v /mydata/mysql/log:/var/log/mysql:将日志文件夹挂载到主机
# -v /mydata/mysql/data:/var/lib/mysql/:将数据文件夹挂载到主机
# -e MYSQL_ROOT_PASSWORD=123456:初始化root用户的密码为123456

进入容器bash:

[root@localhost opt]# docker exec -it 81asdfrvf bash  # 81asdfrvf为容器编号

bash-4.2# mysql -hlocalhost -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.44 MySQL Community Server (GPL)

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

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>

posted @ 2024-08-30 14:43  二月雪  阅读(14)  评论(0编辑  收藏  举报