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

连接数据库

 

(1)设置防火墙

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

 

[plain] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. [root@localhost Desktop]# firewall-cmd --zone=public --add-port=1433/tcp --permanent  
  2. success  
  3. [root@localhost Desktop]# firewall-cmd --reload  
  4. success  
 

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

 

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

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

 

[plain] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. [root@localhost Desktop]# curl https://packages.microsoft.com/config/rhel/7/prod.repo > /etc/yum.repos.d/msprod.repo  
  2.   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current  
  3.                                  Dload  Upload   Total   Spent    Left  Speed  
  4. 100   193  100   193    0     0    106      0  0:00:01  0:00:01 --:--:--   106  

[plain] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. [root@localhost Desktop]# yum install -y mssql-tools  
  2. Loaded plugins: fastestmirror, langpacks  
  3. packages-microsoft-com-prod                                                                         | 2.9 kB  00:00:00       
  4. packages-microsoft-com-prod/primary_db                                                              | 4.6 kB  00:00:01       
  5. Loading mirror speeds from cached hostfile  
  6.  * base: mirrors.aliyun.com  
  7.  * extras: mirrors.aliyun.com  
  8.  * updates: mirrors.163.com  
  9. Resolving Dependencies  
  10. --> Running transaction check  
  11. ---> Package mssql-tools.x86_64 0:14.0.1.246-1 will be installed  
  12. --> Processing Dependency: msodbcsql for package: mssql-tools-14.0.1.246-1.x86_64  
  13. --> Running transaction check  
  14. ---> Package msodbcsql.x86_64 0:13.0.1.0-1 will be installed  
  15. --> Processing Dependency: unixODBC-utf16 for package: msodbcsql-13.0.1.0-1.x86_64  
  16. --> Processing Dependency: libodbcinst.so.2()(64bit) for package: msodbcsql-13.0.1.0-1.x86_64  
  17. --> Running transaction check  
  18. ---> Package unixODBC-utf16.x86_64 0:2.3.1-1 will be installed  
  19. --> Finished Dependency Resolution  
  20.   
  21. Dependencies Resolved  
  22.   
  23. ===========================================================================================================================  
  24.  Package                     Arch                Version                    Repository                                Size  
  25. ===========================================================================================================================  
  26. Installing:  
  27.  mssql-tools                 x86_64              14.0.1.246-1               packages-microsoft-com-prod              249 k  
  28. Installing for dependencies:  
  29.  msodbcsql                   x86_64              13.0.1.0-1                 packages-microsoft-com-prod              3.8 M  
  30.  unixODBC-utf16              x86_64              2.3.1-1                    packages-microsoft-com-prod              329 k  
  31.   
  32. Transaction Summary  
  33. ===========================================================================================================================  
  34. Install  1 Package (+2 Dependent packages)  
  35.   
  36. Total download size: 4.4 M  
  37. Installed size: 4.4 M  
  38. Downloading packages:  
  39. (1/3): mssql-tools-14.0.1.246-1.x86_64.rpm                                                          | 249 kB  00:00:03       
  40. (2/3): unixODBC-utf16-2.3.1-1.x86_64.rpm                                                            | 329 kB  00:00:01       
  41. (3/3): msodbcsql-13.0.1.0-1.x86_64.rpm                                                              | 3.8 MB  00:00:21       
  42. ---------------------------------------------------------------------------------------------------------------------------  
  43. Total                                                                                      211 kB/s | 4.4 MB  00:00:21       
  44. Running transaction check  
  45. Running transaction test  
  46. Transaction test succeeded  
  47. Running transaction  
  48.   Installing : unixODBC-utf16-2.3.1-1.x86_64                                                                           1/3   
  49. The license terms for this product can be downloaded from  
  50. http://go.microsoft.com/fwlink/?LinkId=746838 and found in  
  51. /usr/share/doc/msodbcsql/LICENSE.TXT . By entering 'YES',  
  52. you indicate that you accept the license terms.  
  53.   
  54. Do you accept the license terms? (Enter YES or NO)  
  55. yes  
  56. Please enter YES or NO  
  57. Do you accept the license terms? (Enter YES or NO)  
  58. YES  
  59.   Installing : msodbcsql-13.0.1.0-1.x86_64                                                                             2/3   
  60. The license terms for this product can be downloaded from  
  61. http://go.microsoft.com/fwlink/?LinkId=746949 and found in  
  62. /usr/share/doc/mssql-tools/LICENSE.txt . By entering 'YES',  
  63. you indicate that you accept the license terms.  
  64.   
  65. Do you accept the license terms? (Enter YES or NO)  
  66. YES  
  67.   Installing : mssql-tools-14.0.1.246-1.x86_64                                                                         3/3   
  68.   Verifying  : msodbcsql-13.0.1.0-1.x86_64                                                                             1/3   
  69.   Verifying  : unixODBC-utf16-2.3.1-1.x86_64                                                                           2/3   
  70.   Verifying  : mssql-tools-14.0.1.246-1.x86_64                                                                         3/3   
  71.   
  72. Installed:  
  73.   mssql-tools.x86_64 0:14.0.1.246-1                                                                                          
  74.   
  75. Dependency Installed:  
  76.   msodbcsql.x86_64 0:13.0.1.0-1                               unixODBC-utf16.x86_64 0:2.3.1-1                                
  77.   
  78. Complete!  

