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

时间:2023-04-03
本文介绍了如何在 MySQL 中可靠地删除和创建数据库和用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我目前正在运行两个脚本.

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 中可靠地删除和创建数据库和用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:用于检查数据库(连接)是否存在的 Ant 任务? 下一篇:如何使用 ANT 任务启动 MySql

相关文章