1. <i id='QrBK0'><tr id='QrBK0'><dt id='QrBK0'><q id='QrBK0'><span id='QrBK0'><b id='QrBK0'><form id='QrBK0'><ins id='QrBK0'></ins><ul id='QrBK0'></ul><sub id='QrBK0'></sub></form><legend id='QrBK0'></legend><bdo id='QrBK0'><pre id='QrBK0'><center id='QrBK0'></center></pre></bdo></b><th id='QrBK0'></th></span></q></dt></tr></i><div id='QrBK0'><tfoot id='QrBK0'></tfoot><dl id='QrBK0'><fieldset id='QrBK0'></fieldset></dl></div>

        • <bdo id='QrBK0'></bdo><ul id='QrBK0'></ul>
      2. <tfoot id='QrBK0'></tfoot>
        <legend id='QrBK0'><style id='QrBK0'><dir id='QrBK0'><q id='QrBK0'></q></dir></style></legend>
      3. <small id='QrBK0'></small><noframes id='QrBK0'>

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

        时间:2023-08-21

          1. <small id='PdwUM'></small><noframes id='PdwUM'>

            <i id='PdwUM'><tr id='PdwUM'><dt id='PdwUM'><q id='PdwUM'><span id='PdwUM'><b id='PdwUM'><form id='PdwUM'><ins id='PdwUM'></ins><ul id='PdwUM'></ul><sub id='PdwUM'></sub></form><legend id='PdwUM'></legend><bdo id='PdwUM'><pre id='PdwUM'><center id='PdwUM'></center></pre></bdo></b><th id='PdwUM'></th></span></q></dt></tr></i><div id='PdwUM'><tfoot id='PdwUM'></tfoot><dl id='PdwUM'><fieldset id='PdwUM'></fieldset></dl></div>

            <legend id='PdwUM'><style id='PdwUM'><dir id='PdwUM'><q id='PdwUM'></q></dir></style></legend>

          2. <tfoot id='PdwUM'></tfoot>
                  <tbody id='PdwUM'></tbody>
                  <bdo id='PdwUM'></bdo><ul id='PdwUM'></ul>
                  本文介绍了如何在 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 中可靠地删除和创建数据库和用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

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

                  相关文章

                  最新文章

                  1. <legend id='UZFcE'><style id='UZFcE'><dir id='UZFcE'><q id='UZFcE'></q></dir></style></legend><tfoot id='UZFcE'></tfoot>

                      <small id='UZFcE'></small><noframes id='UZFcE'>

                      • <bdo id='UZFcE'></bdo><ul id='UZFcE'></ul>
                    1. <i id='UZFcE'><tr id='UZFcE'><dt id='UZFcE'><q id='UZFcE'><span id='UZFcE'><b id='UZFcE'><form id='UZFcE'><ins id='UZFcE'></ins><ul id='UZFcE'></ul><sub id='UZFcE'></sub></form><legend id='UZFcE'></legend><bdo id='UZFcE'><pre id='UZFcE'><center id='UZFcE'></center></pre></bdo></b><th id='UZFcE'></th></span></q></dt></tr></i><div id='UZFcE'><tfoot id='UZFcE'></tfoot><dl id='UZFcE'><fieldset id='UZFcE'></fieldset></dl></div>