我正在编写将在 Ubuntu 16.04.1
服务器上运行的脚本。该脚本的目的是在服务器上安装一个 wordpress 站点。我的大部分脚本来自 this site .
我正在尝试创建和授予对数据库的权限,但是当我使用 mysql -u root -p
进入 mysql 环境时,我的脚本停止了。同样,在 bash 环境中创建的变量不会转移到 mysql
环境中。这是我为这部分编写的脚本。
echo "Please enter the Name of the Database(please make name relevant)"
read -p DBNAME
echo "Please enter the Name of the Database User(Please document username)"
read -p DBUSER
echo "Please enter a Database password(Please document password)"
read -p DBPASSWORD
a=\'
b=$a$DBPASSWORD$a
f='CREATE DATABASE '$DBNAME' CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci'
g=$a$f$a
c='GRANT ALL ON '$DBNAME.*'TO '$DBUSER'@localhost'' IDENTIFIED BY '$b
d=$a$c$a
mysql -u root -p -e $g
mysql -u root -p -e $c
mysql -u root -p -e "FLUSH PRIVILEGES"
任何帮助将环境变化整合在一起都是很棒的
最佳答案
定义
f
和c
时使用双引号。单引号不允许您的变量扩展。您将用户作为 root 用户登录到 mysql,但不提供密码,因此它会提示。也许还要求输入 root 密码。
也许像下面这样的东西可以解决问题:
echo "Please enter the Name of the Database(please make name relevant)"
read -p DBNAME
echo "Please enter the Name of the Database User(Please document username)"
read -p DBUSER
echo "Please enter a Database password(Please document password)"
read -p DBPASSWORD
echo "Please enter your database's root password"
read -p DBROOTPASSWORD
a=\'
b=$a$DBPASSWORD$a
f="CREATE DATABASE '$DBNAME' CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci"
g=$a$f$a
c="GRANT ALL ON $DBNAME.* TO $DBUSER@localhost IDENTIFIED BY "$b
d=$a$c$a
mysql -u root -p"$DBROOTPASSWORD" -e $g
mysql -u root -p"$DBROOTPASSWORD" -e $c
mysql -u root -p"$DBROOTPASSWORD" -e "FLUSH PRIVILEGES"
关于mysql - 结合 bash 和 SQL 脚本来创建 wordpress 站点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41748486/