(3)连接sql sever

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

 

[plain] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. [root@localhost Desktop]# sqlcmd  
  2. Microsoft (R) SQL Server Command Line Tool  
  3. Version 14.0.0001.246 Linux  
  4. Copyright (c) 2012 Microsoft. All rights reserved.  
  5.   
  6. usage: sqlcmd            [-U login id]          [-P password]  
  7.   [-S server or Dsn if -D is provided]   
  8.   [-H hostname]          [-E trusted connection]  
  9.   [-N Encrypt Connection][-C Trust Server Certificate]  
  10.   [-d use database name] [-l login timeout]     [-t query timeout]  
  11.   [-h headers]           [-s colseparator]      [-w screen width]  
  12.   [-a packetsize]        [-e echo input]        [-I Enable Quoted Identifiers]  
  13.   [-c cmdend]  
  14.   [-q "cmdline query"]   [-Q "cmdline query" and exit]  
  15.   [-m errorlevel]        [-V severitylevel]     [-W remove trailing spaces]  
  16.   [-u unicode output]    [-r[0|1] msgs to stderr]  
  17.   [-i inputfile]         [-o outputfile]  
  18.   [-k[1|2] remove[replace] control characters]  
  19.   [-y variable length type display width]  
  20.   [-Y fixed length type display width]  
  21.   [-p[1] print statistics[colon format]]  
  22.   [-R use client regional setting]  
  23.   [-K application intent]  
  24.   [-M multisubnet failover]  
  25.   [-b On error batch abort]  
  26.   [-D Dsn flag, indicate -S is Dsn]   
  27.   [-X[1] disable commands, startup script, environment variables [and exit]]  
  28.   [-x disable variable substitution]  
  29.   [-? show syntax summary]  

 

 

这里的-S是指定服务器名称,-U指定用户名,回车后会提示输入密码。

接下来查询当前是哪个库、创建了一个叫test的库、创建了一个叫tb的表、查询tb表的记录数。

[plain] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. [root@localhost Desktop]# sqlcmd -S localhost -U sa  
  2. Password:   
  3.   
  4. 1> select db_name();  
  5. 2> go  
  6.                                                                                                                                   
  7. --------------------------------------------------------------------------------------------------------------------------------  
  8. master                                                                                                                            
  9.   
  10. (1 rows affected)  
  11. 1> create database test;  
  12. 2> go  
  13. 1> use test;  
  14. 2> go  
  15. Changed database context to 'test'.  
  16. 1> select * into tb from sys.tables;  
  17. 2> go  
  18.   
  19. (1 rows affected)  
  20. 1> select count(*) from tb;  
  21. 2> go  
  22.              
  23. -----------  
  24.           1  
  25.   
  26. (1 rows affected)  
  27. 1>   

 

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

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

 

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

 
0
 
0
posted @ 2017-03-15 09:17  网络蚂蚁  阅读(576)  评论(0编辑  收藏  举报