简体   繁体   English

如何在 MySQL 中可靠地删除和创建数据库和用户

[英]How Can I Reliably Drop and Create a Database and a User in MySQL

I am currently running two scripts.我目前正在运行两个脚本。

01_INIT_DATABASE.SQL 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;

and

01_DROP_DATABASE.SQL 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)我怎样才能让它们可靠地运行(或者优雅地失败,例如,只删除存在的用户)

I am running them through an ant task (if that affects things, but it is really just a Java Exec), in the form of我正在通过一个 ant 任务运行它们(如果这会影响事情,但它实际上只是一个 Java Exec),形式为

<exec executable="mysql" input="./src/main/ddl/create/01_init_database.sql">
  <arg value="-uroot"/>
  <arg value="-ptoor"/>
</exec>

My MySQL Version is我的 MySQL 版本是

mysql  Ver 14.14 Distrib 5.5.52, for debian-linux-gnu (x86_64) using readline 6.2

Update IF EXISTS and IF NOT EXISTS does not work for USER.更新IF EXISTS 和 IF NOT EXISTS 对用户不起作用。 It works fine for DATABASE.它适用于 DATABASE。

ERROR 1064 (42000): You have an error in your SQL syntax; ERROR 1064 (42000):您的 SQL 语法有错误; 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检查与您的 MySQL 服务器版本相对应的手册,了解在第 1 行的 'IF NOT EXISTS 'foo'@'localhost' IDENTIFIED BY 'bar'' 附近使用的正确语法

ERROR 1064 (42000): You have an error in your SQL syntax; ERROR 1064 (42000):您的 SQL 语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF EXISTS 'foo'@'%'' at line 1检查与您的 MySQL 服务器版本相对应的手册,了解在第 1 行的 'IF EXISTS 'foo'@'%'' 附近使用的正确语法

You can add IF NOT EXISTS to your databasechema and user creation: like:您可以将 IF NOT EXISTS 添加到您的数据库架构和用户创建中:例如:

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;

and for the drop:和下降:

DROP USER IF EXISTS 'foo'@'localhost';
DROP USER IF EXISTS  'foo'@'%';
DROP DATABASE IF EXISTS foobar;

As mentioned below: the user if not exists only works on mysql 5.7 and higher.如下所述:用户 if not exist 仅适用于 mysql 5.7 及更高版本。 Do not use the create user syntax below 5.7, but change the grant statement to:不要使用5.7以下的create user语法,而是将grant语句改为:

GRANT ALL PRIVILEGES ON foobar.* TO 'foo'@'localhost' identified by 'password' WITH GRANT OPTION;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM