在终端上自动化MYSQL CREATE DATABASE

时间:2018-01-22 15:29:26

标签: mysql wordpress shell terminal

我正在运行我创建的脚本,可下载并安装WordPress。为了节省更多时间,我想打开MAMP MYSQL并创建一个新的数据库,如下所示:

/Applications/MAMP/Library/bin/mysql --host=localhost -uroot -proot
CREATE DATABASE $1; // passed into the function as argument
exit; // to get out of MYSQL 

我的其余功能:

wordpress() {
  cd /Volumes/example/example/Web/dev;  mkdir $1;
  cd $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;
}

如何将MYSQL行添加到此函数并使其按预期工作?

由于

1 个答案:

答案 0 :(得分:0)

为什么不使用:

/Applications/MAMP/Library/bin/mysqladmin -uroot -proot create mydatabase

mysqladmin是一个命令行工具,是标准MySQL客户端软件的一部分。它应该出现在mysql工具存在的任何地方。

您可能想阅读https://dev.mysql.com/doc/refman/5.7/en/mysqladmin.html

另一种方法是将CREATE DATABASE作为参数传递给mysql

/Applications/MAMP/Library/bin/mysql ... -e "CREATE DATABASE mydatabase"
相关问题