Bboysoul's Blog

首页 公告 RSS

ubuntu安装sql-server

February 8, 2018 本文有 1470 个字 需要花费 3 分钟阅读

概述

微软在2016年宣布sqlserver支持linux,目前支持在docker,ubuntu,,centos,suse上安装,安装过程也很简单,就是添加软件源然后安装上mssql-server包就好了,如果你要在linux下连接sqlserver,那么你还要安装mssql-tools
话不多说直接安装

安装sqlserver

我用的是ubuntu-server16.04,内存建议4g,因为这不是mysql,是微软家的东西
首先更新下系统把
sudo apt update && sudo apt upgrade
接着移除掉老旧的内核
sudo apt autoremove
添加sqlserver的软件源
导入公共存储库 GPG 密钥
wget -qO- https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
添加软件源
sudo add-apt-repository "$(wget -qO- https://packages.microsoft.com/config/ubuntu/16.04/mssql-server-2017.list)"
刷新软件源并安装mssql-server
sudo apt-get update
sudo apt-get install -y mssql-server
网速还是挺快的
接着设置sa的密码
sudo /opt/mssql/bin/mssql-conf setup
过程如下

➜  ~ sudo /opt/mssql/bin/mssql-conf setup
Choose an edition of SQL Server:
  1) Evaluation (free, no production use rights, 180-day limit)
  2) Developer (free, no production use rights)
  3) Express (free)
  4) Web (PAID)
  5) Standard (PAID)
  6) Enterprise (PAID)
  7) Enterprise Core (PAID)
  8) I bought a license through a retail sales channel and have a product key to enter.

Details about editions can be found at
https://go.microsoft.com/fwlink/?LinkId=852748&clcid=0x409

Use of PAID editions of this software requires separate licensing through a
Microsoft Volume Licensing program.
By choosing a PAID edition, you are verifying that you have the appropriate
number of licenses in place to install and run this software.

Enter your edition(1-8): 3
The license terms for this product can be found in
/usr/share/doc/mssql-server or downloaded from:
https://go.microsoft.com/fwlink/?LinkId=855862&clcid=0x409

The privacy statement can be viewed at:
https://go.microsoft.com/fwlink/?LinkId=853010&clcid=0x409

Enter the SQL Server system administrator password: 
The specified password does not meet SQL Server password policy requirements because it is not complex enough. The password must be at least 8 characters long and contain characters from three of the following four sets: uppercase letters, lowercase letters, numbers, and symbols.
Enter the SQL Server system administrator password: 
Confirm the SQL Server system administrator password: 
Configuring SQL Server...

The licensing PID was successfully processed. The new edition is [Express Edition].
Created symlink from /etc/systemd/system/multi-user.target.wants/mssql-server.service to /lib/systemd/system/mssql-server.service.
Setup has completed successfully. SQL Server is now starting.

其实这是我第二次设置的结果,正确的流程应该是这样的
首先会让你选择license的版本,我选择的是express这个是免费的,当然你可以选择Developer,这个功能全有,但是不能用于生产环境。之后会让你同意一个协议,我上面就是这一步没有,因为我第一次执行这个脚本的时候在这一步发生了字符编码报错,接着就是输入sa的密码,注意要八个字符以上,包含大写小写字母,数字符号其中三样,接着就完成了。

查看下服务是不是正常运行
sudo systemctl status mssql-server.service

安装sqlserver命令行工具

同样的先安装软件源
导入公共存储库 GPG 密钥
wget -qO- https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
安装软件源
sudo add-apt-repository "$(wget -qO- https://packages.microsoft.com/config/ubuntu/16.04/prod.list)"
刷新软件源,安装命令行工具
sudo apt-get update
sudo apt-get install -y mssql-tools unixodbc-dev
添加环境变量,因为mssql-tools不是安装在系统环境变量的目录中,所以要添加环境变量
编辑下面这个文件
vim ~/.zshrc
在最后面添加

export PATH="$PATH:/opt/mssql-tools/bin"
export PATH="$PATH:/opt/mssql-tools/bin"

使环境变量生效
source ~/.zshrc

连接数据库进行简单的查询操作

连接命令和mysql其实差不多,都是指定主句用户密码就好了,最多还要指定端口
sqlcmd -S localhost -U SA -p

➜  ~ sqlcmd -S localhost -U SA -p
Password: 
1> 

其他的增删改查都差不多,唯一区别的就是我要输入go才可以执行操作,比如查询系统中有什么数据库

1> select name from sys.databases
2> go
name                                                                                                                            
--------------------------------------------------------------------------------------------------------------------------------
master                                                                                                                          
tempdb                                                                                                                          
model                                                                                                                           
msdb                                                                                                                            

(4 rows affected)

Network packet size (bytes): 4096
1 xact[s]:
Clock Time (ms.): total        34  avg   34.0 (29.4 xacts per sec.)
1> 

不输入分号没关系
输入quit退出sqlcmd
当然你在windows下用ssms也是可以直接连接到这个数据库的

docker安装sqlserver

为了完整,我在docker下也安装一下sqlserver,这个那就更简单了
安装docker就不说了
首先看下docker的服务有没有在运行
sudo service docker status
如果没有,接着启动它
sudo service docker start
最后pull镜像
sudo docker pull microsoft/mssql-server-linux:2017-latest
国内一定要用阿里云的docker镜像加速服务,不然这网速
运行容器

sudo docker run -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>' \
   -p 1401:1433 --name sql1 \
   -d microsoft/mssql-server-linux:2017-latest

解释下命令

  • -e 'ACCEPT_EULA=Y' 同意协议
  • -e 'MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>' 设置你sa的sqlserver密码
  • -p 1401:1433 端口映射
  • --name sql1 容器名字
  • -d microsoft/mssql-server-linux:2017-latest 要运行的镜像

接着输入
sudo docker ps -a

查看容器有没有在运行中

➜  ~ sudo docker ps -a 
CONTAINER ID        IMAGE                                                             COMMAND                  CREATED             STATUS                   PORTS                    NAMES
bff7378d26e4        microsoft/mssql-server-linux:2017-latest                          "/bin/sh -c /opt/mss…"   36 seconds ago      Up 35 seconds            0.0.0.0:1401->1433/tcp   sql1

最后我们就连接下数据库吧
sqlcmd -S 192.168.0.102,1401 -U SA -p
注意映射的主机端口不是默认的1433,是1401,所以要指定端口登录

欢迎关注我的博客www.bboy.app
Have Fun


Tags:

本站总访问量 本站总访客数