├── .gitignore ├── Dockerfile ├── README.md ├── log.lua └── main.lua /.gitignore: -------------------------------------------------------------------------------- 1 | .idea -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM debian:jessie 2 | # docker build -t bscheshir/mysql-proxy:0.8.5 . 3 | MAINTAINER BSCheshir 4 | 5 | ENV MYSQL_PROXY_VERSION 0.8.5 6 | ENV MYSQL_PROXY_TAR_NAME mysql-proxy-$MYSQL_PROXY_VERSION-linux-debian6.0-x86-64bit 7 | 8 | RUN apt-get update && \ 9 | DEBIAN_FRONTEND=noninteractive apt-get -y install wget && \ 10 | wget https://downloads.mysql.com/archives/get/file/$MYSQL_PROXY_TAR_NAME.tar.gz && \ 11 | tar -xzvf $MYSQL_PROXY_TAR_NAME.tar.gz && \ 12 | mv $MYSQL_PROXY_TAR_NAME /opt/mysql-proxy && \ 13 | rm $MYSQL_PROXY_TAR_NAME.tar.gz && \ 14 | DEBIAN_FRONTEND=noninteractive apt-get -y remove wget && \ 15 | apt-get clean && \ 16 | rm -rf /var/lib/apt/lists/ && \ 17 | chown -R root:root /opt/mysql-proxy && \ 18 | echo "#!/bin/bash\n\ 19 | \n\ 20 | exec /opt/mysql-proxy/bin/mysql-proxy \\\\\n\ 21 | --keepalive \\\\\n\ 22 | --log-level=debug \\\\\n\ 23 | --plugins=proxy \\\\\n\ 24 | --proxy-address=\${PROXY_DB_HOST}:\${PROXY_DB_PORT} \\\\\n\ 25 | --proxy-backend-addresses=\${REMOTE_DB_HOST}:\${REMOTE_DB_PORT} \\\\\n\ 26 | --proxy-lua-script=\${LUA_SCRIPT}\n\ 27 | " >> /usr/local/bin//entrypoint.sh && \ 28 | chmod u+x /usr/local/bin/entrypoint.sh && \ 29 | ln -s /usr/local/bin/docker-entrypoint.sh /entrypoint.sh # shortcut 30 | EXPOSE 4040 4041 31 | 32 | ENTRYPOINT [ "entrypoint.sh" ] 33 | 34 | 35 | # For another derived image: 36 | 37 | # --help-all 38 | # --proxy-backend-addresses=mysql:3306 39 | # --proxy-skip-profiling 40 | # --proxy-backend-addresses=host:port 41 | # --proxy-read-only-backend-addresses=host:port 42 | # --keepalive 43 | # --admin-username=User 44 | # --admin-password=Password 45 | # --log-level=crititcal 46 | # The log level to use when outputting error messages. 47 | # Messages with that level (or lower) are output. 48 | # For example, message level also outputs message with info, warning, and error levels. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # mysql-proxy 2 | 3 | # Usage with docker-compose 4 | 5 | without 6 | ``` 7 | version: '2' 8 | 9 | services: 10 | db: 11 | image: mysql:8.0.0 12 | restart: always 13 | ports: 14 | - "3307:3306" #for external connection 15 | volumes: 16 | - ../mysql-data/db:/var/lib/mysql #mysql-data 17 | environment: 18 | MYSQL_ROOT_PASSWORD: password 19 | MYSQL_DATABASE: dbuser 20 | MYSQL_USER: dbuser 21 | MYSQL_PASSWORD: password 22 | ``` 23 | 24 | within 25 | ``` 26 | version: '2' 27 | 28 | services: 29 | mysql: 30 | image: mysql:8.0.0 31 | restart: always 32 | expose: 33 | - "3306" #for service mysql-proxy 34 | ports: 35 | - "3307:3306" #for external connection 36 | volumes: 37 | - ../mysql-data/db:/var/lib/mysql #mysql-data 38 | environment: 39 | MYSQL_ROOT_PASSWORD: password 40 | MYSQL_DATABASE: dbuser 41 | MYSQL_USER: dbuser 42 | MYSQL_PASSWORD: password 43 | db: 44 | image: bscheshir/mysqlproxy:0.8.5 45 | expose: 46 | - "3306" #for service php 47 | ports: 48 | - "3308:3306" #for external connection 49 | restart: always 50 | volumes: 51 | - ../mysql-proxy-conf:/opt/mysql-proxy/conf 52 | environment: 53 | PROXY_DB_PORT: 3306 54 | REMOTE_DB_HOST: mysql 55 | REMOTE_DB_PORT: 3306 56 | LUA_SCRIPT: "/opt/mysql-proxy/conf/main.lua" 57 | depends_on: 58 | - mysql 59 | ``` 60 | 61 | # Query to stdout 62 | For `docker-compose up` without `-d` (`../mysql-proxy/main.lua`) 63 | ``` 64 | function read_query(packet) 65 | if string.byte(packet) == proxy.COM_QUERY then 66 | print(string.sub(packet, 2)) 67 | end 68 | end 69 | ``` 70 | 71 | # Query logging for mysql-proxy 72 | 73 | ``` 74 | ... 75 | volumes: 76 | - ../mysql-proxy-conf:/opt/mysql-proxy/conf 77 | - ../mysql-proxy-logs:/opt/mysql-proxy/logs 78 | environment: 79 | PROXY_DB_PORT: 3306 80 | REMOTE_DB_HOST: mysql 81 | REMOTE_DB_PORT: 3306 82 | LUA_SCRIPT: "/opt/mysql-proxy/conf/log.lua" 83 | LOG_FILE: "/opt/mysql-proxy/logs/mysql.log" 84 | ... 85 | ``` 86 | 87 | `/mysql-proxy-conf/log.lua` https://gist.github.com/simonw/1039751 88 | ``` 89 | local log_file = os.getenv("LOG_FILE") 90 | 91 | local fh = io.open(log_file, "a+") 92 | 93 | function read_query( packet ) 94 | if string.byte(packet) == proxy.COM_QUERY then 95 | local query = string.sub(packet, 2) 96 | fh:write( string.format("%s %6d -- %s \n", 97 | os.date('%Y-%m-%d %H:%M:%S'), 98 | proxy.connection.server["thread_id"], 99 | query)) 100 | fh:flush() 101 | end 102 | end 103 | ``` 104 | # thanks 105 | 106 | https://hub.docker.com/r/zwxajh/mysql-proxy 107 | https://hub.docker.com/r/gediminaspuksmys/mysqlproxy/ 108 | 109 | # logrotate 110 | The image can be expand with `logrotate` 111 | Config file `/etc/logrotate.d/mysql-proxy` (approximate) 112 | 113 | ``` 114 | /opt/mysql-proxy/mysql.log { 115 | weekly 116 | missingok 117 | rotate 35600 118 | compress 119 | delaycompress 120 | notifempty 121 | create 666 root root 122 | postrotate 123 | /etc/init.d/mysql-proxy reload > /dev/null 124 | endscript 125 | } 126 | ``` 127 | 128 | # troubleshooting 129 | If you can't create the chain `mysql` -> `mysql-proxy` -> `external client liten 0.0.0.0:3308` 130 | check extends ports on the `mysql` service and/or add `expose` directly 131 | ``` 132 | expose: 133 | - "3306" #for service mysql-proxy 134 | ``` 135 | 136 | > note: Log send to file with delay (buffering mechanism). You can restart the container for get the log immediately. -------------------------------------------------------------------------------- /log.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | 3 | Copyright (C) 2007 MySQL AB 4 | 5 | This program is free software; you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation; version 2 of the License. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program; if not, write to the Free Software 16 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 | 18 | --]] 19 | 20 | --- 21 | -- Uses MySQL-Proxy to log your queries 22 | -- Downloaded from http://ronaldbradford.com/mysql-dba/mysql-proxy/log.lua 23 | -- 24 | -- Written by Giuseppe Maxia, based on examples provided 25 | -- by Jan Knesckhe 26 | -- 27 | -- This script will log the current date and time, the connection id 28 | -- and the query to a file named "mysql.log" 29 | -- 30 | local log_file = os.getenv("LOG_FILE") 31 | 32 | local fh = io.open(log_file, "a+") 33 | 34 | function read_query( packet ) 35 | if string.byte(packet) == proxy.COM_QUERY then 36 | local query = string.sub(packet, 2) 37 | fh:write( string.format("%s %6d -- %s \n", 38 | os.date('%Y-%m-%d %H:%M:%S'), 39 | proxy.connection.server["thread_id"], 40 | query)) 41 | fh:flush() 42 | end 43 | end -------------------------------------------------------------------------------- /main.lua: -------------------------------------------------------------------------------- 1 | function read_query(packet) 2 | if string.byte(packet) == proxy.COM_QUERY then 3 | print(string.sub(packet, 2)) 4 | end 5 | end 6 | --------------------------------------------------------------------------------