-
-
Notifications
You must be signed in to change notification settings - Fork 158
Add support for Microsoft SQL Server #168
Changes from all commits
bf46cc9
2d88144
0090956
ca54dcd
5af58de
953c6c8
51fe2e5
7d325d9
a583d23
9dc60a4
975b485
822cce1
b5bd7fd
ef44944
1bc6f71
9cb6979
de6f708
00f4d9c
b380cf5
9878984
9cc6792
65f62e2
33cf0c1
1f0f77a
d9877e6
06cb200
4d12974
824508f
fb161d8
d7e87eb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
ARG BASE_IMAGE | ||
FROM $BASE_IMAGE | ||
|
||
ENV PATH="${PATH}:/opt/mssql-tools/bin" | ||
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y -o Dpkg::Options::="--force-confold" --no-install-recommends --no-install-suggests apt-utils curl gnupg2 ca-certificates | ||
|
||
RUN curl -sSL -O https://packages.microsoft.com/keys/microsoft.asc | ||
RUN apt-key add <microsoft.asc | ||
RUN curl -sSL -o /etc/apt/sources.list.d/mssql-release.list https://packages.microsoft.com/config/debian/11/prod.list | ||
|
||
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y -o Dpkg::Options::="--force-confold" --no-install-recommends --no-install-suggests build-essential dialog php-pear php-dev unixodbc-dev locales | ||
|
||
RUN ACCEPT_EULA=Y DEBIAN_FRONTEND=noninteractive apt-get install -y msodbcsql18 mssql-tools | ||
|
||
# Change the PHP version to what you want. It is currently set to version 8.0. | ||
RUN pecl channel-update pecl.php.net | ||
RUN pecl -d php_suffix=8.0 install sqlsrv | ||
RUN pecl -d php_suffix=8.0 install pdo_sqlsrv | ||
|
||
RUN echo 'extension=sqlsrv.so' >> "/etc/php/8.0/mods-available/sqlsrv.ini" | ||
RUN echo 'extension=pdo_sqlsrv.so' >> "/etc/php/8.0/mods-available/pdo_sqlsrv.ini" | ||
|
||
RUN phpenmod sqlsrv pdo_sqlsrv |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
# SQL Server (Microsoft) | ||
|
||
Using SQL server from Microsoft. | ||
|
||
## Installation | ||
|
||
1. Copy `docker-compose.sqlsrv.yaml` to your project's `.ddev` directory. | ||
2. Copy `Dockerfile` to your project's `.devv/web-build` directory. | ||
3. Copy the full post start hook commands to your project's `config.yaml` or add them to `config.sqlsrv.yaml`. | ||
4. *(optional)* For Drupal 9+ projects: copy `install-drupal-regex-function.sh` to your project's `.ddev` directory. | ||
|
||
## Connection | ||
|
||
Connect to `sqlsrv` host/db server from within the web container with: | ||
|
||
``` | ||
Host: `ddev-projectname-sqlsrv` | ||
User: sa | ||
Password: password! | ||
Database: master | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it important for it to be named "master"? Again, the default mysql and postgres setups use "db" for the name of the primary database. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It does not need to be master, it is only the default database for SQL Server. The "db" database needs to be created. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah if 'master' is default database then it makes sense to use it. |
||
``` | ||
|
||
Connect to `sqlsrv` host/db server from project directory: | ||
|
||
```bash | ||
ddev exec -s sqlsrv "/opt/mssql-tools/bin/sqlcmd -P password! -S localhost -U sa -d master" | ||
``` | ||
|
||
For external access, use the port used in your `docker-compose.sqlsrv.yaml` and `127.0.0.1` as host. | ||
|
||
When using multiple databases in your project with SQL Server support, remember to update your `docker-compose.sqlsrv.yaml` to use different ports: | ||
|
||
```yaml | ||
ports: | ||
- <EXTERNAL_PORT>:1433 | ||
``` | ||
|
||
## Installing the PHP extensions | ||
|
||
The PHP extensions for SQL Server CANNOT be installed by adding them to the `webimage_extra_packages` setting. The problem is that they are not available as a Debian or any other distribution package. The 2 extensions (`sqlsrv` and `pdo_sqlsrv`) need to be compiled and this needs to be done after PHP is installed on the webimage. The following commands need to be copied to the end of the main `config.yaml` file: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please do this with a .ddev/web-build/Dockerfile instead - it's far more efficient (only has to be done once, instead of on every There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have tried to do that, only when I did that there was no PHP installed yet, therefor it failed. Maybe I did something wrong or there is a way to do it after PHP is installed. For me, I was fantastic that I got it to work in the first place. It took me 2 weeks to get it to work. I just wanted to fix some bugs in the Drupal module sqlsrv. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will be happy to help with this of course. And appreciate you sharing the work that you did! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The main thing for me was to make it a lot easier for the people after me that would like to use DDEV with SQL Server. For some reason the PHP extension for SQL Server are not available as packages. Really annoying! |
||
|
||
```yaml | ||
hooks: | ||
post-start: | ||
- exec: echo export PATH="$PATH:/opt/mssql-tools/bin" >> ~/.bashrc | ||
- exec: source ~/.bashrc | ||
``` | ||
|
||
The minimum required PHP version the these extensions is PHP 7.3. For more information about these extension, see: [MS SQL driver for PHP](https://github.com/microsoft/msphpsql). | ||
|
||
|
||
## Drupal Notice | ||
|
||
Drupal CMS needs the a database function installed that is mimicking the Regex function as Drupal requires. As a one-time setup for Drupal, install the database function by running the following command from your project's directory: | ||
|
||
```bash | ||
./install-drupal-regex-function.sh -u <username> -p <password> -d <database> | ||
``` | ||
|
||
This script also changes the setting for the following database variables: | ||
* `show advanced options` will be set to 1 | ||
* `clr strict security` will be set to 0 | ||
* `clr enable` will be set to 1 | ||
|
||
Drupal also the module `sqlsrv` to be installed as it is providing the database driver for SQL Server. The module can be installed with composer with the following command: | ||
|
||
```bash | ||
$ php composer require drupal/sqlsrv | ||
``` | ||
|
||
## Disabling MySQL & MariaSQL | ||
|
||
* If your project only uses a SQL Server database, you can disable the MySql & MariaDb services. | ||
* Run the following command from your project root. | ||
|
||
```bash | ||
ddev config --omit-containers db | ||
``` | ||
|
||
* Alternatively, you can update your project's `.ddev/config.yaml` directly by updating the following line: | ||
|
||
```yaml | ||
omit_containers: [db] | ||
``` | ||
|
||
* See [.ddev/config.yaml Options](https://ddev.readthedocs.io/en/stable/users/extend/config_yaml/) for additional notes. | ||
|
||
## TODO | ||
|
||
Future enhancements (PR's welcome here) include: | ||
|
||
* Provide custom commands. | ||
|
||
## Links with useful information | ||
|
||
* [SQL Server docker hub](https://hub.docker.com/_/microsoft-mssql-server) | ||
* [Installing the ODBC driver for SQL Server](https://docs.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server) | ||
* [Installing the ODBC driver for SQL Server Tutorial](https://docs.microsoft.com/en-us/sql/connect/php/installation-tutorial-linux-mac) | ||
* [Installation tutorial for MS drivers for PHP](https://docs.microsoft.com/en-us/sql/connect/php/installation-tutorial-linux-mac) | ||
* [The SQLCMD utility](https://docs.microsoft.com/en-us/sql/tools/sqlcmd-utility) | ||
* [The SQL Server on Linux](https://docs.microsoft.com/en-us/sql/linux/sql-server-linux-overview) | ||
* [The password policy](https://docs.microsoft.com/en-us/sql/relational-databases/security/password-policy) | ||
* [The SQL Server environment variables](https://docs.microsoft.com/en-us/sql/linux/sql-server-linux-configure-environment-variables) | ||
* [Beakerboy's Drupal Regex database function](https://github.com/Beakerboy/drupal-sqlsrv-regex) | ||
* [Drupal's module for the SQL Server](https://www.drupal.org/project/sqlsrv) | ||
* [Github MS drivers for PHP](https://github.com/microsoft/msphpsql) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
version: '3.6' | ||
|
||
services: | ||
sqlsrv: | ||
container_name: ddev-${DDEV_SITENAME}-sqlsrv | ||
hostname: ddev-${DDEV_SITENAME}-sqlsrv | ||
|
||
# For possible options: https://hub.docker.com/_/microsoft-mssql-server. | ||
# This does not yet work with Apple M1. See: https://github.com/microsoft/mssql-docker/issues/734 | ||
image: mcr.microsoft.com/mssql/server:2019-CU12-ubuntu-20.04 | ||
|
||
user: root | ||
volumes: | ||
- sqlsystem:/var/opt/mssql/ | ||
restart: "no" | ||
ports: | ||
- "1433:1433" | ||
labels: | ||
com.ddev.site-name: ${DDEV_SITENAME} | ||
com.ddev.approot: $DDEV_APPROOT | ||
environment: | ||
|
||
# With the following setting you agree with Microsoft's end user license | ||
# agreement. | ||
- 'ACCEPT_EULA=Y' | ||
|
||
# The SQL Server enforces password complexity. Trying to set a password | ||
# that is not sufficiently complex will result in the password not be set. | ||
# As a result you cannot login with the "SA" user account. | ||
# The password for the SA account needs to follow the following policy: | ||
# https://docs.microsoft.com/en-us/sql/relational-databases/security/password-policy?view=sql-server-ver15 | ||
- 'SA_PASSWORD=Password12!' | ||
|
||
# The following setting is for selecting the SQL Server edition or product | ||
# key. For possible options: | ||
# https://docs.microsoft.com/en-us/sql/linux/sql-server-linux-configure-environment-variables?view=sql-server-ver15 | ||
- 'MSSQL_PID=Evaluation' | ||
|
||
# Sets the default collation for SQL Server. This setting is optional. The | ||
# current setting is best for Drupal projects. For more information, see: | ||
# https://docs.microsoft.com/en-us/sql/relational-databases/collations/collation-and-unicode-support?view=sql-server-ver15 | ||
- 'MSSQL_COLLATION=LATIN1_GENERAL_100_CI_AS_SC_UTF8' | ||
|
||
# For more possible environment variables that can be set, see: | ||
# https://docs.microsoft.com/en-us/sql/relational-databases/security/password-policy?view=sql-server-ver15 | ||
|
||
web: | ||
links: | ||
- sqlsrv:sqlsrv | ||
|
||
volumes: | ||
sqlsystem: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am more of a programmer than somebody who does production. Therefor if you think that is best then please do so. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't about programming, it's about where the mounts are. Since I know nothing about sqlsrv, I don't know why you are using volumes for all these things. Is is possible that only the database is actually required to be persistent, and that's sqldata, and it could be called sqlsrv? You only need the volumes for stuff that's persistent. On mysql and postgres, that's just the database itself. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated the SQL Server docker file. Removed the other volumes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cool, thanks! |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#!/bin/bash | ||
# Installs the Regex database function for compatibility with Drupal version 9 or higher. | ||
# This script only needs to be run once for a database. | ||
|
||
usage() { echo "./install-drupal-regex-function.sh -u <username> -p <password> -d <database>" 1>&2; exit 1; } | ||
|
||
# The password for the database defaults to "" | ||
password="" | ||
|
||
# The username for the database defaults to "SA" | ||
username="SA" | ||
|
||
# The database name defaults to "master" | ||
database="master" | ||
|
||
while getopts ":u:p:d:h:" arg; do | ||
case $arg in | ||
u) | ||
username=${OPTARG} | ||
;; | ||
p) | ||
password=${OPTARG} | ||
;; | ||
d) | ||
database=${OPTARG} | ||
;; | ||
h) | ||
usage | ||
;; | ||
*) | ||
usage | ||
;; | ||
esac | ||
done | ||
|
||
if [ -z $password ] | ||
then | ||
echo "The parameter -p for the password is not set." | ||
exit 1 | ||
fi | ||
|
||
if ! ddev exec -s sqlsrv test -e "/RegEx.dll" &>/dev/null; | ||
then | ||
# Download the Regex.dll file and copy it to the right location. | ||
ddev exec -s sqlsrv wget https://github.com/Beakerboy/drupal-sqlsrv-regex/releases/download/1.0/RegEx.dll | ||
fi | ||
|
||
# The following are changed to allow the installation and execution of the user provided database function. | ||
ddev exec -s sqlsrv "/opt/mssql-tools/bin/sqlcmd -P $password -S localhost -U $username -d $database -Q 'EXEC sp_configure \"show advanced options\", 1; RECONFIGURE; EXEC sp_configure \"clr strict security\", 0; RECONFIGURE; EXEC sp_configure \"clr enable\", 1; RECONFIGURE;'" | ||
|
||
# Create the assambly and the function for the Regex helper. | ||
ddev exec -s sqlsrv "/opt/mssql-tools/bin/sqlcmd -P $password -S localhost -U $username -d $database -Q 'CREATE ASSEMBLY Regex from \"/RegEx.dll\" WITH PERMISSION_SET = SAFE'" | ||
ddev exec -s sqlsrv "/opt/mssql-tools/bin/sqlcmd -P $password -S localhost -U $username -d $database -Q 'CREATE FUNCTION dbo.REGEXP(@pattern NVARCHAR(100), @matchString NVARCHAR(100)) RETURNS bit EXTERNAL NAME Regex.RegExCompiled.RegExCompiledMatch'" | ||
drupal-daffie marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# **Contributed by [@drupal-daffie](https://github.com/drupal-daffie)** |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the mysql and postgres db setups use
db
as username and password, could we use that here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We cannot use the password "db" as it would fail the restrictions set by SQL Server. See: https://docs.microsoft.com/en-us/sql/relational-databases/security/password-policy?view=sql-server-ver15
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, but could use the username 'db' true?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The default user on SQL Server is the user "sa". I think it stands for "system administrator", a.k.a. the root user on linux. An other user needs to be created with the SQL string "CREATE USER 'db' WITH PASSWORD 'db'". Is that what you would like to do? In this way we can also create the database "db".