From 7fd9e27cc28e35b3656ed4ceac4ceb27d4d3493a Mon Sep 17 00:00:00 2001 From: Djuri Baars Date: Sun, 13 Mar 2022 14:57:17 +0100 Subject: [PATCH 1/3] Add MySQL socket support --- backend/src/config.ts | 2 ++ backend/src/database.ts | 30 +++++++++++++++++++++--------- docker/backend/mempool-config.json | 1 + docker/backend/start.sh | 3 +++ 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/backend/src/config.ts b/backend/src/config.ts index 97c3bb32a..7bb99b429 100644 --- a/backend/src/config.ts +++ b/backend/src/config.ts @@ -43,6 +43,7 @@ interface IConfig { DATABASE: { ENABLED: boolean; HOST: string, + SOCKET: string | undefined, PORT: number; DATABASE: string; USERNAME: string; @@ -121,6 +122,7 @@ const defaults: IConfig = { 'DATABASE': { 'ENABLED': true, 'HOST': '127.0.0.1', + 'SOCKET': undefined, 'PORT': 3306, 'DATABASE': 'mempool', 'USERNAME': 'mempool', diff --git a/backend/src/database.ts b/backend/src/database.ts index 596ce2364..4f73a1b7d 100644 --- a/backend/src/database.ts +++ b/backend/src/database.ts @@ -1,17 +1,29 @@ import config from './config'; import { createPool, PoolConnection } from 'mysql2/promise'; import logger from './logger'; +import { PoolOptions } from 'mysql2/typings/mysql'; export class DB { - static pool = createPool({ - host: config.DATABASE.HOST, - port: config.DATABASE.PORT, - database: config.DATABASE.DATABASE, - user: config.DATABASE.USERNAME, - password: config.DATABASE.PASSWORD, - connectionLimit: 10, - supportBigNumbers: true, - }); + static poolConfig = ():PoolOptions => { + let poolConfig:PoolOptions = { + port: config.DATABASE.PORT, + database: config.DATABASE.DATABASE, + user: config.DATABASE.USERNAME, + password: config.DATABASE.PASSWORD, + connectionLimit: 10, + supportBigNumbers: true, + timezone: '+00:00', + } + + if (config.DATABASE.SOCKET) + poolConfig.socketPath = config.DATABASE.SOCKET + else + poolConfig.host = config.DATABASE.HOST + + return poolConfig; + } + + static pool = createPool(DB.poolConfig()); static connectionsReady: number[] = []; diff --git a/docker/backend/mempool-config.json b/docker/backend/mempool-config.json index 6b2319a59..39d0e5fad 100644 --- a/docker/backend/mempool-config.json +++ b/docker/backend/mempool-config.json @@ -41,6 +41,7 @@ "DATABASE": { "ENABLED": __DATABASE_ENABLED__, "HOST": "__DATABASE_HOST__", + "SOCKET": "__DATABASE_SOCKET__", "PORT": __DATABASE_PORT__, "DATABASE": "__DATABASE_DATABASE__", "USERNAME": "__DATABASE_USERNAME__", diff --git a/docker/backend/start.sh b/docker/backend/start.sh index d26a93b08..db610a362 100644 --- a/docker/backend/start.sh +++ b/docker/backend/start.sh @@ -42,6 +42,7 @@ __SECOND_CORE_RPC_PASSWORD__=${SECOND_CORE_RPC_PASSWORD:=mempool} # DATABASE __DATABASE_ENABLED__=${DATABASE_ENABLED:=true} __DATABASE_HOST__=${DATABASE_HOST:=127.0.0.1} +__DATABASE_SOCKET__=${DATABASE_SOCKET:=""} __DATABASE_PORT__=${DATABASE_PORT:=3306} __DATABASE_DATABASE__=${DATABASE_DATABASE:=mempool} __DATABASE_USERNAME__=${DATABASE_USERNAME:=mempool} @@ -111,6 +112,8 @@ sed -i "s/__SECOND_CORE_RPC_PASSWORD__/${__SECOND_CORE_RPC_PASSWORD__}/g" mempoo sed -i "s/__DATABASE_ENABLED__/${__DATABASE_ENABLED__}/g" mempool-config.json sed -i "s/__DATABASE_HOST__/${__DATABASE_HOST__}/g" mempool-config.json +sed -i "s/__DATABASE_SOCKET__/${__DATABASE_SOCKET__}/g" mempool-config.json + sed -i "s/__DATABASE_PORT__/${__DATABASE_PORT__}/g" mempool-config.json sed -i "s/__DATABASE_DATABASE__/${__DATABASE_DATABASE__}/g" mempool-config.json sed -i "s/__DATABASE_USERNAME__/${__DATABASE_USERNAME__}/g" mempool-config.json From 465529b03f518feabfc5abbfc7c2762e9b84a7bf Mon Sep 17 00:00:00 2001 From: Djuri Baars Date: Sun, 13 Mar 2022 15:09:16 +0100 Subject: [PATCH 2/3] sed and config fix for MySQL socket, accept CLA for @dsbaars --- backend/src/database.ts | 2 +- contributors/dsbaars.txt | 3 +++ docker/backend/start.sh | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-) create mode 100644 contributors/dsbaars.txt diff --git a/backend/src/database.ts b/backend/src/database.ts index 4f73a1b7d..b106c6ec9 100644 --- a/backend/src/database.ts +++ b/backend/src/database.ts @@ -15,7 +15,7 @@ export class DB { timezone: '+00:00', } - if (config.DATABASE.SOCKET) + if (config.DATABASE.SOCKET && config.DATABASE.SOCKET != "") poolConfig.socketPath = config.DATABASE.SOCKET else poolConfig.host = config.DATABASE.HOST diff --git a/contributors/dsbaars.txt b/contributors/dsbaars.txt new file mode 100644 index 000000000..8e4dcbb9a --- /dev/null +++ b/contributors/dsbaars.txt @@ -0,0 +1,3 @@ +I hereby accept the terms of the Contributor License Agreement in the CONTRIBUTING.md file of the mempool/mempool git repository as of January 25, 2022. + +Signed: dsbaars \ No newline at end of file diff --git a/docker/backend/start.sh b/docker/backend/start.sh index db610a362..1d8c0d1fc 100644 --- a/docker/backend/start.sh +++ b/docker/backend/start.sh @@ -112,7 +112,7 @@ sed -i "s/__SECOND_CORE_RPC_PASSWORD__/${__SECOND_CORE_RPC_PASSWORD__}/g" mempoo sed -i "s/__DATABASE_ENABLED__/${__DATABASE_ENABLED__}/g" mempool-config.json sed -i "s/__DATABASE_HOST__/${__DATABASE_HOST__}/g" mempool-config.json -sed -i "s/__DATABASE_SOCKET__/${__DATABASE_SOCKET__}/g" mempool-config.json +sed -i "s!__DATABASE_SOCKET__!${__DATABASE_SOCKET__}!g" mempool-config.json sed -i "s/__DATABASE_PORT__/${__DATABASE_PORT__}/g" mempool-config.json sed -i "s/__DATABASE_DATABASE__/${__DATABASE_DATABASE__}/g" mempool-config.json From 2023d366036cc7b3a3249008d662559a6c0190ba Mon Sep 17 00:00:00 2001 From: wiz Date: Mon, 14 Mar 2022 13:11:04 +0000 Subject: [PATCH 3/3] Cleanup MySQL unix socket code and add to sample config --- backend/mempool-config.sample.json | 1 + backend/src/config.ts | 4 ++-- backend/src/database.ts | 9 +++++---- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/backend/mempool-config.sample.json b/backend/mempool-config.sample.json index 5c45838ee..b5c4b8d53 100644 --- a/backend/mempool-config.sample.json +++ b/backend/mempool-config.sample.json @@ -44,6 +44,7 @@ "ENABLED": true, "HOST": "127.0.0.1", "PORT": 3306, + "SOCKET": "/var/run/mysql/mysql.sock", "DATABASE": "mempool", "USERNAME": "mempool", "PASSWORD": "mempool" diff --git a/backend/src/config.ts b/backend/src/config.ts index 7bb99b429..9ceb66e59 100644 --- a/backend/src/config.ts +++ b/backend/src/config.ts @@ -43,7 +43,7 @@ interface IConfig { DATABASE: { ENABLED: boolean; HOST: string, - SOCKET: string | undefined, + SOCKET: string, PORT: number; DATABASE: string; USERNAME: string; @@ -122,7 +122,7 @@ const defaults: IConfig = { 'DATABASE': { 'ENABLED': true, 'HOST': '127.0.0.1', - 'SOCKET': undefined, + 'SOCKET': '', 'PORT': 3306, 'DATABASE': 'mempool', 'USERNAME': 'mempool', diff --git a/backend/src/database.ts b/backend/src/database.ts index b106c6ec9..55be0ffc3 100644 --- a/backend/src/database.ts +++ b/backend/src/database.ts @@ -15,10 +15,11 @@ export class DB { timezone: '+00:00', } - if (config.DATABASE.SOCKET && config.DATABASE.SOCKET != "") - poolConfig.socketPath = config.DATABASE.SOCKET - else - poolConfig.host = config.DATABASE.HOST + if (config.DATABASE.SOCKET !== "") { + poolConfig.socketPath = config.DATABASE.SOCKET; + } else { + poolConfig.host = config.DATABASE.HOST; + } return poolConfig; }