Unable to connect to MySQL using Diesel

Hi,

I am trying to create a simple API using Rust, MySQL and Diesel on a Windows 10 64bit machine. I am unable to connect to MySQL. I have tried different approaches. If I do a simple connection then it works but if I use diesel it gives SSL error. The below code works. It does connect to MySQL Database.

use mysql::*;
use mysql::prelude::*;
...
...
let url = "mysql://myusr:mypass@localhost:3306/pubdb";
let pool = Pool::new(url)?;

let mut conn = pool.get_conn()?;
...

I setup diesel cli using below command:

cargo install diesel_cli --no-default-features --features mysql

I created a .env file with following:

DATABASE_URL=mysql://myusr:mypass@localhost:3306/pubdb

But when I run the below command which uses Diesel to create a database then it gives SSL error.

D:\Rust\diesel_demo> diesel database setup
Creating database: pubdb
SSL connection error: unknown error number

I have tried creating another project earlier with a different approach but it gives the same error.

Thanks
Amal

It's always hard do say what's wrong with such connection issues as that's mostly dependent on your environment. The error message indicates that your database server might need a ssl connection. Have you tried to set the relevant parameters in the connection url? Does your libmysqlclient version include ssl support?

1 Like

Thanks for your reply. I investigated further. It runs if I specify the ssl_mode=DISABLED in the command line but unable to get it working using .env file, i.e. via rust code. See below:

D:\Rust\diesel_demo>diesel setup --database-url=mysql://myusr:mypass@localhost/pubdb?ssl_mode=DISABLED
Creating database: pubdb

Changed the .env file to the following but it didn't work:

DATABASE_URL=mysql://myusr:mypass@localhost/pubdb?ssl_mode=DISABLED

Tried giving DATABASE_URL in double quotes. Still got an error.

DATABASE_URL="mysql://myusr:mypass@localhost/pubdb?ssl_mode=DISABLED"

D:\Rust\diesel_demo>diesel database setup
The --database-url argument must be passed, or the DATABASE_URL environment variable must be set.

Let me know if anyone has any suggestions on how to specify this in .env file. The documentation says default for ssl_mode is PREFERRED. If we can change it to DISABLED, that might be another solution.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.