0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Raspi3B + Ubuntu 20.04 から Windows 10 + SQL Server 2019 Express にアクセスしてみる

Last updated at Posted at 2021-06-12

目的

そこにUbuntu 20.04をインストールしたRaspi3Bが見えたのでつい試してみる
SQL Server関連のパッケージは今のところarm版にはリリースされていないようなので、tdsodbc、unixodbcを使用して接続してみる
※ほぼLinux PHPからSQL Serverに接続の手順通り
phpのコードはnginxとCLI共に同一コードで確認(nginx+php-fpmはまとめ中)

必要なパッケージをインストールする


$ sudo apt install freetds-common freetds-bin tdsodbc unixodbc

※perl関連のパッケージ
$ sudo apt install perl-doc
$ sudo apt install cpanminus
$ sudo cpanm install DBD::ODBC
$ sudo cpanm install DDP
$ sudo apt install libdbd-odbc-perl

※アンインストールは以下で行う
$ sudo cpanm -U DBD::ODBC -y
$ sudo cpanm -U DDP -y

※nginxとphp-fpm関連のパッケージ
$ sudo apt install nginx
$ sudo apt install php-fpm
$ sudo apt install php7.4-odbc
$ sudo apt install php7.4-mysql ※これはメモ

FreeTDSの設定

man tsqlより
 -C print some of the compile-time configuration parameters.


$ sudo tsql -C
Compile-time settings (established with the "configure" script)
                            Version: freetds v1.1.6
             freetds.conf directory: /etc/freetds
     MS db-lib source compatibility: no
        Sybase binary compatibility: yes
                      Thread safety: yes
                      iconv library: yes
                        TDS version: auto
                              iODBC: no
                           unixodbc: yes
              SSPI "trusted" logins: no
                           Kerberos: yes
                            OpenSSL: no
                             GnuTLS: yes
                               MARS: yes

/etc/freetds/freetds.confを修正する
@arachan@githubさんの指摘により以下を修正
(修正前)tds version = 8.0
(修正後)tds version = auto
※マニュアルよもうぜ・・・


[tdssqlserver]
host = 192.168.5.xxx
port = 1433
tds version = auto

SQL Serverに接続可能かを確認する
man tsqlより
-S servername
 database server to which to connect.
-U username
 database login name. If username is not provided,
 a domain login is attempted for TDS 7+ connections.
-P password
 database password.


$ sudo tsql -S tdssqlserver -U demo -P demo
locale is "C.UTF-8"
locale charset is "UTF-8"
using default charset "UTF-8"
1> quit

unixODBCの設定

/etc/odbc.iniを修正する


[sqlserver]
Description = MS SQL Server
Driver = /usr/lib/arm-linux-gnueabihf/odbc/libtdsodbc.so
Servername = tdssqlserver
Database = AdventureWorksLT2019

man odbcinsより
 odbcinst - An unixODBC tool for manipulating configuration files


$ sudo odbcinst -i -d -f /etc/odbc.ini
odbcinst: Driver installed. Usage count increased to 1.
    Target directory is /etc
  -i インストール
  -d ドライバー
  -f テンプレートファイル名

$ sudo odbcinst -q -d
[sqlserver]
  -q 検索
  -d ドライバー

$ sudo odbcinst -u -d -n sqlserver
sqlserver has been deleted (if it existed at all) because its usage count became zero
  -u アンインストール
  -n ドライバー又はDSN名

Ubuntu+DBD::ODBC->Win10+SQLServer2019Express

DBI::ODBCでアクセスしてみる

use strict;
use utf8;
use DBI;

my $dbh=DBI->connect('dbi:ODBC:sqlserver','demo','demo') or die $!;
my $sth=$dbh->prepare("select * from SalesLT.Address") or die $dbh->errstr;

# 出力エラー対策
$sth->{LongTruncOk}=1;
$sth->{LongReadLen}=2000000;

$sth->execute or die $dbh->errstr;
while(my $arrayref = $sth->fetchrow_arrayref){
        use DDP;
        p $arrayref;
}
$sth->finish;
$dbh->disconnect;

Ubuntu+PHP-FPM->Win10+SQLServer2019Express

<?php
    $dsn = 'odbc:sqlserver';
    $user = 'demo';
    $password = 'demo';

    $dbh = new PDO($dsn, $user, $password);

    $sql = "select * from SalesLT.Address";
    foreach ($dbh->query($sql) as $row) {
        print($row['AddressID'] . ' ' . $row['AddressLine1'].'<br>');
    }

    $dbh = null;
?>

参考にしたのは以下のサイト

CentOS上でPerlのDBIを使ってSQL Serverに接続する手順
LinuxからSQL ServerにODBCで接続する
Connect PHP to MSSQL via PDO ODBC
cpanm - CPANからモジュールを取得、アンパック、インストールする

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?