[英]MySQL - Automate creation of database, tables and user
为了部署应用程序,我需要为基本用户身份验证创建一个数据库。 为了自动化这个过程,我编写了一个 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
?
有没有关于批处理脚本的最佳实践?
不过,请务必在DROP
之前备份这些表。
祝你好运。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.