gpt4 book ai didi

MySQL - 自动创建数据库、表和用户

转载 作者:行者123 更新时间:2023-11-29 00:56:28 25 4
gpt4 key购买 nike

为了部署应用程序,我需要为基本用户身份验证创建一个数据库。为了使该过程自动化,我编写了一个 MySQL 批处理文件:

grant all on jetty.* to 'jetty'@'localhost' identified by 'jetty';

create database if not exists jetty;
use jetty;

create table if not exists users
(
id int unsigned not null auto_increment,
username varchar(100) not null,
password binary(60) not null,
primary key(id),
unique(username)
);

create table if not exists roles
(
id int unsigned not null auto_increment,
role varchar(100) not null,
primary key(id),
unique(role)
);

create table if not exists user_roles
(
user_id int unsigned not null,
role_id int unsigned not null,
unique(user_id, role_id),
index(user_id)
);

insert into users(username, password) values('jetty', $2a$10$YX3cYqn9nHmCOmPZBHXrQ.2nxrktB3CRYG8tXmBmmWvrDKU8uwRSe');

我是第一次这样做,所以我很确定有些事情我可能做错了。用一个用户填充 users 表实际上是个好主意吗?要登录我的应用程序,需要一些帐户,但我仍然不太确定。

另一件让我烦恼的事情是 grant 语句。我想知道 grant all 是否限制得不够。

此外,我是否应该在我要创建的每个表上检查if not exists

是否有关于批处理脚本的最佳实践?

最佳答案

  1. 使用用户启动您的数据库是可以的。如果“码头”离开,请记住更新脚本!
  2. GRANT ALL 并不是非常严格,这是事实。更严格地锁定通常是一件好事。
  3. 检查 IF NOT EXISTS 意味着两件事:(a) 如果表已经存在,您的脚本不会失败 [好] 和 (b) 您不会更新现有表 [坏]。我喜欢 DROP TABLE IF EXISTS 然后创建 TABLE。

尽管如此,请务必在DROPing 那些表之前进行备份。

祝你好运。

关于MySQL - 自动创建数据库、表和用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5888614/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com