Mac Catalina下安装php7.4的sqlsrv扩展失败:无法创建目录求助
Hey there, let's work through this frustrating sqlsrv installation issue you're hitting on Catalina. That "failed to mkdir" error almost always boils down to permission problems or missing parent directories, so let's break down the fixes step by step:
Step 1: Manually Create the Target Directory & Set Correct Permissions
The error tells us pecl can't create /usr/local/Cellar/php/7.4.1/pecl/20190902—let's handle that ourselves:
- First, check if the parent
pecldirectory exists:ls -ld /usr/local/Cellar/php/7.4.1/pecl/ - If it doesn't exist (or the 20190902 subdir is missing), create it with sudo:
sudo mkdir -p /usr/local/Cellar/php/7.4.1/pecl/20190902 - Now set ownership to your user account (Homebrew expects your user to own these directories):
This ensures pecl has full write access to the directory.sudo chown -R $(whoami):admin /usr/local/Cellar/php/7.4.1/pecl/
Step 2: Re-Run the pecl Installation
With permissions fixed, try installing sqlsrv again:
pecl install sqlsrv
If you have multiple PHP versions installed, specify the 7.4 version explicitly to avoid confusion:
php7.4 pecl install sqlsrv
Step 3: Check for macOS Catalina-Specific SIP Restrictions
While /usr/local isn't protected by System Integrity Protection (SIP), it's worth verifying just in case:
- Run this command to check SIP status:
If SIP is enabled, that's fine—no need to disable it. The issue is still almost certainly permissions, not SIP.csrutil status
Alternative: Manual Compilation & Installation
If pecl still fails, you can compile the extension manually:
- Grab the sqlsrv source code compatible with PHP 7.4 (look for versions tagged for PHP 7.4 support)
- Extract the archive, navigate to the source directory in your terminal
- Run these commands to compile:
phpize ./configure make sudo make install - After compilation finishes, copy the generated
sqlsrv.sofile to the target directory:sudo cp ./modules/sqlsrv.so /usr/local/Cellar/php/7.4.1/pecl/20190902/ - Finally, add
extension=sqlsrv.soto your PHP 7.4php.inifile (you can find its location withphp7.4 --ini)
Give these steps a shot—permission issues are the most common culprit here, so the first two steps should get you sorted. If you run into any other errors, feel free to share the details!
内容的提问来源于stack exchange,提问作者seandroid

