我目前正在运行两个脚本.
I am currently running two scripts.
01_INIT_DATABASE.SQL
CREATE DATABASE foobar;
USE foobar;
CREATE USER 'foo'@'localhost' IDENTIFIED BY 'bar';
GRANT ALL PRIVILEGES ON foobar.* TO 'foo'@'localhost' WITH GRANT OPTION;
CREATE USER 'foo'@'%' IDENTIFIED BY 'bar';
GRANT ALL PRIVILEGES ON foobar.* TO 'foo'@'%' WITH GRANT OPTION;
和
01_DROP_DATABASE.SQL
USE foobar
DROP USER 'foo'@'localhost';
DROP USER 'foo'@'%';
DROP DATABASE IF EXISTS foobar;
如果一个或另一个运行不正常,它们都会有选择地失败.
They each selectively fail if one or the other has not run properly.
我怎样才能让它们可靠地运行(或者优雅地失败,例如,只删除存在的用户)
How can I get them to run reliably (or fail gracefully, for example, to only drop a user if it exists)
我正在通过一个 ant 任务运行它们(如果这会影响事情,但它实际上只是一个 Java Exec),形式为
I am running them through an ant task (if that affects things, but it is really just a Java Exec), in the form of
<exec executable="mysql" input="./src/main/ddl/create/01_init_database.sql">
<arg value="-uroot"/>
<arg value="-ptoor"/>
</exec>
我的 MySQL 版本是
My MySQL Version is
mysql Ver 14.14 Distrib 5.5.52, for debian-linux-gnu (x86_64) using readline 6.2
更新IF EXISTS 和 IF NOT EXISTS 对用户不起作用.它适用于 DATABASE.
Update IF EXISTS and IF NOT EXISTS does not work for USER. It works fine for DATABASE.
ERROR 1064 (42000):您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在第 1 行 'IF NOT EXISTS 'foo'@'localhost' IDENTIFIED BY 'bar'' 附近使用的正确语法
ERROR 1064 (42000): 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 'IF NOT EXISTS 'foo'@'localhost' IDENTIFIED BY 'bar'' at line 1
ERROR 1064 (42000):您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在第 1 行的 'IF EXISTS 'foo'@'%'' 附近使用的正确语法
ERROR 1064 (42000): 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 'IF EXISTS 'foo'@'%'' at line 1
您可以将 IF NOT EXISTS 添加到您的数据库架构和用户创建中:例如:
You can add IF NOT EXISTS to your databasechema and user creation: like:
CREATE DATABASE IF NOT EXISTS foobar;
CREATE USER IF NOT EXISTS 'foo'@'localhost' IDENTIFIED BY 'bar';
GRANT ALL PRIVILEGES ON foobar.* TO 'foo'@'localhost' WITH GRANT OPTION;
CREATE USER IF NOT EXISTS 'foo'@'%' IDENTIFIED BY 'bar';
GRANT ALL PRIVILEGES ON foobar.* TO 'foo'@'%' WITH GRANT OPTION;
以及掉落:
DROP USER IF EXISTS 'foo'@'localhost';
DROP USER IF EXISTS 'foo'@'%';
DROP DATABASE IF EXISTS foobar;
如下所述:如果用户不存在,则仅适用于 mysql 5.7 及更高版本.不要使用5.7以下的create user语法,而是将grant语句改为:
As mentioned below: the user if not exists only works on mysql 5.7 and higher. Do not use the create user syntax below 5.7, but change the grant statement to:
GRANT ALL PRIVILEGES ON foobar.* TO 'foo'@'localhost' identified by 'password' WITH GRANT OPTION;
这篇关于如何在 MySQL 中可靠地删除和创建数据库和用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
如何有效地使用窗口函数根据 N 个先前值来决定How to use windowing functions efficiently to decide next N number of rows based on N number of previous values(如何有效地使用窗口函数根据
在“GROUP BY"中重用选择表达式的结果;条款reuse the result of a select expression in the quot;GROUP BYquot; clause?(在“GROUP BY中重用选择表达式的结果;条款?)
Pyspark DataFrameWriter jdbc 函数的 ignore 选项是忽略整Does ignore option of Pyspark DataFrameWriter jdbc function ignore entire transaction or just offending rows?(Pyspark DataFrameWriter jdbc 函数的 ig
使用 INSERT INTO table ON DUPLICATE KEY 时出错,使用 Error while using INSERT INTO table ON DUPLICATE KEY, using a for loop array(使用 INSERT INTO table ON DUPLICATE KEY 时出错,使用 for 循环数组
pyspark mysql jdbc load 调用 o23.load 时发生错误 没有合pyspark mysql jdbc load An error occurred while calling o23.load No suitable driver(pyspark mysql jdbc load 调用 o23.load 时发生错误 没有合适的
如何将 Apache Spark 与 MySQL 集成以将数据库表作为How to integrate Apache Spark with MySQL for reading database tables as a spark dataframe?(如何将 Apache Spark 与 MySQL 集成以将数据库表作为