{"id":4691,"date":"2019-09-05T03:19:34","date_gmt":"2019-09-05T03:19:34","guid":{"rendered":"http:\/\/myprojects.advchaweb.com\/?p=4691"},"modified":"2019-09-05T07:13:18","modified_gmt":"2019-09-05T07:13:18","slug":"install-microsoft-sql-server-2017-on-ubuntu-16-04","status":"publish","type":"post","link":"https:\/\/myprojects.advchaweb.com\/index.php\/2019\/09\/05\/install-microsoft-sql-server-2017-on-ubuntu-16-04\/","title":{"rendered":"Install Microsoft Sql Server 2017 And The PHP Extension On Ubuntu 16.04"},"content":{"rendered":"<p>Ref: https:\/\/www.microsoft.com\/en-us\/sql-server\/developer-get-started\/php\/ubuntu<\/p>\n<p>1. Register the Microsoft Linux repositories and add their keys.<br \/>\nNote: it&#8217;d be better to use &#8216;sudo su&#8217; first!<\/p>\n<pre class=\"lang:default decode:true\">teddy@teddy:~$ curl https:\/\/packages.microsoft.com\/keys\/microsoft.asc | sudo apt-key add -\r\n  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\r\n                                 Dload  Upload   Total   Spent    Left  Speed\r\n100   983  100   983    0     0   2323      0 --:--:-- --:--:-- --:--:--  2329\r\nOK\r\n\r\nteddy@teddy:~$ curl https:\/\/packages.microsoft.com\/config\/ubuntu\/16.04\/mssql-server-2017.list | sudo tee \/etc\/apt\/sources.list.d\/mssql-server-2017.list\r\n  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\r\n                                 Dload  Upload   Total   Spent    Left  Speed\r\n100    91  100    91    0     0    102      0 --:--:-- --:--:-- --:--:--   102\r\ndeb [arch=amd64] https:\/\/packages.microsoft.com\/ubuntu\/16.04\/mssql-server-2017 xenial main<\/pre>\n<p>2. Install SQL Server.<br \/>\nNOTE: I USE &#8216;sudo apt-get install mssql-server=14.0.3192.2-2&#8217; INSTEAD OF &#8216;sudo apt-get install mssql-server&#8217; BECAUSE I GOT AN ERROR FOR SQLCMD &#8216;Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : TCP Provider: Error code 0x2746.&#8217;<br \/>\nREF: https:\/\/stackoverflow.com\/questions\/57265913\/error-tcp-provider-error-code-0x2746-during-the-sql-setup-in-linux-through-te<\/p>\n<pre class=\"lang:default decode:true\">sudo apt-get update\r\n\r\nsudo apt-get install mssql-server=14.0.3192.2-2\r\n...\r\nSetting up mssql-server (14.0.3223.3-15) ...\r\n\r\n+--------------------------------------------------------------+\r\nPlease run 'sudo \/opt\/mssql\/bin\/mssql-conf setup'\r\nto complete the setup of Microsoft SQL Server\r\n+--------------------------------------------------------------+\r\n\r\nSQL Server needs to be restarted in order to apply this setting. Please run\r\n'systemctl restart mssql-server.service'.\r\nProcessing triggers for libc-bin (2.23-0ubuntu10) ...<\/pre>\n<p>3. Setup your SQL Server.<br \/>\nNote: I choose &#8216;Developer&#8217; edition.<\/p>\n<pre class=\"lang:default decode:true\">teddy@teddy:~$ sudo \/opt\/mssql\/bin\/mssql-conf setup\r\nChoose an edition of SQL Server:\r\n  1) Evaluation (free, no production use rights, 180-day limit)\r\n  2) Developer (free, no production use rights)\r\n  3) Express (free)\r\n  4) Web (PAID)\r\n  5) Standard (PAID)\r\n  6) Enterprise (PAID)\r\n  7) Enterprise Core (PAID)\r\n  8) I bought a license through a retail sales channel and have a product key to enter.\r\n\r\nDetails about editions can be found at\r\nhttps:\/\/go.microsoft.com\/fwlink\/?LinkId=852748&clcid=0x409\r\n\r\nUse of PAID editions of this software requires separate licensing through a\r\nMicrosoft Volume Licensing program.\r\nBy choosing a PAID edition, you are verifying that you have the appropriate\r\nnumber of licenses in place to install and run this software.\r\n\r\nEnter your edition(1-8): 2\r\nThe license terms for this product can be found in\r\n\/usr\/share\/doc\/mssql-server or downloaded from:\r\nhttps:\/\/go.microsoft.com\/fwlink\/?LinkId=855862&clcid=0x409\r\n\r\nThe privacy statement can be viewed at:\r\nhttps:\/\/go.microsoft.com\/fwlink\/?LinkId=853010&clcid=0x409\r\n\r\nDo you accept the license terms? [Yes\/No]:yes\r\n\r\nEnter the SQL Server system administrator password: \r\nThe specified password does not meet SQL Server password policy requirements because it is too short. The password must be at least 8 characters\r\nEnter the SQL Server system administrator password: \r\nConfirm the SQL Server system administrator password: \r\nConfiguring SQL Server...\r\n\r\nForceFlush is enabled for this instance. \r\nForceFlush feature is enabled for log durability.\r\nCreated symlink from \/etc\/systemd\/system\/multi-user.target.wants\/mssql-server.service to \/lib\/systemd\/system\/mssql-server.service.\r\nSetup has completed successfully. SQL Server is now starting.<\/pre>\n<span class=\"rcp-restricted-content-message\">SORRY, ONLY ADMIN CAN SHOW THIS!<\/span><br \/>\nCheck the mssql-server service status<\/p>\n<pre class=\"lang:default decode:true \">teddy@teddy:~$ sudo service mssql-server status\r\n\u25cf mssql-server.service - Microsoft SQL Server Database Engine\r\n   Loaded: loaded (\/lib\/systemd\/system\/mssql-server.service; enabled; vendor pre\r\n   Active: active (running) since Kam 2019-09-05 10:37:58 WIB; 2min 40s ago\r\n     Docs: https:\/\/docs.microsoft.com\/en-us\/sql\/linux\r\n Main PID: 21988 (sqlservr)\r\n    Tasks: 142\r\n   Memory: 665.3M\r\n      CPU: 5.910s\r\n   CGroup: \/system.slice\/mssql-server.service\r\n           \u251c\u250021988 \/opt\/mssql\/bin\/sqlservr\r\n           \u2514\u250022018 \/opt\/mssql\/bin\/sqlservr\r\n\r\nSep 05 10:38:00 teddy sqlservr[21988]: [78B blob data]\r\nSep 05 10:38:00 teddy sqlservr[21988]: [84B blob data]\r\nSep 05 10:38:00 teddy sqlservr[21988]: [122B blob data]\r\nSep 05 10:38:00 teddy sqlservr[21988]: [145B blob data]\r\nSep 05 10:38:01 teddy sqlservr[21988]: [66B blob data]\r\nSep 05 10:38:01 teddy sqlservr[21988]: [75B blob data]\r\nSep 05 10:38:01 teddy sqlservr[21988]: [96B blob data]\r\nSep 05 10:38:01 teddy sqlservr[21988]: [100B blob data]\r\nSep 05 10:38:01 teddy sqlservr[21988]: [71B blob data]\r\nSep 05 10:38:01 teddy sqlservr[21988]: [124B blob data]<\/pre>\n<p>NOTE: ERROR LOG IN \/var\/opt\/mssql\/log\/error<br \/>\nMUST OPEN THE DIR WITH &#8216;sudo su&#8217;<br \/>\n4. Install the ODBC Driver and SQL Command Line Utility for SQL Server (SQLCMD)<\/p>\n<pre class=\"lang:default decode:true\">teddy@teddy:~$ sudo su\r\nroot@teddy:\/home\/teddy# curl https:\/\/packages.microsoft.com\/config\/ubuntu\/16.04\/prod.list &gt; \/etc\/apt\/sources.list.d\/mssql-release.list\r\n  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\r\n                                 Dload  Upload   Total   Spent    Left  Speed\r\n100    79  100    79    0     0     89      0 --:--:-- --:--:-- --:--:--    89\r\nroot@teddy:\/home\/teddy# exit\r\nexit\r\n\r\nsudo apt-get update\r\nsudo ACCEPT_EULA=Y apt-get install msodbcsql17 mssql-tools\r\necho 'export PATH=\"$PATH:\/opt\/mssql-tools\/bin\"' &gt;&gt; ~\/.bash_profile\r\necho 'export PATH=\"$PATH:\/opt\/mssql-tools\/bin\"' &gt;&gt; ~\/.bashrc\r\nsource ~\/.bashrc\r\nsudo apt-get install unixodbc-dev<\/pre>\n<p>After installing SQLCMD, you can connect to SQL Server using the following command: sqlcmd -S localhost -U sa -P Yourpassword<br \/>\n<span class=\"rcp-restricted-content-message\">SORRY, ONLY ADMIN CAN SHOW THIS!<\/span><\/p>\n<p>Install the PHP Driver for SQL Server<\/p>\n<pre class=\"lang:default decode:true\">teddy@teddy:~$ sudo pecl install sqlsrv\r\n...\r\nBuild process completed successfully\r\nInstalling '\/usr\/lib\/php\/20170718\/sqlsrv.so'\r\ninstall ok: channel:\/\/pecl.php.net\/sqlsrv-5.6.1\r\nconfiguration option \"php_ini\" is not set to php.ini location\r\nYou should add \"extension=sqlsrv.so\" to php.ini<\/pre>\n<p>THEN<\/p>\n<pre class=\"lang:default decode:true\">teddy@teddy:~$ sudo pecl install pdo_sqlsrv\r\n...\r\nBuild process completed successfully\r\nInstalling '\/usr\/lib\/php\/20170718\/pdo_sqlsrv.so'\r\ninstall ok: channel:\/\/pecl.php.net\/pdo_sqlsrv-5.6.1\r\nconfiguration option \"php_ini\" is not set to php.ini location\r\nYou should add \"extension=pdo_sqlsrv.so\" to php.ini<\/pre>\n<p>THEN<\/p>\n<pre class=\"lang:default decode:true\">teddy@teddy:~$ sudo su\r\nroot@teddy:\/home\/teddy# echo extension=pdo_sqlsrv.so &gt;&gt; `php --ini | grep \"Scan for additional .ini files\" | sed -e \"s|.*:\\s*||\"`\/30-pdo_sqlsrv.ini\r\nroot@teddy:\/home\/teddy# echo extension=sqlsrv.so &gt;&gt; `php --ini | grep \"Scan for additional .ini files\" | sed -e \"s|.*:\\s*||\"`\/20-sqlsrv.ini\r\nroot@teddy:\/home\/teddy# exit\r\nexit<\/pre>\n<p>NOTE: IT&#8217;D COPY THE FILES &#8217;20-sqlsrv.ini&#8217; AND &#8217;30-pdo_sqlsrv.ini&#8217; TO DIR \/etc\/php\/7.2\/cli\/conf.d\/<br \/>\nTHEN<br \/>\nMODIFY &#8216;php.ini&#8217; file for php7.2 fpm and (cli NO NEED???)<\/p>\n<pre class=\"lang:default decode:true\">sudo gedit \/etc\/php\/7.2\/fpm\/php.ini<\/pre>\n<p>THEN INSERT THESE TWO LINES:<\/p>\n<pre class=\"lang:default decode:true \">extension=sqlsrv.so\r\nextension=pdo_sqlsrv.so<\/pre>\n<p>NO NEED &#8211;&gt;PLS DO THE SAME FOR &#8216;cli&#8217;: sudo gedit \/etc\/php\/7.2\/cli\/php.ini<\/p>\n<p>THEN COPY THE TWO INI FILES FROM &#8216;cli&#8217; TO &#8216;fpm&#8217;<\/p>\n<pre class=\"lang:default decode:true \">sudo cp \/etc\/php\/7.2\/cli\/conf.d\/20-sqlsrv.ini \/etc\/php\/7.2\/fpm\/conf.d\/\r\nsudo cp \/etc\/php\/7.2\/cli\/conf.d\/30-pdo_sqlsrv.ini \/etc\/php\/7.2\/fpm\/conf.d\/<\/pre>\n<p>THEN RESTART PHP FPM<\/p>\n<pre class=\"lang:default decode:true \">sudo service php7.2-fpm restart<\/pre>\n<p>CHECK THEM IN phpinfo: http:\/\/localhost:8080\/phpinfo.php<\/p>\n<p>Create a database &#8216;SampleDB&#8217; for your application<\/p>\n<pre class=\"lang:default decode:true \">sqlcmd -S localhost -U sa -P Teddy@234 -Q \"CREATE DATABASE SampleDB;\"<\/pre>\n<p>The DB will be exist in \/var\/opt\/mssql\/data\/SampleDB.mdf and the log file.<\/p>\n<p>Create a PHP app that connects to SQL Server and executes queries<\/p>\n<pre class=\"lang:default decode:true \">teddy@teddy:~\/Documents\/works$ mkdir SqlServerSample\r\nteddy@teddy:~\/Documents\/works$ cd SqlServerSample\/<\/pre>\n<p>Create a new file &#8216;connect.php&#8217;<\/p>\n<pre class=\"lang:default decode:true \">&lt;?php\r\n    ini_set('display_errors', 1);\r\n    error_reporting(E_ALL);\r\n\r\n    $serverName = \"localhost\";\r\n    $connectionOptions = array(\r\n        \"Database\" =&gt; \"SampleDB\",\r\n        \"Uid\" =&gt; \"sa\",\r\n        \"PWD\" =&gt; \"Teddy@234\"\r\n    );\r\n    \/\/Establishes the connection\r\n    $conn = sqlsrv_connect($serverName, $connectionOptions);\r\n    if($conn)\r\n        echo \"OKAY, Connected again!\\n\";\r\n?&gt;<\/pre>\n<p>RUN ON CLI:<\/p>\n<pre class=\"lang:default decode:true \">teddy@teddy:~\/Documents\/works\/SqlServerSample$ php connect.php \r\nOKAY, Connected again!<\/pre>\n<p>NOTE: ON CLI I GOT THIS WARNING:<\/p>\n<pre class=\"lang:default decode:true \">PHP Warning:  PHP Startup: Unable to load dynamic library 'pdo_sqlsrv' (tried: \/usr\/lib\/php\/20170718\/pdo_sqlsrv (\/usr\/lib\/php\/20170718\/pdo_sqlsrv: cannot open shared object file: No such file or directory), \/usr\/lib\/php\/20170718\/pdo_sqlsrv.so (\/usr\/lib\/php\/20170718\/pdo_sqlsrv.so: undefined symbol: php_pdo_unregister_driver)) in Unknown on line 0\r\nPHP Warning:  Module 'sqlsrv' already loaded in Unknown on line 0\r\nOKAY, Connected!<\/pre>\n<p>SOLUTION: REMOVE\/COMMENT THE EXTENSIONS &#8216;sqlsvr&#8217; AND &#8216;pdo_sqlsvr&#8217; IN \/etc\/php\/7.2\/cli\/php.ini FILES!<br \/>\nCHECK THE LOADED MODULE WITH: php -m<br \/>\nIT SHOULD BE ALREADY LOADED FOR CLI SO NO NEED TO INCLUDE THEM IN CLI php.ini BUT IT NEED TO INCLUDE FOR FPM php.ini !<\/p>\n<p>ALSO RUN VIA WEB: http:\/\/localhost:8080\/works\/SqlServerSample\/connect.php<\/p>\n<p>&nbsp;<\/p>\n<p>UNINSTALL MS SQL SERVER<br \/>\nREF: https:\/\/dba.stackexchange.com\/questions\/174175\/how-do-i-totally-remove-sql-server-2017-on-ubuntu<\/p>\n<blockquote class=\"wp-embedded-content\" data-secret=\"a89fS7H82m\"><p><a href=\"https:\/\/www.sqlshack.com\/uninstalling-sql-server-on-ubuntu\/\">Uninstalling SQL Server on Ubuntu<\/a><\/p><\/blockquote>\n<p><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" src=\"https:\/\/www.sqlshack.com\/uninstalling-sql-server-on-ubuntu\/embed\/#?secret=a89fS7H82m\" data-secret=\"a89fS7H82m\" width=\"600\" height=\"338\" title=\"&#8220;Uninstalling SQL Server on Ubuntu&#8221; &#8212; SQL Shack - articles about database auditing, server performance, data recovery, and more\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/p>\n<pre class=\"lang:default decode:true \">sudo service mssql-server stop\r\n\r\nsudo add-apt-repository --remove \"$(curl https:\/\/packages.microsoft.com\/config\/ubuntu\/16.04\/mssql-server-2017.list)\"\r\n\r\n# Remove those packages from the system.\r\nsudo apt-get --purge remove mssql-server mssql-server-fts mssql-tools msodbcsql unixodbc-dev\r\nsudo apt-get remove mssql-server msodbcsql unixodbc-dev\r\n\r\n# Delete the SQL Server repositories\r\nsudo rm \/etc\/apt\/sources.list.d\/mssql-* \/etc\/apt\/sources.list.d\/msprod*\r\n\r\n# Remove the Microsoft keys.\r\nsudo apt-key del \"BC52 8686 B50D 79E3 39D3  721C EB3E 94AD BE12 29CF\"\r\n\r\n# Delete the Microsoft created directories\r\nsudo rm -rf \/var\/opt\/mssql\/ \/opt\/{microsoft,mssql-tools,mssql}\/ \/usr\/share\/doc\/{msodbcsql,mssql-server,mssql-tools}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ref: https:\/\/www.microsoft.com\/en-us\/sql-server\/developer-get-started\/php\/ubuntu 1. Register the Microsoft Linux repositories and add their keys. Note: it&#8217;d be better to use &#8216;sudo su&#8217; first! teddy@teddy:~$ curl https:\/\/packages.microsoft.com\/keys\/microsoft.asc | sudo apt-key add &#8211; % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 983 100 983 0 0 2323 0 &hellip; <a href=\"https:\/\/myprojects.advchaweb.com\/index.php\/2019\/09\/05\/install-microsoft-sql-server-2017-on-ubuntu-16-04\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Install Microsoft Sql Server 2017 And The PHP Extension On Ubuntu 16.04&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[103,17,78],"tags":[],"class_list":["post-4691","post","type-post","status-publish","format-standard","hentry","category-ms-sql-server","category-ubuntu","category-ubuntu-16-04"],"_links":{"self":[{"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/posts\/4691","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/comments?post=4691"}],"version-history":[{"count":18,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/posts\/4691\/revisions"}],"predecessor-version":[{"id":4709,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/posts\/4691\/revisions\/4709"}],"wp:attachment":[{"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/media?parent=4691"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/categories?post=4691"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/tags?post=4691"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}