SQL Serverを外部のPCからリモートで接続できる方法を調査しました。プロトコルの設定やファイアウォールの設定が必要なため、手順をまとめます。
【検証環境】SQLServer 2019 Express / SSMS v18.11.1
利用準備
sa ユーザの設定
セキュリティ > ログイン からsaを右クリックし、プロパティを選択します
![](https://web.archive.org/web/20220704233938im_/https://pg-life.net/wp-content/uploads/2022/05/image-44.png)
SQL Server認証を選択し、好きなパスワードを設定します
![](https://web.archive.org/web/20220704233938im_/https://pg-life.net/wp-content/uploads/2022/05/image-45-1024x904.png)
ページの選択 > 状態 を選択し、ログイン > 有効 を選択します
![](https://web.archive.org/web/20220704233938im_/https://pg-life.net/wp-content/uploads/2022/05/image-34.png)
サーバプロパティの設定
サーバを右クリックしてプロパティを表示します
![](https://web.archive.org/web/20220704233938im_/https://pg-life.net/wp-content/uploads/2022/05/image-46.png)
サーバ認証 > SQL Server 認証モードと Windows 認証モード を選択します
![](https://web.archive.org/web/20220704233938im_/https://pg-life.net/wp-content/uploads/2022/05/image-47-1024x897.png)
SQL Server 構成マネージャーの設定
SQL Server 構成マネージャーを開きます
![](https://web.archive.org/web/20220704233938im_/https://pg-life.net/wp-content/uploads/2022/05/image-48.png)
TCP/IPの状態が無効になっているため、有効に変更します
また、ポートを固定の1433に変更します
![](https://web.archive.org/web/20220704233938im_/https://pg-life.net/wp-content/uploads/2022/05/image-35.png)
![](https://web.archive.org/web/20220704233938im_/https://pg-life.net/wp-content/uploads/2022/05/image-49.png)
![](https://web.archive.org/web/20220704233938im_/https://pg-life.net/wp-content/uploads/2022/05/image-36.png)
SQL Serverを再起動します
![](https://web.archive.org/web/20220704233938im_/https://pg-life.net/wp-content/uploads/2022/05/image-33.png)
ファイアウォールの設定
設定したポート番号を開放するように設定します
Windows Defender ファイアウォール を開きます
![](https://web.archive.org/web/20220704233938im_/https://pg-life.net/wp-content/uploads/2022/05/image-37.png)
受信の規則 > 新しい規則 をクリックします
![](https://web.archive.org/web/20220704233938im_/https://pg-life.net/wp-content/uploads/2022/05/image-38-1024x280.png)
ウィザードが開かれるため、下記の通り進めます
![](https://web.archive.org/web/20220704233938im_/https://pg-life.net/wp-content/uploads/2022/05/image-39.png)
![](https://web.archive.org/web/20220704233938im_/https://pg-life.net/wp-content/uploads/2022/05/image-40.png)
![](https://web.archive.org/web/20220704233938im_/https://pg-life.net/wp-content/uploads/2022/05/image-41.png)
![](https://web.archive.org/web/20220704233938im_/https://pg-life.net/wp-content/uploads/2022/05/image-42.png)
![](https://web.archive.org/web/20220704233938im_/https://pg-life.net/wp-content/uploads/2022/05/image-43.png)
これですべての設定が完了しました
利用方法
SSMSから接続
SSMSから利用する場合、サーバ名にIPとポートを下記の形式で入力してSQLServer認証でログインすることができるようになります。
「xxx.xxx.xxx.xxx,1433」(xxx にIPアドレスを当てはめてください)
※SSMSの場合はポート番号は省略してもログインできます(1433以外で設定した場合は省略不可)
![](https://web.archive.org/web/20220704233938im_/https://pg-life.net/wp-content/uploads/2022/05/image-50.png)
接続文字列
C#などプログラムから接続する場合、接続文字列には同様に「xxx.xxx.xxx.xxx,1433」で記述します。
connectionString = "Server=xxx.xxx.xxx.xxx,1433;Database=DB;persist security info=True;user id=sa;password=PassWord;MultipleActiveResultSets=True";