OddlyGhost
OddlyGhost

Reputation: 368

Error: UrlError { Unknown URL parameter `ssl-mode' } Rust MySQL

I am trying to use SSL in my MySQL Connection String but it is giving me this error Error: UrlError { Unknown URL parameter 'ssl-mode' } I have referred to official documents from this link: https://dev.mysql.com/doc/refman/8.0/en/connecting-using-uri-or-key-value-pairs.html. My connection string looks as follows :

----------------------------------------------------------------------------------------------------------
let connection_string = format!("mysql://{}:{}@{}?ssl-mode=REQUIRED", &user_name, &password, &hostname);
----------------------------------------------------------------------------------------------------------
let connection_url: &str = &connection_string;
    let pool = Pool::new(connection_url)?;
    let mut connection = pool.get_conn()?;

I am still a rookie in using rust. So please let me know if I am doing something wrong here.

Also if possible please suggest me a better way of approaching.

Thank you !!

Upvotes: 1

Views: 231

Answers (1)

at54321
at54321

Reputation: 11708

In order to encrypt your connection to MySQL with TLS/SSL, you need to create an SslOpts instance and pass that to the OptsBuilder instance via its ssl_opts() method.

Here is an example how to create an SslOpts instance via with_danger_accept_invalid_certs():

let builder = OptsBuilder::new();
let opts = builder
    .ip_or_hostname(Some("..."))
    .db_name(Some("..."))
    .user(Some("..."))
    .pass(Some("..."))
    .ssl_opts(SslOpts::with_danger_accept_invalid_certs(SslOpts::default(), true));
let db_pool = Pool::new_manual(2, 10, opts);
    

Using with_danger_accept_invalid_certs() is simple, although, as the name suggests, it might not be the most secure option. It should be much more secure than no encryption at all. Depending on your needs, you might want to take a look at the other options provided by the crate.

Upvotes: 1

Related Questions