使用Docker持久化部署SQL Server
1、拉取镜像
docker pull mcr.microsoft.com/mssql/server
2、数据持久化构建
// 1433(服务器端口):1433(容器端口),sql-server(容器名称) docker run --restart=always -v sqldata:/var/opt/mssql -v /etc/localtime:/etc/localtime:ro -e TZ="Asia/Shanghai" -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=<YourStrong@Passw0rd>' -p 1433:1433 --name sql-server -d mcr.microsoft.com/mssql/server
-
坑点: 挂载目录要为根目录才可以, 这... 也许是我还没有找到可以正确挂载非根目录的方法。然后实际挂载的目录为: /var/lib/docker/volumes/sql1data
3、服务器内连接到SQL Server
docker exec -it sql-server /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P '<YourStrong@Passw0rd>'
#修改账号密码 docker exec -it sql-server /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P '<YourStrong@Passw0rd>' -Q 'ALTER LOGIN SA WITH PASSWORD="<YourNewStrong@Passw0rd>"'
#创建数据库
CREATE DATABASE OperationDB
SELECT Name from sys.Databases
GO
#插入数据 USE TestDB CREATE TABLE Inventory (id INT, name NVARCHAR(50), quantity INT) INSERT INTO Inventory VALUES (1, 'banana', 150); INSERT INTO Inventory VALUES (2, 'orange', 154); GO
#查询数据 SELECT * FROM Inventory WHERE quantity > 152; GO