Create DB in DDEV

User
create db ddev
Assistant
To create a database using **DDEV**, you don’t need to manually create it since DDEV automatically sets up a database for your project when you start the environment. However, you can access and manage the database as follows:

### 1. **Start Your DDEV Project**
If you haven’t already started your DDEV project, initialize it and start it:

ddev config
ddev start

### 2. **Access the Database**
– **Credentials:**
DDEV sets up a default database with credentials:
– **Database Name:** `db`
– **Username:** `db`
– **Password:** `db`
– **Host:** `db` (from within the container)

– **phpMyAdmin (Optional):**
Access `phpMyAdmin` (pre-installed in DDEV) by visiting [http://127.0.0.1:8036](http://127.0.0.1:8036).

– **Connect via Command Line:**
Run the following command to open a MySQL shell:

ddev mysql

### 3. **Create a New Database (If Needed)**
If you need to create a separate database within the DDEV environment, you can do so using:

1. Open MySQL shell:

ddev mysql

2. Run the SQL command to create a new database:

CREATE DATABASE your_database_name;

### 4. **Connect Your Application**
Use the credentials above to configure your application (e.g., Laravel `.env` file, WordPress wp-config, etc.).

Let me know if you need assistance configuring DDEV further!

User
admin1@PC-274:~/ddev-projects/shareyourthoughts$ ddev mysql
Welcome to the MariaDB monitor. Commands end with ; or g.
Your MariaDB connection id is 15
Server version: 10.11.10-MariaDB-ubu2204-log mariadb.org binary distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

MariaDB [db]> create DATABASE shareyourthoughts;
ERROR 1044 (42000): Access denied for user 'db'@'localhost' to database 'shareyourthoughts'

Assistant
The error occurs because the default `db` user in DDEV does not have the privileges to create new databases. By default, DDEV configures a single database (`db`) with specific permissions for the `db` user, and creating additional databases requires elevated privileges.

Here’s how to handle this:

### Solution 1: Use the Default `db` Database
Since DDEV provides a single database (`db`) by default, you can use it instead of creating a new one. Configure your application to use the existing `db` database.

### Solution 2: Grant Privileges for Database Creation
You can temporarily escalate privileges to the root user to create the database:

1. **Access MariaDB as the Root User:**
Run the following command to access the database as the `root` user:

ddev mysql -uroot

2. **Create the Database:**
Once logged in as `root`, create the new database:

CREATE DATABASE shareyourthoughts;

3. **Grant Privileges (Optional):**
If your application will use this new database, ensure the `db` user has access:

GRANT ALL PRIVILEGES ON shareyourthoughts.* TO 'db'@'%';
   FLUSH PRIVILEGES;

### Solution 3: Configure `additional_databases` in DDEV
DDEV supports creating multiple databases automatically via the `config.yaml` file.

1. Open your project's `config.yaml` file (found in the DDEV project directory).
2. Add the `additional_databases` configuration:

additional_databases:
     - shareyourthoughts

3. Restart DDEV:

ddev restart

This will create the `shareyourthoughts` database when the environment is restarted.

Let me know if you need help with any of these steps!

User
name: shareyourthoughts
type: laravel
docroot: public
php_version: "8.3"
webserver_type: nginx-fpm
xdebug_enabled: false
additional_hostnames: []
additional_fqdns: []
database:
type: mariadb
version: "10.11"
use_dns_when_possible: true
additional_databases:
-shareyourthoughts
composer_version: "2"
web_environment: []
corepack_enable: false
Change mariadb to mysql .ddev config.yaml
Assistant
To change the database type from **MariaDB** to **MySQL** in your DDEV `config.yaml`, you need to update the `database` section properly.

Here’s the corrected `config.yaml`:

name: shareyourthoughts
type: laravel
docroot: public
php_version: "8.3"
webserver_type: nginx-fpm
xdebug_enabled: false
additional_hostnames: []
additional_fqdns: []
database:
  type: mysql
  version: "8.0" # Change this to the version of MySQL you want
use_dns_when_possible: true
additional_databases:
  - shareyourthoughts
composer_version: "2"
web_environment: []
corepack_enable: false

### Steps to Apply the Change
1. **Edit the `config.yaml`:**
Open the `config.yaml` file in a text editor and replace `mariadb` with `mysql` as shown above.

2. **Restart DDEV to Apply Changes:**
Run the following commands:

ddev poweroff
   ddev start

This will stop all DDEV services, reinitialize the environment, and apply the updated configuration.

3. **Verify the Database Type:**
To confirm that the database type is now MySQL, access the database using:

ddev mysql

Or check the server version using:

SELECT VERSION();

Let me know if you need help with these steps or anything else!

User
admin1@PC-274:~/ddev-projects/shareyourthoughts$ php artisan migrate

IlluminateDatabaseQueryException

SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo for db failed: Name or service not known (Connection: mariadb, SQL: select table_name as `name`, (data_length + index_length) as `size`, table_comment as `comment`, engine as `engine`, table_collation as `collation` from information_schema.tables where table_schema = 'db' and table_type in ('BASE TABLE', 'SYSTEM VERSIONED') order by table_name)

at vendor/laravel/framework/src/Illuminate/Database/Connection.php:825
821▕ $this->getName(), $query, $this->prepareBindings($bindings), $e
822▕ );
823▕ }
824▕
➜ 825▕ throw new QueryException(
826▕ $this->getName(), $query, $this->prepareBindings($bindings), $e
827▕ );
828▕ }
829▕ }

+41 vendor frames

42 artisan:13
IlluminateFoundationApplication::handleCommand()

Assistant
The error occurs because Laravel cannot resolve the `db` host specified in the `.env` file for the database connection. This typically happens when the configuration in DDEV or Laravel is not properly aligned. Here's how to resolve the issue:

### Step 1: **Verify DDEV Database Configuration**
In DDEV, the database host should be set to `db`. Ensure your DDEV is running:

ddev start

Run the following command to confirm the database is accessible:

ddev mysql

If this works, the DDEV database is properly set up.

### Step 2: **Check Your Laravel `.env` File**
Open your Laravel `.env` file and verify the database configuration:

DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=db
DB_USERNAME=db
DB_PASSWORD=db

Ensure:
– **`DB_HOST` is set to `db`** (the internal DDEV hostname for the database).
– **`DB_CONNECTION` is set to `mysql`**.
– **The credentials match DDEV's defaults**.

### Step 3: **Clear Laravel Configuration Cache**
After updating the `.env` file, clear Laravel’s configuration cache to ensure the changes take effect:

php artisan config:clear
php artisan cache:clear

### Step 4: **Verify DDEV Database Access**
Check if the `db` database exists and is accessible:
1. Log in to the DDEV database:

ddev mysql

2. List the databases:

SHOW DATABASES;

If the `db` database does not exist, recreate it manually or check if the `additional_databases` configuration in `config.yaml` is set correctly.

### Step 5: **Run Migrations Again**
Once everything is properly configured, re-run the migrations:

php artisan migrate

Let me know if the issue persists or if you encounter another error!

Leave a Comment

Your email address will not be published. Required fields are marked *