[英]How Can I Create A MYSQL Database Using A Name Entered By The User
I'm trying to automate the setup of new wordpress hosting clients on my webserver and each client needs it's on MYSQL DB however I cannot figure out a way to give each one a unique name by excepting input from a user 我正在尝试自动在Web服务器上设置新的wordpress托管客户端,并且每个客户端都需要将其放置在MYSQL DB上,但是我无法找出一种方法来排除用户输入,从而为每个名称赋予唯一的名称
I've tried entering a default value, ie wordpressJohn and it worked perfect however it cannot pull in the variable from above and I'm unsure if I am doing it right or not. 我尝试输入一个默认值,即wordpressJohn,它可以完美工作,但是它不能从上面拉入变量,并且我不确定我是否做对了。
read -p "Enter The Customer's Domain Name: " domainName
shortenedDomainName=$domainName | cut -f1 -d"."
sudo mysql -uroot -e "CREATE DATABASE wordpress$shortenedDomainName"
sudo mysql -uroot -e "GRANT ALL PRIVILEGES on wordpress$shortenedDomainName.* to 'admin_$shortenedDomainName'@'localhost' identified by 'arandompasswordhere';"
sudo mysql -uroot -e "FLUSH PRIVILEGES"
I would expect the script to run and create a database based off of the domain name entered ie: 我希望脚本根据输入的域名运行并创建数据库,即:
Domain entered = howtofish.com
I would expect the database wordpresshowtofish to be made, the user admin_howtofish to be given full privileges to the database using a random password 我希望数据库可以使用wordpresshowtofish,用户admin_howtofish可以使用随机密码获得对数据库的完全特权
However, what I get is: 但是,我得到的是:
Domain entered = howtofish.com
I get the database wordpress made, the user admin is given full privileges to the database using a random password 我获得了数据库wordpress,使用随机密码为用户admin授予了数据库的全部特权
The code below solved it. 下面的代码解决了它。
read -p "Enter The Customer's Domain Name: " domainName
shortdomname=${domainName//.}
dbname=${shortdomname}_db
mysql -e "CREATE DATABASE ${dbname};"
mysql -e "GRANT ALL PRIVILEGES ON ${dbname}.* TO 'dbadmin'@'localhost' IDENTIFIED BY 'random';"
mysql -e "FLUSH PRIVILEGES;"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.