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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yupeigu/article/details/79194474

安装篇:Linux下安装SQL Server 2017(安装篇)

连接篇:Linux下安装SQL Server 2017(连接篇)


连接数据库


(1)下载客户端连接工具的源

curl -o /etc/yum.repos.d/msprod.repo https://packages.microsoft.com/config/rhel/7/prod.repo

(2)安装客户端连接工具、unixODBC开发人员包

yum install -y mssql-tools unixODBC-devel
(3)添加环境变量,之后执行客户端工具时,直接输入命令就可以,比如:sqlcmd命令连接数据库时,直接输入命令执行,不需要输入一堆的路径。

echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
source ~/.bashrc

(4)用sqlcmd命令,连接SQL Server实例,建库、建表、插入数据、查数据。

[root@localhost ~]# sqlcmd -S localhost -U SA -P '你设置的密码'
1> select db_name();
2> go

--------------------------------------------------------------------------------------------------------------------------------
master                                                                                                                          

(1 rows affected)

1> create database test;
2> go

1> use test;
2> go
已将数据库上下文更改为 "test"。

1> create table test(id int not null primary key ,name varchar(10));
2> go

1> insert into test values(1,'123');
2> insert into test values(2,'456');
3> go

1> select * from test;
2> go
id          name      
----------- ----------
          1 123       
          2 456       

(2 rows affected)
1> 

从上面的输出来看,成功连接上sql server,成功创建数据库、表。

退出sqlcmd的命令为 exit 或者 quit。

(5)从远程连接SQL Server

通过ifconfig命令,获取SQL Server安装机器的IP地址,这里是 192.168.70.129(命令输出的第2行)

[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.70.129  netmask 255.255.255.0  broadcast 192.168.70.255
        inet6 fe80::e9c4:18ed:61f:5f4f  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:94:a1:c2  txqueuelen 1000  (Ethernet)
        RX packets 287347  bytes 355674334 (339.1 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 75175  bytes 4703959 (4.4 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0用

这里用SQL Server 2014的客户端工具,远程连接2017:



猜你喜欢

转载自blog.csdn.net/yupeigu/article/details/79194474