How can I access ddev's database with external database browsers or similar tools?
Asked Answered
C

2

6

As far as I know, DDEV provides configuration options for phpMyAdmin to comfortably work with containered MySQL/MariaDB databases. But I would rather like to use a different tool like e.g. phpStorm or DBeaver or such. Is there a way to make my wish come true?

Supplementing the docker-compose configuration I exposed port 3306, accordingly.

version: '3.6'

services:
  web:
    ports:
      - 3306:3306

Trying to connect to a containered MariaDB database from my client host looks like the following.

$ mariadb --host=foo.ddev.site --user=db --password=db --database=db
ERROR 2013 (HY000): Lost connection to MySQL server at 'handshake: reading initial communication packet', system error: 11
Costa answered 9/7, 2021 at 17:38 Comment(0)
A
12

The database port is already exposed in ddev, so can easily be used with many, many external tools.

For example, ddev mysql gives you direct access, and there are also ddev sequelpro and ddev sequelace and ddev tableplus, and there's an example in the custom commands showing how to do it with mysqlworkbench (See ~/.ddev/commands/host/mysqlworkbench.example).

Many other database browsers have direct support, see https://ddev.readthedocs.io/en/stable/users/usage/database-management/#database-guis

All of those grab the already-exposed db port.

ddev describe on any project tells you how to access the port.

The host_db_port setting in your project's .ddev/config.yaml can be used to lock down the exposed port so you can easily use it with PhpStorm.

This article also goes through some of the many ways you can access the database using ddev, https://ddev.com/blog/ddev-local-database-management/

Acadia answered 9/7, 2021 at 17:50 Comment(0)
A
-1

TL;DR ddev st -j | grep -oE '"published_port":[0-9]+' | grep -oE '[0-9]+' | tr -d '\n' | pbcopy

You can use clip.exe instead of pbcopy if running in WSL or your environment has.

You use ddev st -j for info about your containers in json. st is an alias for status which is the same as describe as far as I know and -j prints it in an inline json format.

Since this is inline and can be parsed easier, I have aliased this to copy the published_port which is at this time used only for the DB container and copy it directly to my clipboard using pbcopy on mac but you could use any package instead of pbcopy, or... you know... click and copy :P

I am sure this can be written more elegantly and precisely, but it's a start.

Alby answered 22/3 at 12:47 Comment(2)
I know this has been marked down but the grep is useful if you are scripting and want just the port number of the db to sed into your config.yml - many thanks, it works for my purposesImperial
Other setups might benefit from it as well, like phpstorm's database integration etc.Alby

© 2022 - 2024 — McMap. All rights reserved.