https://github.com/rgl/sql-server-vagrant
SQL Server Express Vagrant environment
https://github.com/rgl/sql-server-vagrant
powershell sql-server vagrant
Last synced: 9 months ago
JSON representation
SQL Server Express Vagrant environment
- Host: GitHub
- URL: https://github.com/rgl/sql-server-vagrant
- Owner: rgl
- Created: 2017-01-29T09:47:57.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2024-01-16T19:51:41.000Z (about 2 years ago)
- Last Synced: 2025-03-31T04:36:04.090Z (10 months ago)
- Topics: powershell, sql-server, vagrant
- Language: PowerShell
- Size: 180 KB
- Stars: 14
- Watchers: 1
- Forks: 5
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Security: Security.Cryptography.dll
Awesome Lists containing this project
README
This is an example Vagrant environment for a SQL Server 2022 Express installation.
It will:
* Change the SQL Server Settings
* Mixed mode authentication
* Allow TCP/IP connections
* Allow encrypted connections (using a private CA)
* Create Users
* SQL Server Users: `alice.doe` (in the `sysadmin` role), `carol.doe`, `eve.doe` and `grace.doe`.
* Windows Users: `bob.doe`, `dave.doe`, `frank.doe` and `henry.doe`.
* All have the `HeyH0Password` password.
* Create the `TheSimpsons` Database
* Create the `db_executor` database role with permissions to execute stored procedures.
* Add users to database roles
* `carol.doe` in the `db_datawriter`, `db_datareader` and `db_executor` roles.
* `eve.doe` in the `db_datareader` and `db_executor` roles.
* Run PowerShell, Python, Java, C# and Go [examples](examples/).
# Usage
Install the [Windows 2022 base box](https://github.com/rgl/windows-vagrant).
Run `vagrant up --no-destroy-on-error` to launch the environment.
Login into the Windows VM, open ConEmu/bash, and dump the SQL Server TLS details:
```bash
# see the TLS certificate validation result:
echo | openssl s_client -connect $COMPUTERNAME:1433 -servername $COMPUTERNAME -CAfile /c/vagrant/tmp/ca/example-ca-crt.pem
# see the TLS certificate being returned by the server:
echo | openssl s_client -connect $COMPUTERNAME:1433 -servername $COMPUTERNAME -CAfile /c/vagrant/tmp/ca/example-ca-crt.pem | openssl x509 -noout -text -in -
```
# Reference
* [TDS 8.0 and TLS 1.3 support](https://learn.microsoft.com/en-us/sql/relational-databases/security/networking/tds-8-and-tls-1-3?view=sql-server-ver16)
# Example queries
## List active connections
List active connections details:
```sql
select
c.client_net_address,
s.login_name,
db_name(s.database_id) as database_name,
s.program_name,
c.encrypt_option,
c.connect_time
from
sys.dm_exec_connections as c
inner join sys.dm_exec_sessions as s
on c.session_id = s.session_id
order by
c.client_net_address,
s.login_name,
s.program_name
```
**NB** you can customize what appears on `s.program_name` by setting the `Application Name`
connection string property, e.g., `Application Name=Example Application;`.
## List database principals permissions
```sql
select
principals.principal_id,
principals.name,
principals.type_desc,
principals.authentication_type_desc,
permissions.state_desc,
permissions.permission_name
from
sys.database_principals as principals
inner join sys.database_permissions as permissions
on principals.principal_id = permissions.grantee_principal_id
order by
principals.name,
principals.type_desc,
principals.authentication_type_desc,
permissions.state_desc,
permissions.permission_name
```
## List database schema tables row count
```sql
select
schema_name(schema_id) as schema_name,
t.name as table_name,
sum(p.rows) as row_count
from
sys.tables as t
inner join sys.partitions as p
on t.object_id = p.object_id
and p.index_id in (0, 1)
group by
schema_name(schema_id),
t.name
```
## List database row count and storage usage
```sql
select
sum(p.rows) as row_count,
(select sum(case when type = 1 then size end) * cast(8 * 1024 as bigint) from sys.master_files where database_id = db_id()) as data_size_bytes,
(select sum(case when type = 0 then size end) * cast(8 * 1024 as bigint) from sys.master_files where database_id = db_id()) as log_size_bytes
from
sys.tables as t
inner join sys.partitions as p
on t.object_id = p.object_id
and p.index_id in (0, 1)
```
## List databases storage usage
```sql
select
db_name(database_id) as database_name,
sum(case when type = 1 then size end) * cast(8 * 1024 as bigint) as data_size_bytes,
sum(case when type = 0 then size end) * cast(8 * 1024 as bigint) as log_size_bytes
from
sys.master_files
group by
database_id
```