I am trying to connect to my MAMP PRO database but I getting Connection refused (os error 61)
all the time.
Here is my code and my env file:
Rust
use mysql::*;
use mysql::prelude::*;
pub struct DB {
connection: PooledConn
}
struct Test {
name: String,
last_name: String
}
#[allow(dead_code)]
impl DB {
pub fn new<'a>(host: &'a str, user: &'a str, name: &'a str, pass: &'a str) -> Self {
let url = format!("mysql://{}:{}@{}/{}?prefer_socket=false", user, pass, host, name);
println!("{}", url);
let pool = Pool::new(url.as_str()).unwrap();
Self {
connection: pool.get_conn().unwrap()
}
}
pub fn test(&mut self) -> std::result::Result<(), Box<dyn std::error::Error>> {
let tests = vec![
Test {name: String::from("test1"), last_name: String::from("test2")}
];
self.connection.exec_batch(
r"INSERT INTO test (name, last_name)
VALUES ( :name, :last_name)",
tests.iter().map(|test| params! {
"name" => &test.name,
"last_name" => &test.last_name,
})
)?;
Ok(())
}
}
Env
SERVER_IP="127.0.0.1:1337"
DB_HOST="127.0.0.1:3306"
DB_USER="api_host"
DB_NAME="api_host"
DB_PASSWORD="12345678admin"
Also here some additional images:
I already tried 8889 port and localhost instead of 127.0.0.1. Remanaged user with database. Don't know to pass socket. Would appreciate any suggestions, thanks in advance.
Okay, I've finally resolved the issue. First of all, I'd like to thank kmdreko for their assistance. Their question helped to clarify the problem - the socket.
So, if someone else encounters this issue, here's how I resolved it:
Here is my DB constructor
pub fn new<'a>(host: &'a str, user: &'a str, name: &'a str, pass: &'a str, socket: Option<&str>) -> Self {
let builder = (|| -> OptsBuilder {
let this = OptsBuilder::new()
.ip_or_hostname(Some(host)) // Specify the socket path here
.user(Some(user))
.pass(Some(pass))
.db_name(Some(name));
match socket {
Some(socket) => this.socket(Some(socket)),
_ => this
}
})();
let opts = Opts::from(builder);
let pool = Pool::new(opts).unwrap();
Self {
connection: pool.get_conn().unwrap()
}
}
And here is my .env
SERVER_IP="127.0.0.1:1337"
DB_HOST="127.0.0.1:3306"
DB_USER="api"
DB_NAME="api"
DB_PASSWORD="api"
DB_SOCKET="/Applications/MAMP/tmp/mysql/mysql.sock"