mirror of
https://github.com/Portabase/agent.git
synced 2026-09-10 01:57:10 +00:00
fix: mysql localhost
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
use crate::domain::mariadb::connection::{select_mariadb_path, server_version};
|
||||
use crate::services::backup::logger::JobLogger;
|
||||
use crate::domain::mysql::connection::connection_args;
|
||||
use crate::services::config::DatabaseConfig;
|
||||
use anyhow::{Context, Result};
|
||||
use std::collections::HashMap;
|
||||
@@ -42,9 +43,7 @@ pub async fn run(
|
||||
|
||||
let start = Instant::now();
|
||||
let output = Command::new("mariadb-dump")
|
||||
.arg("--host").arg(&cfg.host)
|
||||
.arg("--port").arg(cfg.port.to_string())
|
||||
.arg("--user").arg(&cfg.username)
|
||||
.args(connection_args(&cfg))
|
||||
.arg("--routines")
|
||||
.arg("--events")
|
||||
.arg("--triggers")
|
||||
|
||||
@@ -1,16 +1,12 @@
|
||||
use std::path::PathBuf;
|
||||
use crate::domain::mysql::connection::connection_args;
|
||||
use crate::services::config::DatabaseConfig;
|
||||
use anyhow::Result;
|
||||
use std::process::Command;
|
||||
|
||||
pub async fn server_version(cfg: &DatabaseConfig) -> Result<String> {
|
||||
let output = Command::new("mariadb")
|
||||
.arg("--host")
|
||||
.arg(&cfg.host)
|
||||
.arg("--port")
|
||||
.arg(cfg.port.to_string())
|
||||
.arg("--user")
|
||||
.arg(&cfg.username)
|
||||
.args(connection_args(cfg))
|
||||
.arg("-e")
|
||||
.arg("SELECT VERSION();")
|
||||
.env("MYSQL_PWD", &cfg.password)
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use crate::domain::mysql::connection::connection_args;
|
||||
use crate::services::config::DatabaseConfig;
|
||||
use std::collections::HashMap;
|
||||
use tokio::process::Command;
|
||||
@@ -5,12 +6,7 @@ use tokio::time::{Duration, timeout};
|
||||
|
||||
pub async fn run(cfg: DatabaseConfig, env: HashMap<String, String>) -> anyhow::Result<bool> {
|
||||
let mut cmd = Command::new("mysqladmin");
|
||||
cmd.arg("--host")
|
||||
.arg(cfg.host)
|
||||
.arg("--port")
|
||||
.arg(cfg.port.to_string())
|
||||
.arg("--user")
|
||||
.arg(cfg.username)
|
||||
cmd.args(connection_args(&cfg))
|
||||
.arg("ping")
|
||||
.envs(env);
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use crate::services::backup::logger::JobLogger;
|
||||
use crate::domain::mysql::connection::connection_args;
|
||||
use crate::services::config::DatabaseConfig;
|
||||
use anyhow::{Context, Result};
|
||||
use std::fs::File;
|
||||
@@ -21,12 +22,7 @@ pub async fn run(cfg: DatabaseConfig, restore_file: PathBuf, logger: Arc<JobLogg
|
||||
|
||||
let drop_start = Instant::now();
|
||||
let drop_output = Command::new("mariadb")
|
||||
.arg("--host")
|
||||
.arg(&cfg.host)
|
||||
.arg("--port")
|
||||
.arg(cfg.port.to_string())
|
||||
.arg("--user")
|
||||
.arg(&cfg.username)
|
||||
.args(connection_args(&cfg))
|
||||
.arg("-e")
|
||||
.arg(&drop_create_cmd)
|
||||
.env("MYSQL_PWD", &cfg.password)
|
||||
@@ -48,12 +44,7 @@ pub async fn run(cfg: DatabaseConfig, restore_file: PathBuf, logger: Arc<JobLogg
|
||||
let start = Instant::now();
|
||||
|
||||
let mut child = Command::new("mariadb")
|
||||
.arg("--host")
|
||||
.arg(&cfg.host)
|
||||
.arg("--port")
|
||||
.arg(cfg.port.to_string())
|
||||
.arg("--user")
|
||||
.arg(&cfg.username)
|
||||
.args(connection_args(&cfg))
|
||||
.arg("--database")
|
||||
.arg(&cfg.database)
|
||||
.env("MYSQL_PWD", &cfg.password)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::domain::mysql::connection::server_version;
|
||||
use crate::domain::mysql::connection::{connection_args, server_version};
|
||||
use crate::services::backup::logger::JobLogger;
|
||||
use crate::services::config::DatabaseConfig;
|
||||
use anyhow::{Context, Result};
|
||||
@@ -39,9 +39,7 @@ pub async fn run(
|
||||
|
||||
let start = Instant::now();
|
||||
let output = Command::new("mysqldump")
|
||||
.arg("--host").arg(&cfg.host)
|
||||
.arg("--port").arg(cfg.port.to_string())
|
||||
.arg("--user").arg(&cfg.username)
|
||||
.args(connection_args(&cfg))
|
||||
.arg("--routines")
|
||||
.arg("--events")
|
||||
.arg("--triggers")
|
||||
|
||||
@@ -2,14 +2,33 @@ use crate::services::config::DatabaseConfig;
|
||||
use anyhow::Result;
|
||||
use std::process::Command;
|
||||
|
||||
pub fn connection_args(cfg: &DatabaseConfig) -> Vec<String> {
|
||||
let protocol = cfg
|
||||
.options
|
||||
.get("protocol")
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or("tcp");
|
||||
|
||||
let mut args = vec![
|
||||
format!("--protocol={}", protocol),
|
||||
"--host".to_string(),
|
||||
cfg.host.clone(),
|
||||
"--port".to_string(),
|
||||
cfg.port.to_string(),
|
||||
"--user".to_string(),
|
||||
cfg.username.clone(),
|
||||
];
|
||||
|
||||
if let Some(socket) = cfg.options.get("socket").and_then(|v| v.as_str()) {
|
||||
args.push(format!("--socket={}", socket));
|
||||
}
|
||||
|
||||
args
|
||||
}
|
||||
|
||||
pub async fn server_version(cfg: &DatabaseConfig) -> Result<String> {
|
||||
let output = Command::new("mysql")
|
||||
.arg("--host")
|
||||
.arg(&cfg.host)
|
||||
.arg("--port")
|
||||
.arg(cfg.port.to_string())
|
||||
.arg("--user")
|
||||
.arg(&cfg.username)
|
||||
.args(connection_args(cfg))
|
||||
.arg("-e")
|
||||
.arg("SELECT VERSION();")
|
||||
.env("MYSQL_PWD", &cfg.password)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
pub mod backup;
|
||||
mod connection;
|
||||
pub mod connection;
|
||||
pub mod database;
|
||||
mod ping;
|
||||
mod restore;
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use crate::domain::mysql::connection::connection_args;
|
||||
use crate::services::config::DatabaseConfig;
|
||||
use std::collections::HashMap;
|
||||
use tokio::process::Command;
|
||||
@@ -5,12 +6,7 @@ use tokio::time::{Duration, timeout};
|
||||
|
||||
pub async fn run(cfg: DatabaseConfig, env: HashMap<String, String>) -> anyhow::Result<bool> {
|
||||
let mut cmd = Command::new("mariadb-admin");
|
||||
cmd.arg("--host")
|
||||
.arg(cfg.host)
|
||||
.arg("--port")
|
||||
.arg(cfg.port.to_string())
|
||||
.arg("--user")
|
||||
.arg(cfg.username)
|
||||
cmd.args(connection_args(&cfg))
|
||||
.arg("ping")
|
||||
.envs(env);
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use crate::services::backup::logger::JobLogger;
|
||||
use crate::domain::mysql::connection::connection_args;
|
||||
use crate::services::config::DatabaseConfig;
|
||||
use anyhow::{Context, Result};
|
||||
use std::fs::File;
|
||||
@@ -21,12 +22,7 @@ pub async fn run(cfg: DatabaseConfig, restore_file: PathBuf, logger: Arc<JobLogg
|
||||
|
||||
let drop_start = Instant::now();
|
||||
let drop_output = Command::new("mysql")
|
||||
.arg("--host")
|
||||
.arg(&cfg.host)
|
||||
.arg("--port")
|
||||
.arg(cfg.port.to_string())
|
||||
.arg("--user")
|
||||
.arg(&cfg.username)
|
||||
.args(connection_args(&cfg))
|
||||
.arg("-e")
|
||||
.arg(&drop_create_cmd)
|
||||
.env("MYSQL_PWD", &cfg.password)
|
||||
@@ -48,12 +44,7 @@ pub async fn run(cfg: DatabaseConfig, restore_file: PathBuf, logger: Arc<JobLogg
|
||||
let start = Instant::now();
|
||||
|
||||
let mut child = Command::new("mysql")
|
||||
.arg("--host")
|
||||
.arg(&cfg.host)
|
||||
.arg("--port")
|
||||
.arg(cfg.port.to_string())
|
||||
.arg("--user")
|
||||
.arg(&cfg.username)
|
||||
.args(connection_args(&cfg))
|
||||
.arg("--database")
|
||||
.arg(&cfg.database)
|
||||
.env("MYSQL_PWD", &cfg.password)
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use crate::domain::factory::DatabaseFactory;
|
||||
use crate::domain::mysql::connection::connection_args;
|
||||
use crate::services::config::{DatabaseConfig, DbType};
|
||||
use crate::tests::init_tracing_for_test;
|
||||
use crate::utils::compress::{compress_to_tar_gz_large, decompress_large_tar_gz};
|
||||
@@ -96,3 +97,54 @@ async fn mysql_backup_restore_test() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn tunnelled_config(options: serde_json::Value) -> DatabaseConfig {
|
||||
DatabaseConfig {
|
||||
name: "my-db".to_string(),
|
||||
database: "my-db".to_string(),
|
||||
db_type: DbType::Mysql,
|
||||
username: "my-db-user".to_string(),
|
||||
password: "my-db-password".to_string(),
|
||||
port: 3306,
|
||||
host: "localhost".to_string(),
|
||||
generated_id: "16678159-ff7e-4c97-8c83-0adeff214681".to_string(),
|
||||
path: "".to_string(),
|
||||
max_packet_size: "512M".to_string(),
|
||||
volume_name: "".to_string(),
|
||||
container_name: None,
|
||||
options: serde_json::from_value(options).unwrap(),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn connection_args_force_tcp_for_localhost() {
|
||||
// A `localhost` host makes the clients pick a Unix socket and ignore `--port`,
|
||||
// which breaks databases reached through an SSH tunnel.
|
||||
let cfg = tunnelled_config(serde_json::json!({}));
|
||||
|
||||
assert_eq!(
|
||||
connection_args(&cfg),
|
||||
vec![
|
||||
"--protocol=tcp",
|
||||
"--host",
|
||||
"localhost",
|
||||
"--port",
|
||||
"3306",
|
||||
"--user",
|
||||
"my-db-user",
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn connection_args_allow_socket_opt_in() {
|
||||
let cfg = tunnelled_config(serde_json::json!({
|
||||
"protocol": "socket",
|
||||
"socket": "/var/run/mysqld/mysqld.sock",
|
||||
}));
|
||||
|
||||
let args = connection_args(&cfg);
|
||||
|
||||
assert_eq!(args[0], "--protocol=socket");
|
||||
assert_eq!(args.last().unwrap(), "--socket=/var/run/mysqld/mysqld.sock");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user