您的位置:首页 > 运维架构 > Linux

Linux下安装SQL Server 2016(连接篇SQL Server on linux)

2017-03-15 09:17 225 查看
连接数据库

(1)设置防火墙

要连接数据库,首先要打开防火墙上1433端口,也就是,增加tcp端口1433到公共区域,并且永久生效。

[plain] view plain copy







[root@localhost Desktop]# firewall-cmd --zone=public --add-port=1433/tcp --permanent

success

[root@localhost Desktop]# firewall-cmd --reload

success

(2)下载客户端工具的源、安装客户端工具

可以参考这个文章:安装sql server的客户端工具

也是非常简单的,先下载,然后安装



[plain] view plain copy







[root@localhost Desktop]# curl https://packages.microsoft.com/config/rhel/7/prod.repo > /etc/yum.repos.d/msprod.repo

% Total % Received % Xferd Average Speed Time Time Time Current

Dload Upload Total Spent Left Speed

100 193 100 193 0 0 106 0 0:00:01 0:00:01 --:--:-- 106

[plain] view plain copy







[root@localhost Desktop]# yum install -y mssql-tools

Loaded plugins: fastestmirror, langpacks

packages-microsoft-com-prod | 2.9 kB 00:00:00

packages-microsoft-com-prod/primary_db | 4.6 kB 00:00:01

Loading mirror speeds from cached hostfile

* base: mirrors.aliyun.com

* extras: mirrors.aliyun.com

* updates: mirrors.163.com

Resolving Dependencies

--> Running transaction check

---> Package mssql-tools.x86_64 0:14.0.1.246-1 will be installed

--> Processing Dependency: msodbcsql for package: mssql-tools-14.0.1.246-1.x86_64

--> Running transaction check

---> Package msodbcsql.x86_64 0:13.0.1.0-1 will be installed

--> Processing Dependency: unixODBC-utf16 for package: msodbcsql-13.0.1.0-1.x86_64

--> Processing Dependency: libodbcinst.so.2()(64bit) for package: msodbcsql-13.0.1.0-1.x86_64

--> Running transaction check

---> Package unixODBC-utf16.x86_64 0:2.3.1-1 will be installed

--> Finished Dependency Resolution

Dependencies Resolved

===========================================================================================================================

Package Arch Version Repository Size

===========================================================================================================================

Installing:

mssql-tools x86_64 14.0.1.246-1 packages-microsoft-com-prod 249 k

Installing for dependencies:

msodbcsql x86_64 13.0.1.0-1 packages-microsoft-com-prod 3.8 M

unixODBC-utf16 x86_64 2.3.1-1 packages-microsoft-com-prod 329 k

Transaction Summary

===========================================================================================================================

Install 1 Package (+2 Dependent packages)

Total download size: 4.4 M

Installed size: 4.4 M

Downloading packages:

(1/3): mssql-tools-14.0.1.246-1.x86_64.rpm | 249 kB 00:00:03

(2/3): unixODBC-utf16-2.3.1-1.x86_64.rpm | 329 kB 00:00:01

(3/3): msodbcsql-13.0.1.0-1.x86_64.rpm | 3.8 MB 00:00:21

---------------------------------------------------------------------------------------------------------------------------

Total 211 kB/s | 4.4 MB 00:00:21

Running transaction check

Running transaction test

Transaction test succeeded

Running transaction

Installing : unixODBC-utf16-2.3.1-1.x86_64 1/3

The license terms for this product can be downloaded from
http://go.microsoft.com/fwlink/?LinkId=746838 and found in

/usr/share/doc/msodbcsql/LICENSE.TXT . By entering 'YES',

you indicate that you accept the license terms.

Do you accept the license terms? (Enter YES or NO)

yes

Please enter YES or NO

Do you accept the license terms? (Enter YES or NO)

YES

Installing : msodbcsql-13.0.1.0-1.x86_64 2/3

The license terms for this product can be downloaded from
http://go.microsoft.com/fwlink/?LinkId=746949 and found in

/usr/share/doc/mssql-tools/LICENSE.txt . By entering 'YES',

you indicate that you accept the license terms.

Do you accept the license terms? (Enter YES or NO)

YES

Installing : mssql-tools-14.0.1.246-1.x86_64 3/3

Verifying : msodbcsql-13.0.1.0-1.x86_64 1/3

Verifying : unixODBC-utf16-2.3.1-1.x86_64 2/3

Verifying : mssql-tools-14.0.1.246-1.x86_64 3/3

Installed:

mssql-tools.x86_64 0:14.0.1.246-1

Dependency Installed:

msodbcsql.x86_64 0:13.0.1.0-1 unixODBC-utf16.x86_64 0:2.3.1-1

Complete!

(3)连接sql sever
这里用sqlcmd来连接sql server,下面是一些命令行参数。

[plain] view plain copy







[root@localhost Desktop]# sqlcmd

Microsoft (R) SQL Server Command Line Tool

Version 14.0.0001.246 Linux

Copyright (c) 2012 Microsoft. All rights reserved.

usage: sqlcmd [-U login id] [-P password]

[-S server or Dsn if -D is provided]

[-H hostname] [-E trusted connection]

[-N Encrypt Connection][-C Trust Server Certificate]

[-d use database name] [-l login timeout] [-t query timeout]

[-h headers] [-s colseparator] [-w screen width]

[-a packetsize] [-e echo input] [-I Enable Quoted Identifiers]

[-c cmdend]

[-q "cmdline query"] [-Q "cmdline query" and exit]

[-m errorlevel] [-V severitylevel] [-W remove trailing spaces]

[-u unicode output] [-r[0|1] msgs to stderr]

[-i inputfile] [-o outputfile]

[-k[1|2] remove[replace] control characters]

[-y variable length type display width]

[-Y fixed length type display width]

[-p[1] print statistics[colon format]]

[-R use client regional setting]

[-K application intent]

[-M multisubnet failover]

[-b On error batch abort]

[-D Dsn flag, indicate -S is Dsn]

[-X[1] disable commands, startup script, environment variables [and exit]]

[-x disable variable substitution]

[-? show syntax summary]

这里的-S是指定服务器名称,-U指定用户名,回车后会提示输入密码。
接下来查询当前是哪个库、创建了一个叫test的库、创建了一个叫tb的表、查询tb表的记录数。

[plain] view plain copy







[root@localhost Desktop]# sqlcmd -S localhost -U sa

Password:

1> select db_name();

2> go

--------------------------------------------------------------------------------------------------------------------------------

master

(1 rows affected)

1> create database test;

2> go

1> use test;

2> go

Changed database context to 'test'.

1> select * into tb from sys.tables;

2> go

(1 rows affected)

1> select count(*) from tb;

2> go

-----------

1

(1 rows affected)

1>

(4)在windows上用Microsoft SQL Server Management Studio(ssms)连接

连接上sql server,查询表tb的数据。



另外,可以在Linux中查询当前连接到sql server的会话,发现有sqlcmd命令,还有ssms,在看看hostprocess进程号就是3228.



顶0 踩0
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: