简体   繁体   English

如何使用PHP PDO从Mac连接到Sql Server?

[英]How can I connect to Sql Server from a Mac with PHP PDO?

If you search Google for this question, you will find a lot of incorrect, misleading, and outdated information. 如果您在Google上搜索此问题,您会发现很多不正确,误导性和过时的信息。 Surprisingly, there isn't a solid answer on Stack Overflow, so we should change that. 令人惊讶的是,Stack Overflow没有一个可靠的答案,所以我们应该改变它。

I am using the Mac port installation of Apache and PHP. 我正在使用Apache和PHP的Mac端口安装。 I have installed php5-mssql, and I can see mssql on my phpinfo() page. 我已经安装了php5-mssql,我可以在我的phpinfo()页面上看到mssql。

But I don't see it listed under PDO. 但我不认为它在PDO下列出。

PDO support enabled
PDO drivers     dblib, mysql, odbc, pgsql 

Is mssql not associated with PDO? mssql是否与PDO无关? Is there another driver that can be used on a Mac to connect to a SqlServer database using PDO? 是否有其他驱动程序可以在Mac上使用PDO连接到SqlServer数据库? Seems like this is something that should be possible. 似乎这是应该可能的事情。

Does this help you? 这对你有帮助吗?

http://blog.nguyenvq.com/2010/05/16/freetds-unixodbc-rodbc-r/ http://blog.nguyenvq.com/2010/05/16/freetds-unixodbc-rodbc-r/

I use FreeTDS to connect to Microsoft SQL servers from a Linux server and it looks like the person in the link above has used FreeTDS to connect from a Mac. 我使用FreeTDS从Linux服务器连接到Microsoft SQL服务器,看起来上面链接中的人使用FreeTDS从Mac连接。

Here is my /etc/freetds/freetds.conf file (the only part I added was at the very end for the XYZ server): 这是我的/etc/freetds/freetds.conf文件(我添加的唯一部分是XYZ服务器的最后一部分):

[global]
        # TDS protocol version
;       tds version = 4.2

        # Whether to write a TDSDUMP file for diagnostic purposes
        # (setting this to /tmp is insecure on a multi-user system)
;       dump file = /tmp/freetds.log
;       debug flags = 0xffff

        # Command and connection timeouts
;       timeout = 10
;       connect timeout = 10

        # If you get out-of-memory errors, it may mean that your client
        # is trying to allocate a huge buffer for a TEXT field.  
        # Try setting 'text size' to a more reasonable limit 
        text size = 64512

# Define a connection to the MSSQL server.
[xyz]
        host = xyz
        port = 1433
        tds version = 8.0

[Edit by the asker] [由提问者编辑]

FreeTDS configuration is the first half of the answer. FreeTDS配置是答案的前半部分。 Once it's configured you should be able to run something like this from the command line and connect: 一旦配置完毕,您应该能够从命令行运行这样的东西并连接:

tsql -S xyz -U username -P password

Then you need to use dblib, not mssql, as the PDO driver: 然后你需要使用dblib而不是mssql作为PDO驱动程序:

$pdo = new PDO("dblib:host=$dbhost;dbname=$dbname",
                "$dbuser","$dbpwd");

Where $dbhost is the name from the freetds.conf file 其中$ dbhost是freetds.conf文件中的名称

dblib is the driver that need to be used with mssql on unix systems dblib是需要在unix系统上与mssql一起使用的驱动程序

No need for you to install anything else, 你无需安装任何其他东西,

<?php
    $dsn = 'dblib:dbname=testdb;host=127.0.0.1';
    $user = 'dbuser';
    $password = 'dbpass';
    $dbh = new PDO($dsn, $user, $password);

After looking at many threads, I've found that the best way to connect to MSSQL from Mac OS X with PHP 7 or older is to use dblib. 在查看了很多线程后,我发现使用PHP 7或更早版本从Mac OS X连接到MSSQL的最佳方法是使用dblib。 (Just download the correct php version) (只需下载正确的php版本)

You can follow these instructions (ignoring the mssql.so extension) to connect very easily: 您可以按照这些说明(忽略mssql.so扩展名)轻松连接:

https://github.com/BellevueCollege/public-docs/blob/master/PHP/configure-mssql-pdodblib-mac.md https://github.com/BellevueCollege/public-docs/blob/master/PHP/configure-mssql-pdodblib-mac.md

It worked perfect with OS X El Capitan, Bitnami with PHP 7. 它与OS X El Capitan,Bitnami和PHP 7完美配合。

Steps 1.- Install XCode 步骤1.-安装XCode

$ xcode-select ---install

2.- Install Homebrew 2.-安装Homebrew

3.- Install autoconf using Homebrew. 3.-使用Homebrew安装autoconf。

$ brew install autoconf

4.- Install FreeTDS 4.-安装FreeTDS

$ brew install freetds

5.- Download your version of PHP Source and uncompress it. 5.-下载您的PHP源版本并解压缩。

6.- Build the PDO DBLIB extension (Example for PHP 5.5.14) 6.-构建PDO DBLIB扩展(PHP 5.5.14的示例)

