diff --git a/common/seaf-db.c b/common/seaf-db.c index 343931e6..4093b444 100644 --- a/common/seaf-db.c +++ b/common/seaf-db.c @@ -845,6 +845,14 @@ mysql_db_get_connection (SeafDB *vdb) if (db->charset) mysql_options(db_conn, MYSQL_SET_CHARSET_NAME, db->charset); + if (db->unix_socket) { + int pro_type = MYSQL_PROTOCOL_SOCKET; + mysql_options (db_conn, MYSQL_OPT_PROTOCOL, &pro_type); + if (!db->user) { + mysql_options (db_conn, MYSQL_DEFAULT_AUTH, "unix_socket"); + } + } + mysql_options(db_conn, MYSQL_OPT_CONNECT_TIMEOUT, (const char*)&conn_timeout); mysql_options(db_conn, MYSQL_OPT_READ_TIMEOUT, (const char*)&read_write_timeout); mysql_options(db_conn, MYSQL_OPT_WRITE_TIMEOUT, (const char*)&read_write_timeout); diff --git a/common/seaf-utils.c b/common/seaf-utils.c index a2bfc76b..e8db8ee5 100644 --- a/common/seaf-utils.c +++ b/common/seaf-utils.c @@ -67,8 +67,11 @@ mysql_db_start (SeafileSession *session) int max_connections = 0; GError *error = NULL; + unix_socket = seaf_key_file_get_string (session->config, + "database", "unix_socket", NULL); + host = seaf_key_file_get_string (session->config, "database", "host", &error); - if (!host) { + if (!host && !unix_socket) { seaf_warning ("DB host not set in config.\n"); return -1; } @@ -79,13 +82,13 @@ mysql_db_start (SeafileSession *session) } user = seaf_key_file_get_string (session->config, "database", "user", &error); - if (!user) { + if (!user && !unix_socket) { seaf_warning ("DB user not set in config.\n"); return -1; } passwd = seaf_key_file_get_string (session->config, "database", "password", &error); - if (!passwd) { + if (!passwd && !unix_socket) { seaf_warning ("DB passwd not set in config.\n"); return -1; } @@ -96,9 +99,6 @@ mysql_db_start (SeafileSession *session) return -1; } - unix_socket = seaf_key_file_get_string (session->config, - "database", "unix_socket", NULL); - use_ssl = g_key_file_get_boolean (session->config, "database", "use_ssl", NULL); @@ -274,16 +274,18 @@ ccnet_init_mysql_database (SeafileSession *session) user = ccnet_key_file_get_string (session->ccnet_config, "Database", "USER"); passwd = ccnet_key_file_get_string (session->ccnet_config, "Database", "PASSWD"); db = ccnet_key_file_get_string (session->ccnet_config, "Database", "DB"); + unix_socket = ccnet_key_file_get_string (session->ccnet_config, + "Database", "UNIX_SOCKET"); - if (!host) { + if (!host && !unix_socket) { seaf_warning ("DB host not set in config.\n"); return -1; } - if (!user) { + if (!user && !unix_socket) { seaf_warning ("DB user not set in config.\n"); return -1; } - if (!passwd) { + if (!passwd && !unix_socket) { seaf_warning ("DB passwd not set in config.\n"); return -1; } @@ -299,8 +301,6 @@ ccnet_init_mysql_database (SeafileSession *session) port = MYSQL_DEFAULT_PORT; } - unix_socket = ccnet_key_file_get_string (session->ccnet_config, - "Database", "UNIX_SOCKET"); use_ssl = g_key_file_get_boolean (session->ccnet_config, "Database", "USE_SSL", NULL); skip_verify = g_key_file_get_boolean (session->ccnet_config, "Database", "SKIP_VERIFY", NULL); if (use_ssl && !skip_verify) { diff --git a/fileserver/fileserver.go b/fileserver/fileserver.go index 43066f12..9b84ec90 100644 --- a/fileserver/fileserver.go +++ b/fileserver/fileserver.go @@ -91,18 +91,27 @@ func loadCcnetDB() { } if strings.EqualFold(dbEngine, "mysql") { - if key, err = section.GetKey("HOST"); err != nil { + unixSocket := "" + if key, err = section.GetKey("UNIX_SOCKET"); err == nil { + unixSocket = key.String() + } + host := "" + if key, err = section.GetKey("HOST"); err == nil { + host = key.String() + } else if unixSocket == "" { log.Fatal("No database host in ccnet.conf.") } - host := key.String() + // user is required. if key, err = section.GetKey("USER"); err != nil { log.Fatal("No database user in ccnet.conf.") } user := key.String() - if key, err = section.GetKey("PASSWD"); err != nil { + password := "" + if key, err = section.GetKey("PASSWD"); err == nil { + password = key.String() + } else if unixSocket == "" { log.Fatal("No database password in ccnet.conf.") } - password := key.String() if key, err = section.GetKey("DB"); err != nil { log.Fatal("No database db_name in ccnet.conf.") } @@ -111,10 +120,6 @@ func loadCcnetDB() { if key, err = section.GetKey("PORT"); err == nil { port, _ = key.Int() } - unixSocket := "" - if key, err = section.GetKey("UNIX_SOCKET"); err == nil { - unixSocket = key.String() - } useTLS := false if key, err = section.GetKey("USE_SSL"); err == nil { useTLS, _ = key.Bool() @@ -191,18 +196,28 @@ func loadSeafileDB() { dbEngine = key.String() } if strings.EqualFold(dbEngine, "mysql") { - if key, err = section.GetKey("host"); err != nil { + unixSocket := "" + if key, err = section.GetKey("unix_socket"); err == nil { + unixSocket = key.String() + } + host := "" + if key, err = section.GetKey("host"); err == nil { + host = key.String() + } else if unixSocket == "" { log.Fatal("No database host in seafile.conf.") } - host := key.String() + // user is required. if key, err = section.GetKey("user"); err != nil { log.Fatal("No database user in seafile.conf.") } user := key.String() - if key, err = section.GetKey("password"); err != nil { + + password := "" + if key, err = section.GetKey("password"); err == nil { + password = key.String() + } else if unixSocket == "" { log.Fatal("No database password in seafile.conf.") } - password := key.String() if key, err = section.GetKey("db_name"); err != nil { log.Fatal("No database db_name in seafile.conf.") } @@ -211,10 +226,6 @@ func loadSeafileDB() { if key, err = section.GetKey("port"); err == nil { port, _ = key.Int() } - unixSocket := "" - if key, err = section.GetKey("unix_socket"); err == nil { - unixSocket = key.String() - } useTLS := false if key, err = section.GetKey("use_ssl"); err == nil { useTLS, _ = key.Bool() diff --git a/notification-server/server.go b/notification-server/server.go index 6d59e72d..de938b40 100644 --- a/notification-server/server.go +++ b/notification-server/server.go @@ -104,18 +104,28 @@ func loadCcnetDB() { log.Fatalf("Unsupported database %s.", dbEngine) } - if key, err = section.GetKey("HOST"); err != nil { + unixSocket := "" + if key, err = section.GetKey("UNIX_SOCKET"); err == nil { + unixSocket = key.String() + } + + host := "" + if key, err = section.GetKey("HOST"); err == nil { + host = key.String() + } else if unixSocket == "" { log.Fatal("No database host in ccnet.conf.") } - host := key.String() + // user is required. if key, err = section.GetKey("USER"); err != nil { log.Fatal("No database user in ccnet.conf.") } user := key.String() - if key, err = section.GetKey("PASSWD"); err != nil { + password := "" + if key, err = section.GetKey("PASSWD"); err == nil { + password = key.String() + } else if unixSocket == "" { log.Fatal("No database password in ccnet.conf.") } - password := key.String() if key, err = section.GetKey("DB"); err != nil { log.Fatal("No database db_name in ccnet.conf.") } @@ -124,10 +134,6 @@ func loadCcnetDB() { if key, err = section.GetKey("PORT"); err == nil { port, _ = key.Int() } - unixSocket := "" - if key, err = section.GetKey("UNIX_SOCKET"); err == nil { - unixSocket = key.String() - } useTLS := false if key, err = section.GetKey("USE_SSL"); err == nil { useTLS, _ = key.Bool()