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 97c3bb32a..9ceb66e59 100644 --- a/backend/src/config.ts +++ b/backend/src/config.ts @@ -43,6 +43,7 @@ interface IConfig { DATABASE: { ENABLED: boolean; HOST: string, + SOCKET: string, PORT: number; DATABASE: string; USERNAME: string; @@ -121,6 +122,7 @@ const defaults: IConfig = { 'DATABASE': { 'ENABLED': true, 'HOST': '127.0.0.1', + 'SOCKET': '', 'PORT': 3306, 'DATABASE': 'mempool', 'USERNAME': 'mempool', diff --git a/backend/src/database.ts b/backend/src/database.ts index 596ce2364..55be0ffc3 100644 --- a/backend/src/database.ts +++ b/backend/src/database.ts @@ -1,17 +1,30 @@ 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/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/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..1d8c0d1fc 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