我有一个创建数据库的 bash,为它分配用户权限,然后加载转储。我从 opensuse 中的 bash 中执行此操作。使用具有数据库名称的参数 = $1。
这是我处理此问题的脚本部分:
echo "creating database"
#(This is Line 22)create new database and configure grants
mysql -hlocalhost -uroot -e "create database $1;"
echo "select db, use"
mysql -hlocalhost -uroot -e "use $1;"
echo "granting rights"
mysql -hlocalhost -uroot -e "grant all on $1.* to 'smic_db_root'@'localhost' identified by
'sm_for_all_987';"
echo "loading dump"
#load the database dump
mysql -h localhost -uroot $1</opt/otrsadm/otrs_template.sql
这是我的错误日志:
creating database
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
select db, use
ERROR at line 1: USE must be followed by a database name
granting rights
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '* to 'smic_db_root'@'localhost' identified by
'sm_for_all_987'' at line 1
loading dump
ERROR 1046 (3D000) at line 22: No database selected
我该怎么做才能在 bash 中选择数据库以及何时需要这样做。由于错误消息指的是第 22 行,因此我需要在内部某处执行此操作。我尝试添加 use $1;像这样:
mysql -hlocalhost -uroot -e "create database $1; **use $1;** grant all on $1.* to 'smic_db_root'@'localhost' identified by 'sm_for_all_987';"
行为没有区别。
创建/授予命令不需要选择数据库。它们明确地在全局级别(创建)或在 mysql 数据库(授予)上工作。尝试进行一些调试以准确查看错误发生的位置?
echo "creating database"
mysql .... 'create database ..';
echo "granting rights"
mysql .... 'grant ....';
echo "loading dump"
mysql .... < file.sql
现在错误信息不会告诉你什么失败了,只是告诉你有什么失败了。通过如上添加的 echo ,您至少会知道哪个阶段导致了哪个错误。
我是一名优秀的程序员,十分优秀!