Docker-Swarm-Loadbalancer/mysql-proxy/README.md
Matthew Baggett 6db6395f66
Trunk linter ()
* linting is fun

* mis-detection of missing healthcheck staements.

* typo

* disable tagging vanity tags on non-main branch

* Unbugger node build?

* Add gitleaks detector, remove an expired secret.

* More linting-derived cleanup

* Fiddle with trivy

* Fiddle with trivy

* add a readme

* Fix build bug with php flavours

* Marshall should build other flavours of ubuntu.

* Fiddle with act cache location.

* Add concurrency checks

* Composer version gubbins for 7.0/7.1

* ubuntu is just a label, and injected over the top of.

* Composer version gubbins for 7.0/7.1

* Run when workflow is altered too please.

* Hopefully fix composer stage.

* setup tooling meta-tooling.

* Add trunk

* Disable mirror mode, its being problematic, and increase retries to 5.

* Revisit how ghcr login works.

* Add trunk checks.

* All hail the linter

* Heavilly revise workflow

* Fettling

* Fettling

* Fettling

* Fettling

* Fettling

* Cleanup

* Cleanup

* Fettling.. Why does mitm build but not redis?

* Fettling.. Why does mitm build but not redis?

* Debuggin

* Fettling.

* Fix build?

* Permissions are a pain

* Switch around some should_push logic because envs aren't available that early.

* Permissionssssss

* Trivy, bane of my life

* Fix merge?

* Fix labels

* Help node along, among other things

* Redis 6.1 & 7.1 aren't a thing any more.

* Ffff USER nonsense

* latest-openssl doesn't exist.

* fixup mysqlproxy.

* Fix labels

* uurrgh

* uurrgh

* Didn't need to add the mitmproxy user, it exists

* Missing ghcr login

* Missing backtick

* Fix build?

* Add validate build step to bouncer.

* Fix bouncer build

* Disable laravel build

* Missing env

* Fix swarm mon build

* Scout just doesn't seem to work.
2024-02-07 16:21:14 +01:00

143 lines
2.9 KiB
Markdown
Executable file

# MySQL Proxy
## Usage with docker-compose
without
```yaml
version: "2"
services:
db:
image: mysql:8.0.0
restart: always
ports:
- "3307:3306" #for external connection
volumes:
- ../mysql-data/db:/var/lib/mysql #mysql-data
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: dbuser
MYSQL_USER: dbuser
MYSQL_PASSWORD: password
```
within
```yaml
version: "2"
services:
mysql:
image: mysql:8.0.0
restart: always
expose:
- "3306" #for service mysql-proxy
ports:
- "3307:3306" #for external connection
volumes:
- ../mysql-data/db:/var/lib/mysql #mysql-data
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: dbuser
MYSQL_USER: dbuser
MYSQL_PASSWORD: password
db:
image: matthewbaggett/mysql-proxy
expose:
- "3306" #for service php
ports:
- "3308:3306" #for external connection
restart: always
volumes:
- ../mysql-proxy-conf:/opt/mysql-proxy/conf
environment:
PROXY_DB_PORT: 3306
REMOTE_DB_HOST: mysql
REMOTE_DB_PORT: 3306
LUA_SCRIPT: "/opt/mysql-proxy/conf/main.lua"
depends_on:
- mysql
```
## Query to stdout
For `docker-compose up` without `-d` (`../mysql-proxy/main.lua`)
```lua
function read_query(packet)
if string.byte(packet) == proxy.COM_QUERY then
print(string.sub(packet, 2))
end
end
```
## Query logging for mysql-proxy
```yaml
volumes:
- ../mysql-proxy-conf:/opt/mysql-proxy/conf
- ../mysql-proxy-logs:/opt/mysql-proxy/logs
environment:
PROXY_DB_PORT: 3306
REMOTE_DB_HOST: mysql
REMOTE_DB_PORT: 3306
LUA_SCRIPT: "/opt/mysql-proxy/conf/log.lua"
LOG_FILE: "/opt/mysql-proxy/logs/mysql.log"
```
`/mysql-proxy-conf/log.lua` https://gist.github.com/simonw/1039751
```lua
local log_file = os.getenv("LOG_FILE")
local fh = io.open(log_file, "a+")
function read_query( packet )
if string.byte(packet) == proxy.COM_QUERY then
local query = string.sub(packet, 2)
fh:write( string.format("%s %6d -- %s \n",
os.date('%Y-%m-%d %H:%M:%S'),
proxy.connection.server["thread_id"],
query))
fh:flush()
end
end
```
## thanks
https://hub.docker.com/r/zwxajh/mysql-proxy
https://hub.docker.com/r/gediminaspuksmys/mysqlproxy/
## logrotate
The image can be expand with `logrotate`
Config file `/etc/logrotate.d/mysql-proxy` (approximate)
```text
/opt/mysql-proxy/mysql.log {
weekly
missingok
rotate 35600
compress
delaycompress
notifempty
create 666 root root
postrotate
/etc/init.d/mysql-proxy reload > /dev/null
endscript
}
```
## troubleshooting
If you can't create the chain `mysql` -> `mysql-proxy` -> `external client liten 0.0.0.0:3308`
check extends ports on the `mysql` service and/or add `expose` directly
```yaml
expose:
- "3306" #for service mysql-proxy
```
> note: Log send to file with delay (buffering mechanism). You can restart the container for get the log immediately.