连接数据库

(1)设置防火墙

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

  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的客户端工具

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

  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
  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. Dependencies Resolved
  21. ===========================================================================================================================
  22. Package                     Arch                Version                    Repository                                Size
  23. ===========================================================================================================================
  24. Installing:
  25. mssql-tools                 x86_64              14.0.1.246-1               packages-microsoft-com-prod              249 k
  26. Installing for dependencies:
  27. msodbcsql                   x86_64              13.0.1.0-1                 packages-microsoft-com-prod              3.8 M
  28. unixODBC-utf16              x86_64              2.3.1-1                    packages-microsoft-com-prod              329 k
  29. Transaction Summary
  30. ===========================================================================================================================
  31. Install  1 Package (+2 Dependent packages)
  32. Total download size: 4.4 M
  33. Installed size: 4.4 M
  34. Downloading packages:
  35. (1/3): mssql-tools-14.0.1.246-1.x86_64.rpm                                                          | 249 kB  00:00:03
  36. (2/3): unixODBC-utf16-2.3.1-1.x86_64.rpm                                                            | 329 kB  00:00:01
  37. (3/3): msodbcsql-13.0.1.0-1.x86_64.rpm                                                              | 3.8 MB  00:00:21
  38. ---------------------------------------------------------------------------------------------------------------------------
  39. Total                                                                                      211 kB/s | 4.4 MB  00:00:21
  40. Running transaction check
  41. Running transaction test
  42. Transaction test succeeded
  43. Running transaction
  44. Installing : unixODBC-utf16-2.3.1-1.x86_64                                                                           1/3
  45. The license terms for this product can be downloaded from
  46. http://go.microsoft.com/fwlink/?LinkId=746838 and found in
  47. /usr/share/doc/msodbcsql/LICENSE.TXT . By entering 'YES',
  48. you indicate that you accept the license terms.
  49. Do you accept the license terms? (Enter YES or NO)
  50. yes
  51. Please enter YES or NO
  52. Do you accept the license terms? (Enter YES or NO)
  53. YES
  54. Installing : msodbcsql-13.0.1.0-1.x86_64                                                                             2/3
  55. The license terms for this product can be downloaded from
  56. http://go.microsoft.com/fwlink/?LinkId=746949 and found in
  57. /usr/share/doc/mssql-tools/LICENSE.txt . By entering 'YES',
  58. you indicate that you accept the license terms.
  59. Do you accept the license terms? (Enter YES or NO)
  60. YES
  61. Installing : mssql-tools-14.0.1.246-1.x86_64                                                                         3/3
  62. Verifying  : msodbcsql-13.0.1.0-1.x86_64                                                                             1/3
  63. Verifying  : unixODBC-utf16-2.3.1-1.x86_64                                                                           2/3
  64. Verifying  : mssql-tools-14.0.1.246-1.x86_64                                                                         3/3
  65. Installed:
  66. mssql-tools.x86_64 0:14.0.1.246-1
  67. Dependency Installed:
  68. msodbcsql.x86_64 0:13.0.1.0-1                               unixODBC-utf16.x86_64 0:2.3.1-1
  69. Complete!

(3)连接sql sever

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

  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. usage: sqlcmd            [-U login id]          [-P password]
  6. [-S server or Dsn if -D is provided]
  7. [-H hostname]          [-E trusted connection]
  8. [-N Encrypt Connection][-C Trust Server Certificate]
  9. [-d use database name] [-l login timeout]     [-t query timeout]
  10. [-h headers]           [-s colseparator]      [-w screen width]
  11. [-a packetsize]        [-e echo input]        [-I Enable Quoted Identifiers]
  12. [-c cmdend]
  13. [-q "cmdline query"]   [-Q "cmdline query" and exit]
  14. [-m errorlevel]        [-V severitylevel]     [-W remove trailing spaces]
  15. [-u unicode output]    [-r[0|1] msgs to stderr]
  16. [-i inputfile]         [-o outputfile]
  17. [-k[1|2] remove[replace] control characters]
  18. [-y variable length type display width]
  19. [-Y fixed length type display width]
  20. [-p[1] print statistics[colon format]]
  21. [-R use client regional setting]
  22. [-K application intent]
  23. [-M multisubnet failover]
  24. [-b On error batch abort]
  25. [-D Dsn flag, indicate -S is Dsn]
  26. [-X[1] disable commands, startup script, environment variables [and exit]]
  27. [-x disable variable substitution]
  28. [-? show syntax summary]

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

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

  1. [root@localhost Desktop]# sqlcmd -S localhost -U sa
  2. Password:
  3. 1> select db_name();
  4. 2> go
  5. --------------------------------------------------------------------------------------------------------------------------------
  6. master
  7. (1 rows affected)
  8. 1> create database test;
  9. 2> go
  10. 1> use test;
  11. 2> go
  12. Changed database context to 'test'.
  13. 1> select * into tb from sys.tables;
  14. 2> go
  15. (1 rows affected)
  16. 1> select count(*) from tb;
  17. 2> go
  18. -----------
  19. 1
  20. (1 rows affected)
  21. 1>

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

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

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

 
0

0

最新文章

  1. centos7 开启防火墙端口 firewalld
  2. English -有感过四六级后的托福单词表-附下载
  3. JAVA equals, ==
  4. Android NOtification 使用(震动 闪屏 铃声)
  5. 小白浅论JAVA数组中“for加强版”
  6. [ASP.NET教程] 防止表单重复提交
  7. 【linux之压缩归档,tar】
  8. GetForegroundWindow获取的是托管进程ApplicationFrameHost,而不是真正的进程,比如XD软件
  9. 【MySQL 读书笔记】当我们在执行更新语句的时候我们在做什么
  10. zipline-- 开发指南
  11. 数据类型、运算符及Scanner类练习
  12. 项目集成swagger【转载】
  13. 痞子衡嵌入式:蓝牙芯片厂商三强(Qualcomm&CSR, TI, Nordic)产品一览
  14. android ActionBarSherlock使用说明
  15. SAP项目的整体预算大概是多少?
  16. 2、My Scripts
  17. 左侧菜单栏,有对个li对应一个content
  18. SharePoint 列表视图修改多行文本字段显示长度
  19. Ubuntu去掉命令行前用户名和主机名方法
  20. Asp.Net Mvc ScriptBundle 脚本文件捆绑压缩 导致 脚本出错的问题

热门文章

  1. 三、Kubernetes之深入了解Pod
  2. 第5件事 做一个有taste的产品人
  3. SQL SERVER锁(LOCK)知识及锁应用
  4. 一不小心发现了个Asp.Net Bug
  5. 【转】JS浮点数运算Bug的解决办法
  6. 知物由学 | AI在Facebook清理有害内容上扮演了什么角色?
  7. Flask系列07--Flask中的CBV, 蓝图的CBV
  8. Android逆向——smali复杂类解析
  9. solr初识
  10. Shell - 简明Shell入门10 - 管道(Pipe)