Install Microsoft SQL Server 2022 On Ubuntu 22.04

Ref: https://learn.microsoft.com/en-us/sql/linux/quickstart-install-connect-ubuntu?view=sql-server-ver16&tabs=ubuntu2204

1. Update and upgrade the packages on ubuntu 22.04
[codesyntax lang=”bash”]

[/codesyntax]

2. Download the public key, convert from ASCII to GPG format, and write it to the required location:
[codesyntax lang=”bash”]

[/codesyntax]

3. Manually download and register the SQL Server Ubuntu repository:
[codesyntax lang=”bash”]

[/codesyntax]

4. Run the following commands to install SQL Server:
[codesyntax lang=”bash”]

[/codesyntax]

5. After the package installation finishes, run mssql-conf setup and follow the prompts to set the sa password and choose your edition.
Just choose ‘Developer’ version. type 2. My local sa password is {my-nick-name-first-uppercase}@123
[codesyntax lang=”bash”]

[/codesyntax]

6. Once the configuration is done, verify that the service is running:
[codesyntax lang=”bash”]

[/codesyntax]

Install the SQL Server command-line tools (sqlcmd)

1. Import the public repository GPG keys.
[codesyntax lang=”bash”]

[/codesyntax]

2. Register the Microsoft Ubuntu repository. For Ubuntu 22.04, use the following command:
[codesyntax lang=”bash”]

[/codesyntax]

3. Update the sources list and run the installation command with the unixODBC developer package.
[codesyntax lang=”bash”]

[/codesyntax]

To update to the latest version of mssql-tools, run the following commands:
[codesyntax lang=”bash”]

[/codesyntax]

4. Add /opt/mssql-tools18/bin/ to your PATH environment variable in a bash shell.

To make sqlcmd and bcp accessible from the bash shell for login sessions, modify your PATH in the ~/.bash_profile file with the following command:
[codesyntax lang=”bash”]

[/codesyntax]

5. Test sqlcmd with ‘sqlcmd -?’
[codesyntax lang=”bash”]

[/codesyntax]

Connect locally. I tried to use : sqlcmd -S localhost -U sa -P your_password
but it showed up this error
[codesyntax lang=”bash”]

[/codesyntax]

it turned out I need to add -C parameter at the end like this
[codesyntax lang=”bash”]

[/codesyntax]

Create a new database

For example create a new database TestDB from sqlcmd
[codesyntax lang=”bash”]

[/codesyntax]

Create a new table dbo.Inventory and Insert data
[codesyntax lang=”bash”]

[/codesyntax]

Install DBeaver (https://dbeaver.io/)
This is Database GUI for many databases including MS SQL Server

Download it https://dbeaver.io/download/
I used Ubuntu PPA
[codesyntax lang=”bash”]

[/codesyntax]

Open DBeaver CE and select to the MS SQL Server

Connect to the MS SQL Server but it needs to use JDBC driver. Install it directly from the DBeaver

If everything is okay, it’ll look like this

on the DBeaver GUI you can add a new row. for example add a new row
ID: 3
Name: Apple
Quantity: 158
Then save

on the sqlcmd should reflect the changes
[codesyntax lang=”bash”]

[/codesyntax]

Install Microsoft Sql Server 2017 And The PHP Extension On Ubuntu 16.04

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’d be better to use ‘sudo su’ first!

2. Install SQL Server.
NOTE: I USE ‘sudo apt-get install mssql-server=14.0.3192.2-2’ INSTEAD OF ‘sudo apt-get install mssql-server’ BECAUSE I GOT AN ERROR FOR SQLCMD ‘Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : TCP Provider: Error code 0x2746.’
REF: https://stackoverflow.com/questions/57265913/error-tcp-provider-error-code-0x2746-during-the-sql-setup-in-linux-through-te

3. Setup your SQL Server.
Note: I choose ‘Developer’ edition.

SORRY, ONLY ADMIN CAN SHOW THIS!
Check the mssql-server service status

NOTE: ERROR LOG IN /var/opt/mssql/log/error
MUST OPEN THE DIR WITH ‘sudo su’
4. Install the ODBC Driver and SQL Command Line Utility for SQL Server (SQLCMD)

After installing SQLCMD, you can connect to SQL Server using the following command: sqlcmd -S localhost -U sa -P Yourpassword
SORRY, ONLY ADMIN CAN SHOW THIS!

Install the PHP Driver for SQL Server

THEN

THEN

NOTE: IT’D COPY THE FILES ’20-sqlsrv.ini’ AND ’30-pdo_sqlsrv.ini’ TO DIR /etc/php/7.2/cli/conf.d/
THEN
MODIFY ‘php.ini’ file for php7.2 fpm and (cli NO NEED???)

THEN INSERT THESE TWO LINES:

NO NEED –>PLS DO THE SAME FOR ‘cli’: sudo gedit /etc/php/7.2/cli/php.ini

THEN COPY THE TWO INI FILES FROM ‘cli’ TO ‘fpm’

THEN RESTART PHP FPM

CHECK THEM IN phpinfo: http://localhost:8080/phpinfo.php

Create a database ‘SampleDB’ for your application

The DB will be exist in /var/opt/mssql/data/SampleDB.mdf and the log file.

Create a PHP app that connects to SQL Server and executes queries

Create a new file ‘connect.php’

RUN ON CLI:

NOTE: ON CLI I GOT THIS WARNING:

SOLUTION: REMOVE/COMMENT THE EXTENSIONS ‘sqlsvr’ AND ‘pdo_sqlsvr’ IN /etc/php/7.2/cli/php.ini FILES!
CHECK THE LOADED MODULE WITH: php -m
IT 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 !

ALSO RUN VIA WEB: http://localhost:8080/works/SqlServerSample/connect.php

 

UNINSTALL MS SQL SERVER
REF: https://dba.stackexchange.com/questions/174175/how-do-i-totally-remove-sql-server-2017-on-ubuntu

Uninstalling SQL Server on Ubuntu