How can I create and load a second database in ddev?

2 min read 06-10-2024
How can I create and load a second database in ddev?


Creating and Loading a Second Database in DDEV: A Guide for Developers

DDEV is a powerful tool for local development, but managing multiple databases within a single project can sometimes feel like navigating a tangled web. This article will guide you through the process of creating and loading a second database within your DDEV environment, making multi-database scenarios much easier.

Scenario: Imagine you're building a website with a primary database for user data and a separate database for storing product information. DDEV's default setup comes with a single database, which is great for basic projects. But for complex applications, you'll need a way to manage multiple databases efficiently.

Original Code:

DDEV's configuration files, typically located in the .ddev directory, provide the foundation for defining your development environment. The default configuration will include settings for a single database.

# .ddev/config.yaml
...
db_type: mysql
db_name: my_app
...

Adding a Second Database:

  1. Modify the config.yaml file: Add a new database definition to the config.yaml file. You'll need to specify a unique name and any additional settings like the database type.

    # .ddev/config.yaml
    ...
    db_type: mysql
    db_name: my_app
    db_2_type: mysql
    db_2_name: products
    ...
    
  2. Create the Database:

    • Run ddev start to start the DDEV environment. This will create the first database (my_app) automatically.

    • Connect to the DDEV database server using a tool like phpMyAdmin or your preferred SQL client.

    • Execute the following command to create the second database (products):

      CREATE DATABASE products;
      
  3. Connect to the Second Database: You can connect to the new database in your application using the appropriate configuration settings. For example, in PHP using PDO:

    // Connect to the primary database (my_app)
    $dsn = "mysql:host=db;dbname=my_app";
    $pdo = new PDO($dsn, 'db', 'db');
    
    // Connect to the second database (products)
    $dsn2 = "mysql:host=db;dbname=products";
    $pdo2 = new PDO($dsn2, 'db', 'db');
    

Insights:

  • Naming Conventions: It's important to use descriptive and unique names for your databases. This will make your code easier to understand and maintain.
  • Database Type: DDEV supports a wide range of database types (MySQL, PostgreSQL, etc.). Choose the type that best suits your project's needs.
  • Data Import: If your second database already has existing data, you can import it into the DDEV environment using tools like mysqldump or other database migration tools.

Additional Value:

This article provides a practical guide for creating and loading a second database in DDEV. By understanding this process, developers can build complex applications with multiple data sources, increasing their flexibility and scalability.

Further Resources: