laravel5 Using freetds to Connect sql server
- 2021-11-13 01:04:11
- OfStack
Related version
System ubuntu 16.04, using PHP version 7.0. 30, sqlserver 2012, freetds 0.92 Laravel 5.5 and 5.4 have been tested
What is FreeTDS
Simply said FreeTDS is a program library, can be achieved in Linux system access to Microsoft's SQL database! FreeTDS is an open source library that is a re-implementation of the TDS (Tabular Data Flow) protocol. It can be used in the db-lib or ct-lib libraries of Sybase. It also contains a library for ODBC. Allows many open source applications such as Perl and PHP (or your own c or C + + programs) to connect to Sybase or Microsoft SQL servers. FreeTDS is released as source code and can be compiled on almost any operating system. Means Unix and Unix-like systems (including famous branches such as Interix and QNX), as well as Win32, VMS, and OSX.
This article will give you a detailed introduction to laravel5 using freetds connection sql server related content, the following words do not say much, to see a detailed introduction
The steps are as follows
Installing the php driver
sudo apt-get install php7.0-odbc
sudo apt install php7.0-sybase
Installing freetds
sudo apt-get install freetds-bin freetds-common tdsodbc odbcinst unixodbc unixodbc-dev
sudo mv /etc/odbcinst.ini /etc/odbcinst.ini.bak
sudo cp /usr/share/tdsodbc/odbcinst.ini /etc/
Configuring freetds
sudo vim /etc/freetds/freetds.conf
Modify configuration
[global]
tds version = 8.0 # TDS version, ref <a href="http://www.freetds.org/userguide/choosingtdsprotocol.htm" rel="external nofollow" target="_blank">this</a>.
client charset = UTF-8
text size = 20971520
[Server2012] # Custom name, which needs to be used later
host = {yourdomain}.database.windows.net // ip Address or domain name
port = 1433
tds version = 8.0 #8.0 For 2012 Other self-test
Test SQLSERVER
TDSVER=8.0 tsql -H my_server_host -p 1433 -U my_user -P my_password -D my_database
Configuring Laravel5
Open config/database. php to add configuration in connections, and the driver uses sqlsrv
'mssql' => [
'driver' => 'sqlsrv',
'host' => 'Server2012', // This correspondence freetds.conf Configuration name of
'port' => '1433',
'database' => env('DB_DATABASE', ' Database '),
'username' => env('DB_USERNAME', ' Users '),
'password' => env('DB_PASSWORD', ' Password '),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
Multi-database
If you use mysql and want to use part of the information of sqlserver, personal project reasons, but a general approach is sqlserver system to write API to make Mysql system call, but this time secretly lazy, on two 1 to use
Add in Model
protected $connection = 'mssql';
And use the
protected $table = 'EMPLOYEE';
Indicate the data table, so you don't have to write the connection in Controller every time.
Summarize