Connecting to Microsoft SQL Server from Linux

December 11, 2018

Lang: cs en de es

Microsoft SQL Server is a relational SQL database server. It is a common choice where Microsoft technologies are used. However, Microsoft technologies typically only run on Microsoft products again and have traditionally always been closed. What to do where Windows is not used? How to connect from Linux to an MS SQL database server? It can. I'll show you how to do it.

The database never works by itself. When using a database, the following situations may occur:

  • it is necessary to connect to MS SQL server from the application
  • you need to explore the data in the database
  • you need to switch from an MS SQL database to another multi-platform database

Since some time MS SQL server is running on Linux https://docs.microsoft.com/en-us/sql/linux/sql-server-linux-setup?view=sql-server-2017

PHP and MS SQL - FreeTDS

One way to connect from a PHP script to an MSSQL server is to run FreeTDS and connect to an MS SQL server through it. So you need to get FreeTDS and MSSQL for PHP.

FreeTDS

FreeTDS is a set of libraries for Linux and other Unixes. These libraries allow programs to interact natively with Microsoft SQL Server and Sybase.

The configuration file for FreeTDS is in /etc/freetds.conf.
Its contents may look like this:

[servermssql]

host = 192.168.1.1
port = 7594
tds version = 7.0
client charset = UTF-8

PHP and MSSQL

Then you need to install the php-mssql package.
Here is a sample PHP script:

$link = mssql_pconnect('server'mssql, $mssql_user, $mssql_pass, true);
if (!$link || !mssql_select_db($mssql_db , $link)) {
    die('Unable to connect or select database!');
}
echo "connect established:\n";

// Do a simple query, select the version of
// MSSQL and print it.
$version = mssql_query('SELECT @@VERSION');
$row = mssql_fetch_array($version);

echo $row[0];
echo "---\n";
// Clean up
mssql_free_result($version);

 

GUI for MS SQL

For the graphical display of MS SQL database data, I have chosen two programs that are easy to install as they are available in the package:

Downloadable Beaver and SQL Electron download.

SQL Electron

esqlelectron

The SQL Electron program is very easy to use, but simpler

Dbeaver

dbeaver

I preferred Dbeaver, it makes many options available and shows details from the database as well. For example, it also shows the table structure using DDL.

Články na podobné téma

How to use MailCatcher to test emails
Python OpenAI API
Creating a WebSocket web application and setting up a proxy
Project management: agile software development
How to run old PHP applications
What a good programmer should know
Rust programming language
NodeJS: development, server configuration
Nette security bug CVE-2020-15227
REST API: platform API
Custom web and mail hosting with ISP Config software
Programming in SQL: PostgreSQL, MySQL/MariaDB
HTTPS: secure web
NoSQL database Mongo DB
What is the job description of a programmer
Which mail and web hosting to choose
Digispark - Program Atmel ATtiny microcontroller with Arduino IDE
Development for ARM processors with Arduino IDE
How to program the ESP8266 WiFi processor
Open smartphone with Linux - Openmoko Neo FreeRunner

Newsletter

If you are interested in receiving occasional news by email.
You can register by filling in your email news subscription.


+