mysql - dockerfile mysql安装链接数据目录

标签 mysql docker ubuntu

我需要用 ubuntu 和 mysql 创建 docker 文件(不是单独的容器)
当我添加

apt-get install -y mysql-server 
在 dockerfile
安装程序创建 var/lib/mysql 并将数据放在这里。
所以当我在 docker-compose 中配置它时
volumes:
    - ./var/mysql:/var/lib/mysql
并在入口点尝试创建数据库
/usr/bin/mysql -u root -e "CREATE DATABASE IF NOT EXISTS ${MYSQL_DATABASE};"
    /usr/bin/mysql -u root -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY '${MYSQL_ROOT_PASSWORD}';"
./var/mysql 是空目录
mysql不启动
那么如何正确地做到这一点呢?

最佳答案

您想在一个 Docker 容器中结合 Ubuntu 和 MySQL。
但最好的方法是按原样使用 Docker,将这种应用程序拆分到它们自己的容器中。
创建一个 Ubuntu 容器 en 创建一个 MySQL 容器,并让 Ubuntu 访问 MySQL。
请参见下面的示例:
docker -compose.yml
在文件夹中创建一个 docker-compose.yml。

version: '3.9'

services:

  ubuntu:
    build: .
    image: ubuntusql
    container_name: ubuntu
    command: ["sleep", "300d"]

  mysqlcontainer:
    image: mysql
    container_name: mysql
    environment:
      MYSQL_ROOT_PASSWORD: example
    volumes:
      - ./database:/var/lib/mysql
      - ./mysql_init:/docker-entrypoint-initdb.d
    ports:
      - "33060:33060"
      - "3306:3306"
Dockerfile
在同一个文件夹中,创建一个 Dockerfile 来创建您自己的 Ubuntu 镜像。
您自己的图像的原因是您希望 mysql_client 在 Ubuntu 中可用
FROM ubuntu

WORKDIR /root

RUN apt-get update && apt-get upgrade \
    && apt-get --assume-yes install mysql-client
mysql_init.sql
创建一个名为 mysql_init 的子文件夹并在那里创建一个 mysql_init.sql 文件。
(请参阅 docker-compose.yml,它希望该文件可用)
CREATE DATABASE IF NOT EXISTS exampledb;

USE exampledb;

CREATE USER dbuser IDENTIFIED BY 'dbpassword';

GRANT ALL PRIVILEGES ON exampledb.* TO dbuser ;

CREATE TABLE contacts (
    name VARCHAR(255),
    address VARCHAR(255)
);

INSERT INTO contacts (name, address) VALUES ('John', 'Street');
INSERT INTO contacts (name, address) VALUES ('Mary', 'Another Street');
INSERT INTO contacts (name, address) VALUES ('Donald', 'No Street');
数据库文件夹
创建一个名为“数据库”的子文件夹。
docker-compose.yml 期望它在那里,MySQL Docker 容器将在该文件夹中创建数据库。
启动 Docker-Compose
docker compose up
从 Ubuntu 进行测试
进入 Ubuntu 容器:
docker exec -it ubuntu /bin/bash
在 Ubuntu Container 中,启动 MySQL 并检查是否可以访问 MySQL 数据库和数据
$ docker exec -it ubuntu /bin/bash
root@10c8ef06a399:~# mysql -h mysql -u dbuser -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.25 MySQL Community Server - GPL

Copyright (c) 2000, 2021, 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> use exampledb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+---------------------+
| Tables_in_exampledb |
+---------------------+
| contacts            |
+---------------------+
1 row in set (0.00 sec)

mysql> select * from contacts;
+--------+----------------+
| name   | address        |
+--------+----------------+
| John   | Street         |
| Mary   | Another Street |
| Donald | No Street      |
+--------+----------------+
3 rows in set (0.01 sec)

mysql> exit
Bye
root@10c8ef06a399:~# exit
exit
$ 

关于mysql - dockerfile mysql安装链接数据目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68420429/

相关文章:

java - 相同的 tomcat 版本,但行为不同,为什么?

java - 尽管 Ubuntu 上的用户名和密码正确,但 jcifs.smb.SmbAuthException

linux - 使用 SSHFS 在 Windows 10 中挂载 Linux FS

python - 进程监控 - Python/Ubuntu/Linux

mysql - ASP.NET MVC 4、代码优先、MySQL

php - 数据库/服务器设计(Cookie 和索引)- 整数还是文本?

mysql - 'MySQL STR_TO_DATE 字符串中没有日期

docker - Console.ForegroundColor/BackgroundColor 未反射(reflect)在 Docker 日志中

php - JQuery 和 PHP 查找字段

python - 使用 Django、npm 和 gulp 创建 Docker 容器