LoginSignup
2
1

More than 5 years have passed since last update.

CircleCI でLaravel+SQLServer を構築したらエラーが出た

Last updated at Posted at 2018-12-14

発生した現象について

エラー内容

SQLSTATE[HY000]: General error: 4004 Unicode data in a Unicode-only collation or ntext data cannot be sent to clients using DB-Library (such as ISQL) or ODBC version 3.7 or earlier. [4004] (severity 16) [(null)] (SQL: 

.circleci/config(各種バージョン)

  build:
    docker:
      - image: circleci/php:7.1-node-browsers
      - image: microsoft/mssql-server-linux:2017-CU8
        environment:
          - ACCEPT_EULA: Y
          - SA_PASSWORD: 1234abcD
  steps:
      - checkout # special step to check out source code to working directory
      - run: sudo apt install -y zlib1g-dev ruby freetds-dev unixodbc-dev imagemagick libmagickcore-dev libmagickwand-dev apt-transport-https
      - run: curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
      - run: curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list | sudo tee /etc/apt/sources.list.d/msprod.list
      - run: sudo apt update
      - run: sudo ACCEPT_EULA=Y apt install -y mssql-tools unixodbc-dev

調査方法

circleci にssh ログインして色々叩いてみよう

  1. circleci でbuild に失敗したモノについれSSHを有効にしてRebuildする

スクリーンショット 2018-12-14 12.34.26.png

  1. 一番最後にSSHへのログイン方法が出てくるのでログインする。Githubに登録してある公開鍵に関連した秘密鍵が必要なので忘れないように!

スクリーンショット 2018-12-14 12.36.14.png

  1. 調査開始

sqlcmd を叩いてみる

注意点
/opt/mssql-tools/bin/ にはパスを通してないので全部叩かないと駄目だぞ!

$ /opt/mssql-tools/bin/sqlcmd

Laravelとの通信をしてるfreetds を疑う

とりあえずインストールから

$ sudo apt-get install freetds-bin
$ tsql -C
Compile-time settings (established with the "configure" script)
                            Version: freetds v0.91
             freetds.conf directory: /etc/freetds
     MS db-lib source compatibility: no
        Sybase binary compatibility: yes
                      Thread safety: yes
                      iconv library: yes
                        TDS version: 4.2
                              iODBC: no
                           unixodbc: yes
              SSPI "trusted" logins: no
                           Kerberos: yes

接続して色々確認

$ tsql -S 0.0.0.0 -U [hogehoge] -P [hogehoge]
locale is "C.UTF-8"
locale charset is "UTF-8"
using default charset "UTF-8"
1> select @@version
2> GO

Microsoft SQL Server 2017 (RTM-CU8) (KB4338363) - 14.0.3029.16 (X64)
    Jun 13 2018 13:35:56
    Copyright (C) 2017 Microsoft Corporation
    Developer Edition (64-bit) on Linux (Ubuntu 16.04.4 LTS)
(1 row affected)
1> use hogehoge
2> GO
1> SELECT TOP 1 * FROM hogehoge
2> GO
Msg 4004 (severity 16, state 1) from 030843d6b223 Line 1:
    "Unicode data in a Unicode-only collation or ntext data cannot be sent to clients using DB-Library (such as ISQL) or ODBC version 3.7 or earlier."

あー。freetds周りの設定ぽいですね。。

解決方法

CircleCIへfreetdsへの反映をする

エラー的にはこことドンピシャ
http://php.net/manual/ja/function.mssql-query.php#105843

なので、

        tds version = 8.0
        client charset = UTF-8

を追加してあげればおっけー

そんな追加したfreetds.conf をGit上に用意します
今回は、.circleci/freetds.conf に用意しました
蛇足ですが、.circleci 周りにしか使わないのは .circleci ディレクトリに集める傾向があります。。。

  build:
    docker:
      - image: circleci/php:7.1-node-browsers
      - image: microsoft/mssql-server-linux:2017-CU8
        environment:
          - ACCEPT_EULA: Y
          - SA_PASSWORD: 1234abcD
  steps:
      - checkout # special step to check out source code to working directory
      - run: sudo apt install -y zlib1g-dev ruby freetds-dev unixodbc-dev imagemagick libmagickcore-dev libmagickwand-dev apt-transport-https
      - run: curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
      - run: curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list | sudo tee /etc/apt/sources.list.d/msprod.list
      - run: sudo cp .circleci/freetds.conf /etc/freetds/freetds.conf
      - run: sudo apt update
      - run: sudo ACCEPT_EULA=Y apt install -y mssql-tools unixodbc-dev

これで問題なくなりました
よかったよかった

2
1
2

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
2
1