$ cd php-5.5.14/ext/pdo_dblib
$ phpize
$ ./configure --with-php-config=/usr/bin/php-config --with-pdo-dblib=/usr/local/
$ make
$ sudo cp modules/pdo_dblib.so /usr/lib/php/extensions/no-debug-non-zts-20121212

7.- Add the .so extensio to php.ini extension=pdo_dblib.so 7.-将.so extensio添加到php.ini extension = pdo_dblib.so

8.- Restart Apache 8.-重启Apache

9.- Connect using the dblib dsn: 9.-使用dblib dsn连接:

$pdo = new PDO("dblib:host=$dbhost;dbname=$dbname","$dbuser","$dbpwd");

Thanks Esteban for the nice guide ( https://stackoverflow.com/a/37707426 ) which succesfully helped me install the pdo_dblib driver. 感谢Esteban提供了很好的指南( https://stackoverflow.com/a/37707426 ),它成功地帮助我安装了pdo_dblib驱动程序。

However, I was having issues connecting to my Azure SQL database from OSX 10 with PHP (5.5) and FreeTDS using the provided dblib dsn. 但是,我在使用提供的dblib dsn从OSX 10与PHP(5.5)和FreeTDS连接到我的Azure SQL数据库时遇到问题。 What finally fixed it for me was appending the Azure database (m53man42a) to my username . 最后为我修复的是将Azure数据库(m53man42a)附加到我的用户名

My dblib PDO connection in PHP: 我在PHP中的dblib PDO连接:

$conn = new PDO("dblib:host=azure-sql;dbname=my-database-name", 
"username@m53man42a",
"my-secret-password");

My FreeTDS.conf: 我的FreeTDS.conf:

[azure-sql]
host = m53man42a.database.windows.net
port = 1433
tds version = 8.0
client charset = UTF-8
text size = 20971520

Consider adding this as a bullet number 10 in your list... :D 考虑在列表中将其添加为子弹编号10:D

Note that Microsoft have published a PHP7 extension for this, but if you're still on PHP5.x, that doesn't help you. 请注意,Microsoft已为此发布了PHP7扩展,但如果您仍在使用PHP5.x,那对您没有帮助。 I've succeeded in connecting using a different stack: freetds,odbc,pdo. 我已成功使用不同的堆栈进行连接:freetds,odbc,pdo。

I'm using OS X 10.11.6 (El Capitan) with Macports, PHP5.6. 我正在使用OS X 10.11.6(El Capitan)和Macports,PHP5.6。

I've started by creating an Azure SQL Database called mydb on a server with a name of myserver.database.windows.net . 我首先在名为myserver.database.windows.net的服务器上创建名为mydb的Azure SQL数据库。 It's important to remember to open the firewall to your client IP address, which you do on the server. 重要的是要记住打开防火墙到您在服务器上执行的客户端IP地址。

First step is to install freetds with the ODBC driver, and its PHP connector (change php56 to the correct version of your PHP): 第一步是安装带有ODBC驱动程序的freetds及其PHP连接器(将php56更改为正确的PHP版本):

sudo port install freetds +odbc
sudo port install php56-odbc

Next, you need to include some lines in your configuration files: 接下来,您需要在配置文件中包含一些行:

/opt/local/etc/odbcinst.ini /opt/local/etc/odbcinst.ini

[FreeTDS]
    Description = ODBC for FreeTDS
    Driver      = /opt/local/lib/libtdsodbc.so
    Setup       = /opt/local/lib/libtdsodbc.so
    FileUsage   = 1

This tells the odbc library where to find its odbc driver. 这告诉odbc库在哪里找到它的odbc驱动程序。

/opt/local/etc/freetds/freetds.conf /opt/local/etc/freetds/freetds.conf

[myserver]
    host = myserver.database.windows.net
    port = 1433
    tds version = 7.0

This tells the freetdc library where to find your server. 这告诉freetdc库在哪里找到你的服务器。

/opt/local/etc/odbc.ini /opt/local/etc/odbc.ini

[myds]
Description = Test for SQL Server on Azure
Driver = FreeTDS
Trace = Yes
TraceFile = /var/log/sql.log
Database = mydb
Servername = myserver
UserName = myusername
Password = mypassword
Port = 1433
Protocol = 7.0
ReadOnly = No
RowVersioning = No
ShowSystemTables = No
ShowOidColumn = No
FakeOidIndex = No

This creates a data source called myds pointing at your database, enabling you to connect with the following PHP: 这将创建一个名为myds的数据源,指向您的数据库,使您可以使用以下PHP连接:

$conn = new PDO('odbc:myds', 'myusername', 'mypassword');

If any of this doesn't work for you, first check that the freetds installation is correct using: 如果其中任何一项对您不起作用,请首先使用以下方法检查freetds安装是否正确:

tsql -S myserver -U myusername -P mypassword

And then check that the ODBC specifications are OK using: 然后使用以下命令检查ODBC规范是否正常:

isql -v myds myusername mypassword

Thanks to https://github.com/lionheart/django-pyodbc/wiki/Mac-setup-to-connect-to-a-MS-SQL-Server , which does the equivalent job for Python and which pointed me in the right direction for all this. 感谢https://github.com/lionheart/django-pyodbc/wiki/Mac-setup-to-connect-to-a-MS-SQL-Server ,它为Python做了相同的工作,并指出了我的权利所有这一切的方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM