You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将MAMP MySQL建库命令整合到WordPress自动安装脚本函数?

How to Add MySQL Database Creation to Your WordPress Setup Function

Alright, let's get this sorted for you. Your original MySQL command had a small issue: you weren't properly passing the CREATE DATABASE statement to the MySQL client. Here's the fix, plus how to integrate it seamlessly into your existing function.

The Corrected MySQL Command

Instead of your original command, use this one—it uses the -e flag to execute the SQL statement directly (required for running one-off commands from the shell), and adds a safety check to avoid errors if the database already exists:

/Applications/MAMP/Library/bin/mysql --host=localhost -uroot -proot -e "CREATE DATABASE IF NOT EXISTS $1;"

You don't need the exit; line here—the MySQL client will automatically quit after running the command.

Updated wordpress() Function

I've inserted the corrected database creation step right after creating and entering the project directory (so the database name matches your project folder, which I assume is your intent). Here's the full updated function:

wordpress() { 
    cd /Volumes/example/example/Web/dev; 
    mkdir $1; 
    cd $1; 
    # Add database creation here
    /Applications/MAMP/Library/bin/mysql --host=localhost -uroot -proot -e "CREATE DATABASE IF NOT EXISTS $1;"
    curl -O https://wordpress.org/latest.tar.gz; 
    tar -xvzf latest.tar.gz; 
    mv wordpress/* .; 
    rmdir wordpress/; 
    rm latest.tar.gz; 
    cp wp-config-sample.php wp-config.php; 
    vim -s /Volumes/example/example/Web/dev/db_overwrite.txt wp-config.php; 
    curl -O https://downloads.wordpress.org/plugin/wp-super-cache.latest-stable.zip; 
    curl -O https://downloads.wordpress.org/plugin/wp-migrate-db.latest-stable.zip; 
    curl -O https://downloads.wordpress.org/plugin/contact-form-7.latest-stable.zip; 
    curl -O https://downloads.wordpress.org/plugin/wordpress-seo.latest-stable.zip 
    mv wp-super-cache.latest-stable.zip wp-migrate-db.latest-stable.zip contact-form-7.latest-stable.zip wordpress-seo.latest-stable.zip wp-content/plugins; 
    cd wp-content/plugins; 
    cp /Volumes/example/example/Web/Plugins/advanced-custom-fields-pro.zip . 
    unzip advanced-custom-fields-pro.zip; 
    unzip wp-super-cache.latest-stable.zip; 
    unzip wp-migrate-db.latest-stable.zip; 
    unzip contact-form-7.latest-stable.zip; 
    unzip wordpress-seo.latest-stable.zip; 
    rm wp-super-cache.latest-stable.zip wp-migrate-db.latest-stable.zip contact-form-7.latest-stable.zip wordpress-seo.latest-stable.zip; 
    cd ../themes; 
    git clone https://github.com/example/my-template; 
}

Key Notes

  • The IF NOT EXISTS clause ensures the function won't throw an error if the database with name $1 already exists—this makes your script more resilient.
  • Double-check that the $1 parameter you pass is a valid MySQL database name (avoid spaces, special characters, or MySQL reserved keywords like user or database).
  • If your MAMP MySQL credentials differ from root/root, update the -uroot -proot part to match your actual username and password.

内容的提问来源于stack exchange,提问作者Ryan Mc

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 03:55:25