简体   繁体   English

如何使用用户输入的名称创建MYSQL数据库

[英]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.

相关问题 如何在 MySQL 中可靠地删除和创建数据库和用户 - How Can I Reliably Drop and Create a Database and a User in MySQL 用户输入文本后,如何使用 AJAX 更新我的数据库? - How can I update my Database using AJAX after the user has entered text? 如何使用php将用户输入的信息传递到数据库? - How can I pass my user entered information to my database using php? 如何从 JSP 文件中的 HTML 表单中获取用户输入的数据,并使用 java 类将其输入到 mysql 数据库中? - How do I take user entered data from HTML forms in a JSP file, and enter it into a mysql database using a java class? 如何使用PHP检查用户是否已向Mysql输入数据? - How to check if the user has entered or not entered data to Mysql using PHP? 如何维护在HTML表单中输入并存储在MYSQL数据库中的文本的用户格式? - How do I maintain user-formatting of text entered in an HTML form and stored in a MYSQL database? 如何导出MySQL数据库用户? - How can I export a MySQL database user? 如何使用JDBC将jsp输入类型“时间”中的用户输入值插入MYSQL数据库 - How to insert the user entered value in jsp input type “time” to MYSQL database using JDBC 如何使用 php 创建一个 MYSQL 表,我们可以在其中从 mysql 数据库值(用户会话)设置表名? - How To Create A MYSQL tabe with php in which we can set the table name from a mysql database value(user session)? 如何创建MySQL数据库 - How can I create a MySQL-database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM