Add support for MySQL UNIX Socket connections #3565
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds support for MySQL UNIX Socket connections.
Details
OpenSIPS traditionally communicates with MySQL using the network stack. Often times OpenSIPS runs within a Docker container. When MySQL also runs within a Docker container on the same server a performance improvement can be achieved by removing the network stack communication and replacing it with UNIX sockets.
Solution
MySQL supports UNIX socket communication and its C API exposes the mysql_real_connect() function contains a "unix_socket" parameter. Prior to this PR, the "unix_socket" parameter is hard-coded to NULL indicating that is is not used.
This PR enhances the db_url parsing logic to add the ability to specify a UNIX Socket named path. When a UNIX Socket is specified, the "host" and "port" parameters are no longer parsed by the db_url parsing logic. Note that when a UNIX Socket is specified, the MySQL documentation indicates that the "host" must be set to NULL or "localhost". This PR sets the "host" to "localhost" when a UNIX Socket is specified.
The db_url parsing will recognize the following db_url modparam as shown in this example:
modparam("usrloc", "db_url", "mysql://opensips:opensipsrw@unix(/var/run/mysqld/mysqld.sock)/opensips)
The UNIX Socket is defined in the same place that previously would have been the "host:port". The rest of the db_url syntax remains unchanged.
To use a MySQL UNIX Socket in a Docker containerized environment, the following example settings can be made:
Update the MySQL my.cnf file to use a UNIX Socket:
[mysqld]
socket=/var/run/mysqld/mysqld.sock
Update docker-compose.yml to share the UNIX Socket between the MySQL and OpenSIPS services:
volumes:
shared-sockets:
driver: local
services:
mysql:
volumes:
- shared-sockets:/var/run/mysqld
opensips:
volumes:
- shared-sockets:/var/run/mysqld
Define the UNIX Socket in opensips.cfg:
modparam("usrloc", "db_url", "mysql://opensips:opensipsrw@unix(/var/run/mysqld/mysqld.sock)/opensips)
Compatibility
This is a new feature so there are no backward compatibility issues.
The db_url parsing has been enhanced to support the UNIX Socket specification. If not specified, the existing db_url parsing logic will be used and remains unchanged.
Closing issues