由于项目需要从 SqlServer 迁移数据到 Mysql,需要连接 SqlServer 数据库。
第一次处理这个事情,还是在 Yii2 框架里面,折腾了 3 天,终于弄好了。
经验:遇到问题 百度 找不到就去谷歌,最后的问题靠谷歌解决的。
第一步:要支持 Sqlserver 先安装2个扩展:sqlsrv.so 和 pdo_sqlsrv.so
我的环境:php7.2
#############安装 sqlsrv 扩展#############
# cd ~/mssql/ # wget https://pecl.php.net/get/sqlsrv-4.3.0.tgz # tar -zxvf sqlsrv-4.3.0.tgz # cd sqlsrv-4.3.0 # phpize # ./configure --with-php-config=/usr/local/php/bin/php-config # make #如果 make 报错,重新安装 unixODBC-devel /*sql/sqlsrv-4.3.0/conn.cpp -fPIC -DPIC -o .libs/conn.o In file included from /root/mssql/sqlsrv-4.3.0/shared/typedefs_for_linux.h:23:0, from /root/mssql/sqlsrv-4.3.0/shared/xplat_winnls.h:24, from /root/mssql/sqlsrv-4.3.0/shared/FormattedPrint.h:24, from /root/mssql/sqlsrv-4.3.0/shared/core_sqlsrv.h:41, from /root/mssql/sqlsrv-4.3.0/php_sqlsrv.h:25, from /root/mssql/sqlsrv-4.3.0/conn.cpp:20: /root/mssql/sqlsrv-4.3.0/shared/xplat.h:30:17: fatal error: sql.h: No such file or directory #include <sql.h> ^ compilation terminated. make: *** [conn.lo] Error 1*/ ###重新安装 unixODBC-devel### #yum -y install unixODBC-devel # make install Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20170718/ #php.ini 增加扩展 echo "extension=sqlsrv.so" >> /usr/local/php/etc/php.ini
##############安装 pdo sqlsrv 扩展 ################
#cd ~/mssql/ #wget https://pecl.php.net/get/pdo_sqlsrv-4.3.0.tgz #tar -zxvf pdo_sqlsrv-4.3.0.tgz #cd pdo_sqlsrv-4.3.0 #phpize #./configure --with-php-config=/usr/local/php/bin/php-config # make && make install # php.ini 添加 pdo_sqlsrv.so 扩展 # echo "extension=pdo_sqlsrv.so" >> /usr/local/php/etc/php.ini # 重启 php-fpm # /etc/init.d/php-fpm restart
############Yii2 配置 Sqlsev 配置 ##########
参考:https://www.yiichina.com/doc/guide/2.0/db-active-record
config/app.php 'db2'=>[ 'class' => 'yii\db\Connection', 'driverName' => 'sqlsrv', 'dsn' => 'sqlsrv:Server='.$db2['host'].','.$db2['port'].';Database='.$db2['dbname'], 'username' => $db2['username'], 'password' => $db2['password'], 'tablePrefix' => $db2['tablePrefix'], 'charset' => $db2['charset'], ], //使用方法:再模型里面重写此方法 /** * 使用 db2 连接配置 * @return \yii\db\Connection */ public static function getDb() { return \Yii::$app->getOtherDb('db2'); }
#############错误处理##############
Can't open lib 'ODBC Driver 13 for SQL Server' : file not found
需要安装
#yum -y install msodbcsql
#碰到问题服务连接,需要在 centos 安装客户:
Download the Microsoft Red Hat repository configuration file. #curl https://packages.microsoft.com/config/rhel/8/prod.repo > /etc/yum.repos.d/msprod.repo Exit superuser mode. #exit If you had a previous version of mssql-tools installed, remove any older unixODBC packages. #sudo yum remove mssql-tools unixODBC-utf16-devel Run the following commands to install mssql-tools with the unixODBC developer package. #sudo yum install mssql-tools unixODBC-devel Note To update to the latest version of mssql-tools run the following commands: #sudo yum check-update #sudo yum update mssql-tools Optional: Add /opt/mssql-tools/bin/ to your PATH environment variable in a bash shell. To make sqlcmd/bcp accessible from the bash shell for login sessions, modify your PATH in the ~/.bash_profile file with the following command: #echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile To make sqlcmd/bcp accessible from the bash shell for interactive/non-login sessions, modify the PATH in the ~/.bashrc file with the following command: #echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrcsource ~/.bashrc
sqlcmd 使用例子:
sqlcmd -S IP,端口 -U 用户明 -P 密码
安装 sqlcmd :https://docs.microsoft.com/en-us/sql/linux/sql-server-linux-setup-tools?view=sql-server-ver15#RHEL
sqlcmd 使用参考:https://blog.csdn.net/example440982/article/details/53572218