<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>David Barbarin &#187; WSL2</title>
	<atom:link href="https://blog.developpez.com/mikedavem/ptag/wsl2/feed" rel="self" type="application/rss+xml" />
	<link>https://blog.developpez.com/mikedavem</link>
	<description>MVP DataPlatform - MCM SQL Server</description>
	<lastBuildDate>Thu, 09 Sep 2021 21:19:50 +0000</lastBuildDate>
	<language>fr-FR</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.42</generator>
	<item>
		<title>Dealing with SQL Server on Linux on WSL2</title>
		<link>https://blog.developpez.com/mikedavem/p13196/sql-server-2012/dealing-with-sql-server-on-linux-on-wsl2</link>
		<comments>https://blog.developpez.com/mikedavem/p13196/sql-server-2012/dealing-with-sql-server-on-linux-on-wsl2#comments</comments>
		<pubDate>Mon, 27 Jul 2020 06:53:32 +0000</pubDate>
		<dc:creator><![CDATA[mikedavem]]></dc:creator>
				<category><![CDATA[SQL Server 2012]]></category>
		<category><![CDATA[Init]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[sqlserver]]></category>
		<category><![CDATA[start-stop-daemon]]></category>
		<category><![CDATA[WSL]]></category>
		<category><![CDATA[WSL2]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/mikedavem/?p=1626</guid>
		<description><![CDATA[This is a blog post I intended to write sometime ago &#8230; about using SQL Server on Linux on WSL2. For whom already installed it on Windows 10, version 2004, you are already aware it doesn’t come with the support &#8230; <a href="https://blog.developpez.com/mikedavem/p13196/sql-server-2012/dealing-with-sql-server-on-linux-on-wsl2">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>This is a blog post I intended to write sometime ago &#8230; about using SQL Server on Linux on WSL2. For whom already installed it on Windows 10, version 2004, you are already aware it doesn’t come with the support of systemd. Indeed, although it does exist in the file system, systemd is not running. If you intend to use directly SQL Server on WSL2, you need to read this carefully, because installation and management relies precisely on systemd!</p>
<p><span id="more-1626"></span></p>
<p><a href="http://blog.developpez.com/mikedavem/files/2020/07/163-0-banner.jpg"><img src="http://blog.developpez.com/mikedavem/files/2020/07/163-0-banner.jpg" alt="163 - 0 - banner" width="508" height="187" class="alignnone size-full wp-image-1639" /></a></p>
<p>First, let&rsquo;s say if you want to run SQL Server on WSL2 in a supported way, docker containers remain probably the better fit. Podman may be also an alternative but it seems to remain an experimental stuff so far. The interesting point is that some people prefer to use Docker directly on WSL2 whereas other ones prefer to use Docker Desktop for Windows with the integration of WSL2 based engine. The option seems to be available for a few months as shown below:</p>
<p><a href="http://blog.developpez.com/mikedavem/files/2020/07/163-1-Docker-Desktop-WSL2.jpg"><img src="http://blog.developpez.com/mikedavem/files/2020/07/163-1-Docker-Desktop-WSL2.jpg" alt="163 - 1 - Docker Desktop WSL2" width="824" height="495" class="alignnone size-full wp-image-1627" /></a></p>
<p>But this is not the main topic of this blog post. Having interesting discussions with some of my colleagues, we wanted to know if we were able to install directly SQL Server on WSL2 and the good news is we finally managed to do it but at the cost of some (dirty) tricks required to make SQL Server starting / stopping correctly.</p>
<p>Before continuing, let’s precise we did it in a <strong>pure experimental and academic way</strong> and this is definitely <strong>not supported by Microsoft</strong>. But before starting my holidays in August, it was something very fun and a good reminder of some Linux concepts … </p>
<p>I did my test with the Ubuntu-18.04 distro in WSL v2.</p>
<p><a href="http://blog.developpez.com/mikedavem/files/2020/07/163-2-WSL2-Ubuntu18.04.jpg"><img src="http://blog.developpez.com/mikedavem/files/2020/07/163-2-WSL2-Ubuntu18.04.jpg" alt="163 - 2 - WSL2 Ubuntu18.04" width="387" height="86" class="alignnone size-full wp-image-1629" /></a></p>
<p>First step consisted in installing SQL Server in an usual way as per the <a href="https://docs.microsoft.com/en-us/sql/linux/quickstart-install-connect-ubuntu?view=sql-server-ver15" rel="noopener" target="_blank">Microsoft BOL</a>. The installation was done correctly but after running the setup, an error message raised quickly in the last step related to service initialization:</p>
<p><a href="http://blog.developpez.com/mikedavem/files/2020/07/163-3-install-sql.jpg"><img src="http://blog.developpez.com/mikedavem/files/2020/07/163-3-install-sql.jpg" alt="163 - 3 - install sql" width="716" height="96" class="alignnone size-full wp-image-1630" /></a></p>
<p>In fact, if you think about it, this message is expected because systemd is not running on WSL2 and the configuration process attempts to initialize the mssql-server service based on systemd. But let&rsquo;s dig further on the installation process &#8230; Referring to how a deb package is made, there exist preinst, postinst, prerm, postrm scripts for mssqlserver deb package located in <strong>/var/lib/dpkg/info/</strong>:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:650px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ sudo ls -l /var/lib/dpkg/info/mssql-server*<br />
-rwxr-xr-x 1 root root &nbsp; 108 Jun 16 20:01 /var/lib/dpkg/info/mssql-server.config<br />
-rw-r--r-- 1 root root &nbsp;8579 Jul 26 00:31 /var/lib/dpkg/info/mssql-server.list<br />
-rw-r--r-- 1 root root 11543 Jun 16 20:01 /var/lib/dpkg/info/mssql-server.md5sums<br />
-rwxr-xr-x 1 root root &nbsp;1436 Jun 16 20:01 /var/lib/dpkg/info/mssql-server.postinst<br />
-rwxr-xr-x 1 root root &nbsp; 289 Jun 16 20:01 /var/lib/dpkg/info/mssql-server.postrm<br />
-rwxr-xr-x 1 root root &nbsp;1353 Jun 16 20:01 /var/lib/dpkg/info/mssql-server.preinst<br />
-rwxr-xr-x 1 root root &nbsp; 365 Jun 16 20:01 /var/lib/dpkg/info/mssql-server.prerm<br />
-rw-r--r-- 1 root root &nbsp; &nbsp;72 Jun 16 20:01 /var/lib/dpkg/info/mssql-server.shlibs<br />
-rw-r--r-- 1 root root &nbsp; 305 Jun 16 20:00 /var/lib/dpkg/info/mssql-server.templates<br />
-rw-r--r-- 1 root root &nbsp; &nbsp;74 Jun 16 20:01 /var/lib/dpkg/info/mssql-server.triggers</div></div>
<p>My suspicion was those files content references of systemctl commands and yes, there were in preinst/prerm/postins/prerm files:</p>
<p><a href="http://blog.developpez.com/mikedavem/files/2020/07/163-4-install-files-and-systemctl-dependencies-e1595827795971.jpg"><img src="http://blog.developpez.com/mikedavem/files/2020/07/163-4-install-files-and-systemctl-dependencies-e1595827795971.jpg" alt="163 - 4 - install files and systemctl dependencies" width="800" height="170" class="alignnone size-full wp-image-1631" /></a></p>
<p>Well, we found a reasonable explanation of the previous error message at least. At this stage, I would say you can use your new fresh installed SQL Server instance, but it requires to manually start / stop it because there are no systemctl commands to handle it. But thinking about it … we are still using the famous init daemon (PID1) on WSL2 and it works for regular services. </p>
<p><a href="http://blog.developpez.com/mikedavem/files/2020/07/163-5-top-tree-init.jpg"><img src="http://blog.developpez.com/mikedavem/files/2020/07/163-5-top-tree-init.jpg" alt="163 - 5 - top tree init" width="812" height="142" class="alignnone size-full wp-image-1632" /></a></p>
<p>A good alternative could be to rely on init scripts and <a href="on start-stop-daemon wrapped into LSB-compliant init scripts with" rel="noopener" target="_blank">start-stop-daemon</a> wrapped into LSB-compliant init scripts with:</p>
<p>&#8211;	At least start, stop, restart, force-reload, and status<br />
&#8211;	Return Proper exit code<br />
&#8211;	Document run-time dependencies</p>
<p>LSB provides default set of functions which is in <strong>/lib/lsb/init-functions</strong> and we can make use of those functions in our Init scripts. The script file is located to <strong>/etc/init.d</strong> and named mssql-server.</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:650px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ sudo ls -l /etc/init.d/mssql*<br />
-rwxr-xr-x 1 root root 1606 Jul 26 22:13 /etc/init.d/mssql-server</div></div>
<p>Here the content of my mssql-server script file:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:650px;height:450px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">#! /bin/sh -e<br />
#<br />
### BEGIN INIT INFO<br />
# Provides: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sqlserver<br />
# Required-Start: &nbsp; &nbsp;$all<br />
# Required-Stop:<br />
# Default-Start: &nbsp; &nbsp; 2 3 4 5<br />
# Default-Stop: &nbsp; &nbsp; &nbsp;0 1 6<br />
# Short-Description: Manages SQL Server instance on Linux<br />
### END INIT INFO<br />
<br />
DAEMON=&quot;/opt/mssql/bin/sqlservr&quot;<br />
daemon_OPT=&quot;&quot;<br />
DAEMONUSER=&quot;mssql&quot;<br />
daemon_NAME=&quot;sqlservr&quot;<br />
<br />
export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/var/opt/mssql<br />
<br />
# Check sqlserver is present<br />
if [ ! -x $DAEMON ]; then<br />
&nbsp; &nbsp; &nbsp; &nbsp; log_failure_msg &quot;$DAEMON not present or not executable&quot;<br />
&nbsp; &nbsp; &nbsp; &nbsp; exit 1<br />
fi<br />
<br />
# Load init functions<br />
. /lib/lsb/init-functions<br />
<br />
<br />
d_start () {<br />
&nbsp; &nbsp; &nbsp; &nbsp; log_daemon_msg &quot;Starting system $daemon_NAME Daemon&quot;<br />
&nbsp; &nbsp; &nbsp; &nbsp; start-stop-daemon --background --name $daemon_NAME --start --quiet --chuid $DAEMONUSER --exec $DAEMON --umask 007 -- $DAEMON_OPTS --oknodo<br />
&nbsp; &nbsp; &nbsp; &nbsp; log_end_msg $?<br />
}<br />
<br />
d_stop () {<br />
&nbsp; &nbsp; &nbsp; &nbsp; log_daemon_msg &quot;Stopping system $daemon_NAME Daemon&quot;<br />
&nbsp; &nbsp; &nbsp; &nbsp; start-stop-daemon --name $daemon_NAME --stop --retry 5 --quiet --name $daemon_NAME --oknodo<br />
&nbsp; &nbsp; &nbsp; &nbsp; log_end_msg $?<br />
}<br />
<br />
case &quot;$1&quot; in<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; start|stop)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; d_${1}<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ;;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; restart|reload|force-reload)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; d_stop<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; d_start<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ;;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; force-stop)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;d_stop<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ;;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; status)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; status_of_proc &quot;$daemon_NAME&quot; &quot;$DAEMON&quot; &quot;system-wide $daemon_NAME&quot; &amp;amp;&amp;amp; exit 0 || exit $?<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ;;<br />
&nbsp; &nbsp; &nbsp; &nbsp; *)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo &quot;Usage: /etc/init.d/$daemon_NAME {start|stop|force-stop|restart|reload|force-reload|status}&quot;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit 1<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ;;<br />
esac<br />
exit 0</div></div>
<p>Thus, I was now able to manage with status / start / stop / restart operations:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:650px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ sudo service mssql-server status<br />
&nbsp;/opt/mssql/bin/sqlservr is not running<br />
$ sudo service mssql-server start<br />
&nbsp;Starting system sqlservr Daemon &nbsp; &nbsp; &nbsp; &nbsp; [ OK ]</div></div>
<p>The SQL Server engine was started successfully, and I can double check with the top command &#8230;</p>
<p><a href="http://blog.developpez.com/mikedavem/files/2020/07/163-6-top-sqlservr-e1595828331414.jpg"><img src="http://blog.developpez.com/mikedavem/files/2020/07/163-6-top-sqlservr-e1595828331414.jpg" alt="163 - 6 - top sqlservr" width="800" height="303" class="alignnone size-full wp-image-1634" /></a></p>
<p>&#8230; and with a quick connect to my SQL Server instance:</p>
<p><a href="http://blog.developpez.com/mikedavem/files/2020/07/163-7-mssql-cli-e1595828387503.jpg"><img src="http://blog.developpez.com/mikedavem/files/2020/07/163-7-mssql-cli-e1595828387503.jpg" alt="163 - 7 - mssql-cli" width="800" height="257" class="alignnone size-full wp-image-1635" /></a></p>
<p>As starting my SQL Server instance, stopping it can be performed with the service command and stop:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:650px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ sudo service mssql-server stop<br />
&nbsp;Stopping system sqlservr Daemon &nbsp; &nbsp; &nbsp; &nbsp; [ OK ]<br />
<br />
$ sudo service mssql-server status<br />
/opt/mssql/bin/sqlservr is not running</div></div>
<p>Generally speaking, there is no concept of runlevel with WSL, so starting automatically the mssql-service with the WSL2 startup can be achieved in different ways with entries in .bashrc file or tricks from <a href="https://github.com/shayne/wsl2-hacks/blob/master/README.md" rel="noopener" target="_blank">github projects</a> (not tested from my side). </p>
<p>Finally, let’s talk about removing SQL Server from WSL2. As a reminder, during the installation / setup we faced an error message related to systemd. We also identified some dependencies to systemd in some dkpg files as shown below:</p>
<p><a href="http://blog.developpez.com/mikedavem/files/2020/07/163-4-install-files-and-systemctl-dependencies-e1595827795971.jpg"><img src="http://blog.developpez.com/mikedavem/files/2020/07/163-4-install-files-and-systemctl-dependencies-e1595827795971.jpg" alt="163 - 4 - install files and systemctl dependencies" width="800" height="170" class="alignnone size-full wp-image-1631" /></a></p>
<p>Similar to the installation step, you will experience the same kind of issue with prerm / postrm files when removing SQL Server. In my case, I had to comment concerned lines in those files to uninstall successfully my SQL Server instance. </p>
<p>Hope this blog post helps! </p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
