合 Linux下MSSQL的安装
Tags: LinuxMSSQLSQL Server安装部署
简介
SQL Server 2016 开始支持 Linux。随着2017 和2019 版本的推出,它开始支持Linux和Container平台上的HA/DR、Kubernetes和大数据集群解决方案。
我的镜像
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | docker rm -f lhrmssql2017 docker run -d --name lhrmssql2017 -h lhrmssql2017 \ -p 1437:1433 -p 217:22 \ -v /sys/fs/cgroup:/sys/fs/cgroup \ --privileged=true lhrbest/mssql2017:2.0 \ /usr/sbin/init docker rm -f lhrmssql2019 docker run -d --name lhrmssql2019 -h lhrmssql2019 \ -p 1439:1433 -p 219:22 \ -v /sys/fs/cgroup:/sys/fs/cgroup \ --privileged=true lhrbest/mssql2019:2.0 \ /usr/sbin/init docker rm -f lhrmssql2022 docker run -d --name lhrmssql2022 -h lhrmssql2022 \ -p 14322:1433 -p 222:22 \ -v /sys/fs/cgroup:/sys/fs/cgroup \ --privileged=true lhrbest/mssql2022:2.0 \ /usr/sbin/init |
RHEL安装
参考:https://learn.microsoft.com/zh-cn/sql/linux/quickstart-install-connect-red-hat?view=sql-server-ver15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | -- 创建专用网络 docker network create --subnet=172.72.9.0/24 mssql-network docker rm -f lhrmssql2017 docker run -d --name lhrmssql2017 -h lhrmssql2017 \ -p 1437:1433 -p 217:22 \ -v /sys/fs/cgroup:/sys/fs/cgroup \ --privileged=true lhrbest/lhrcentos76:9.2 \ /usr/sbin/init docker rm -f lhrmssql2019 docker run -d --name lhrmssql2019 -h lhrmssql2019 \ -p 1439:1433 -p 219:22 \ -v /sys/fs/cgroup:/sys/fs/cgroup \ --privileged=true lhrbest/lhrcentos76:9.2 \ /usr/sbin/init docker rm -f lhrmssql2022 docker run -d --name lhrmssql2022 -h lhrmssql2022 \ -p 14322:1433 -p 222:22 \ -v /sys/fs/cgroup:/sys/fs/cgroup \ --privileged=true lhrbest/centosstream8:3.0 \ /usr/sbin/init # 下载 SQL Server repository,2022只有8版本以上有 https://packages.microsoft.com/config/rhel/ curl -o /etc/yum.repos.d/mssql-server.repo https://packages.microsoft.com/config/rhel/7/mssql-server-2017.repo curl -o /etc/yum.repos.d/mssql-server.repo https://packages.microsoft.com/config/rhel/7/mssql-server-2019.repo curl -o /etc/yum.repos.d/mssql-server.repo https://packages.microsoft.com/config/rhel/8/mssql-server-2022.repo # 下载并安装 SQL Server(220MB)、 mssql-server-fts是全文搜索为230MB yum install -y mssql-server mssql-server-fts systemctl status mssql-server systemctl enable mssql-server # 启动配置(版本选择7Enterprise Core;sa密码此处设置为:lhr@xxt123) MSSQL_LCID='2052' MSSQL_COLLATION='Chinese_PRC_CI_AS' /opt/mssql/bin/mssql-conf setup -- 重新初始化的话,直接删除rm -rf /var/opt/mssql/* 后再操作mssql-conf setup -- 配置排序规则:Chinese_PRC_BIN区分大小写,Chinese_PRC_CI_AS 不区分大小写 -- 不能有业务数据库,需要停机 systemctl stop mssql-server /opt/mssql/bin/mssql-conf set-collation -- 默认语言 /opt/mssql/bin/mssql-conf set language.lcid 2052 -- 启用代理 /opt/mssql/bin/mssql-conf set sqlagent.enabled true systemctl restart mssql-server -- 默认备份路径 mkdir -p /var/opt/mssql/bk chown mssql.mssql /var/opt/mssql/bk sudo /opt/mssql/bin/mssql-conf set filelocation.defaultbackupdir /var/opt/mssql/bk systemctl restart mssql-server -- linux客户端 curl -o /etc/yum.repos.d/msprod.repo https://packages.microsoft.com/config/rhel/7/prod.repo yum install -y mssql-tools unixODBC-devel mssql-cli echo "export PATH=/opt/mssql-tools/bin:$PATH" >> /root/.bashrc source /root/.bashrc -- 建库 create database lhrdb COLLATE Chinese_PRC_BIN; /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P "lhr@xxt123" sqlcmd -S 192.168.8.8,1433 -U SA -P "lhr@xxt123" SELECT Name from sys.Databases go SELECT @@VERSION SELECT SERVERPROPERTY('Edition') go -- 修改sa密码 USE [master] GO ALTER LOGIN [sa] WITH CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF GO ALTER LOGIN [sa] WITH PASSWORD=N'lhr' GO -- 修改内存 SELECT * FROM sys.configurations d where d.name like '%max server memory (MB)%' ORDER BY name ; GO sp_configure 'show advanced options', 1; GO RECONFIGURE WITH OVERRIDE; GO sp_configure 'max server memory (MB)', 1024; GO RECONFIGURE WITH OVERRIDE; GO -- 设置限制 SQL Server 进程的可用物理内存量(服务器-》属性-》常规中的内存) sudo /opt/mssql/bin/mssql-conf set memory.memorylimitmb 1024 -- 远程登录 sqlcmd -S 192.168.8.8,1403 -U sa -P lhr sqlcmd -S 192.168.8.8,1403 -d master -h -1 -U sa -P lhr -W -Q "set nocount on;SELECT name FROM master..sysdatabases" -- 重建SQL Server实例 systemctl stop mssql-server.service MSSQL_LCID='2052' MSSQL_COLLATION='Chinese_PRC_CI_AS' /opt/mssql/bin/sqlservr --force-setup -- 或者: rm -rf /var/opt/mssql/* MSSQL_LCID='2052' MSSQL_COLLATION='Chinese_PRC_CI_AS' /opt/mssql/bin/mssql-conf setup -- 配置文件 /var/opt/mssql/mssql.conf -- 日志文件 tailf /var/opt/mssql/log/errorlog.loc |
一键安装脚本
https://docs.microsoft.com/zh-cn/sql/linux/sample-unattended-install-redhat?view=sql-server-2017
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | cat > /tmp/install_sql.sh << "EOFMSSQL" #!/bin/bash -e # Use the following variables to control your install: # Password for the SA user (required) MSSQL_SA_PASSWORD='<YourStrong!Passw0rd>' # Product ID of the version of SQL server you're installing # Must be evaluation, developer, express, web, standard, enterprise, or your 25 digit product key # Defaults to developer MSSQL_PID='evaluation' # Install SQL Server Agent (recommended) SQL_ENABLE_AGENT='y' # Install SQL Server Full Text Search (optional) # SQL_INSTALL_FULLTEXT='y' # Create an additional user with sysadmin privileges (optional) # SQL_INSTALL_USER='<Username>' # SQL_INSTALL_USER_PASSWORD='<YourStrong!Passw0rd>' if [ -z $MSSQL_SA_PASSWORD ] then echo Environment variable MSSQL_SA_PASSWORD must be set for unattended install exit 1 fi echo Adding Microsoft repositories... sudo curl -o /etc/yum.repos.d/mssql-server.repo https://packages.microsoft.com/config/rhel/7/mssql-server-2017.repo sudo curl -o /etc/yum.repos.d/msprod.repo https://packages.microsoft.com/config/rhel/7/prod.repo echo Installing SQL Server... sudo yum install -y mssql-server echo Running mssql-conf setup... sudo MSSQL_SA_PASSWORD=$MSSQL_SA_PASSWORD \ MSSQL_PID=$MSSQL_PID \ /opt/mssql/bin/mssql-conf -n setup accept-eula echo Installing mssql-tools and unixODBC developer... sudo ACCEPT_EULA=Y yum install -y mssql-tools unixODBC-devel # Add SQL Server tools to the path by default: echo Adding SQL Server tools to your path... echo PATH="$PATH:/opt/mssql-tools/bin" >> ~/.bash_profile echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc source ~/.bashrc # Optional Enable SQL Server Agent : if [ ! -z $SQL_ENABLE_AGENT ] then echo Enable SQL Server Agent... sudo /opt/mssql/bin/mssql-conf set sqlagent.enabled true sudo systemctl restart mssql-server fi # Optional SQL Server Full Text Search installation: if [ ! -z $SQL_INSTALL_FULLTEXT ] then echo Installing SQL Server Full-Text Search... sudo yum install -y mssql-server-fts fi # Configure firewall to allow TCP port 1433: echo Configuring firewall to allow traffic on port 1433... sudo firewall-cmd --zone=public --add-port=1433/tcp --permanent sudo firewall-cmd --reload # Example of setting post-installation configuration options # Set trace flags 1204 and 1222 for deadlock tracing: #echo Setting trace flags... #sudo /opt/mssql/bin/mssql-conf traceflag 1204 1222 on # Restart SQL Server after making configuration changes: echo Restarting SQL Server... sudo systemctl restart mssql-server # Connect to server and get the version: counter=1 errstatus=1 while [ $counter -le 5 ] && [ $errstatus = 1 ] do echo Waiting for SQL Server to start... sleep 5s /opt/mssql-tools/bin/sqlcmd \ -S localhost \ -U SA \ -P $MSSQL_SA_PASSWORD \ -Q "SELECT @@VERSION" 2>/dev/null errstatus=$? ((counter++)) done # Display error if connection failed: if [ $errstatus = 1 ] then echo Cannot connect to SQL Server, installation aborted exit $errstatus fi # Optional new user creation: if [ ! -z $SQL_INSTALL_USER ] && [ ! -z $SQL_INSTALL_USER_PASSWORD ] then echo Creating user $SQL_INSTALL_USER /opt/mssql-tools/bin/sqlcmd \ -S localhost \ -U SA \ -P $MSSQL_SA_PASSWORD \ -Q "CREATE LOGIN [$SQL_INSTALL_USER] WITH PASSWORD=N'$SQL_INSTALL_USER_PASSWORD', DEFAULT_DATABASE=[master], CHECK_EXPIRATION=ON, CHECK_POLICY=ON; ALTER SERVER ROLE [sysadmin] ADD MEMBER [$SQL_INSTALL_USER]" fi echo Done! EOFMSSQL |
运行: