对 NodeJS 和 SQL Server 使用 docker-compose
我有下面的 docker-compose.yml :
version: "3.7"
services:
sql-server-db:
container_name: sql-server-db
image: mcr.microsoft.com/mssql/server:2019-latest
ports:
- "1433:1433"
environment:
SA_PASSWORD: "Thieu@098551298"
ACCEPT_EULA: "Y"
我将运行命令:
docker-compose up -d
我使用了 2 个命令去我的数据库
$ docker exec -it sql-server-db "bash"
$ /opt/mssql-tools/bin/sqlcmd -s localhost -U SA -P Thieu@098551298
1> select name from sys.databases
2> go
name
--------------------------------------------------------------------------------------------------------------------------------
master
tempdb
model
msdb
然后我将创建并插入数据:
1> create database blog
2> go
1> use blog
2> go
Changed database context to 'blog'.
1> create table post (id int,title nvarchar(100),content nvarchar(100))
2> go
1> insert into post values(1,"How to learn docker"," qwe qwje qwb ejqwbe bqwe bqwe bqwje jkqwn e")
2> go
(1 rows affected)
1> select *from post
2> go
id ltitle lcontent
-----------l----------------------------------------------------------------------------------------------------l----------------------------------------------------------------------------------------------------
1lHow to learn docker l qwe qwje qwb ejqwbe bqwe bqwe bqwje jkqwn e
(1 rows affected)
1>
当我有上面的数据库时,我写:
var sql = require('mssql')
var config = {
user: 'sa',
password: 'Thieu@098551298',
server: 'localhost',
database: "blog",
options: {
enableArithAbort: true,
trustServerCertificate: true
}
}
function ExecuteSQL(query) {
return new Promise((resolve, reject) => {
sql.connect(config, (err, db) => {
if (err) reject(err);
var request = new sql.Request();
request.query(query, (err, db) => {
if (err) reject(err);
resolve(db);
});
})
})
}
async function z(){
var z = await ExecuteSQL("select *from post")
console.log(z.recordsets)
}
z()
它将抛出一个错误:
连接错误:用户“sa”登录失败。
目的:我想显示我刚刚添加的数据,但它似乎连接到我的机器上的SQL Server,所以有什么方法可以连接到Docker SQL Server。谢谢。
I have the docker-compose.yml
below:
version: "3.7"
services:
sql-server-db:
container_name: sql-server-db
image: mcr.microsoft.com/mssql/server:2019-latest
ports:
- "1433:1433"
environment:
SA_PASSWORD: "Thieu@098551298"
ACCEPT_EULA: "Y"
I will run command:
docker-compose up -d
I used 2 command to go my database
$ docker exec -it sql-server-db "bash"
$ /opt/mssql-tools/bin/sqlcmd -s localhost -U SA -P Thieu@098551298
1> select name from sys.databases
2> go
name
--------------------------------------------------------------------------------------------------------------------------------
master
tempdb
model
msdb
Then I will create and insert data:
1> create database blog
2> go
1> use blog
2> go
Changed database context to 'blog'.
1> create table post (id int,title nvarchar(100),content nvarchar(100))
2> go
1> insert into post values(1,"How to learn docker"," qwe qwje qwb ejqwbe bqwe bqwe bqwje jkqwn e")
2> go
(1 rows affected)
1> select *from post
2> go
id ltitle lcontent
-----------l----------------------------------------------------------------------------------------------------l----------------------------------------------------------------------------------------------------
1lHow to learn docker l qwe qwje qwb ejqwbe bqwe bqwe bqwje jkqwn e
(1 rows affected)
1>
When I have the database above I write:
var sql = require('mssql')
var config = {
user: 'sa',
password: 'Thieu@098551298',
server: 'localhost',
database: "blog",
options: {
enableArithAbort: true,
trustServerCertificate: true
}
}
function ExecuteSQL(query) {
return new Promise((resolve, reject) => {
sql.connect(config, (err, db) => {
if (err) reject(err);
var request = new sql.Request();
request.query(query, (err, db) => {
if (err) reject(err);
resolve(db);
});
})
})
}
async function z(){
var z = await ExecuteSQL("select *from post")
console.log(z.recordsets)
}
z()
It will throw an error:
ConnectionError: Login failed for user 'sa'.
Purpose: I want to display the data I just added, but it seems to connect to SQL Server on my machine so is there any way I can connect to Docker SQL Server. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当我删除SQL Server Management Studio时我解决了,nodejs文件将连接到sql server容器,谢谢大家
I was solve when I delelte SQL Server Management Studio, and the nodejs file will connect to sql server container, thanks everyone