Setting up diesel with mysql

Hello y'all,
I started working on a project and wanted to try out diesel for the db stuff. However I can't get it to connect to mariadb. I always get the error Can't connect to local server through socket '/run/mysqld/mysqld.sock'.

The setup is as follows:

docker container run --rm -p 3306:3306 --env MARIADB_USER=user --env MARIADB_PASSWORD=passwd --env MARIADB_ROOT_PASSWORD=root mariadb:latest

echo DATABASE_URL=mysql://user:passwd@localhost:3306/my_db > .env

diesel setup

What am I doing wrong here?

That's due to an "feature" of the underlying library (libmysqlclient). It replaces connection urls containing localhost automatically with connection urls using unix sockets. This does not work if the database is running inside of a docker container. You can work around this by just using an explicit ip there:

echo DATABASE_URL=mysql://user:passwd@127.0.0.1:3306/my_db > .env

should fix your problem

1 Like

Thanks, it worked.