开发者

MySQL - Automate creation of database, tables and user

For deploying an application, I'm required to create a database for basic user authentication. To automate the process, I've written a MySQL batch file:

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');

I've been doing this for the first time, so I'm quite sure there are some things I might not get right. Is it actually a good idea to populate the users table with one user? To log into my application, there needs to be some account, still I'm not quite sure.

Another thing that bugs me is the grant statement. I wonder if grant all may not be restrictive enough.

Also, should I check for if not exists on every table I'm going to create?

Are there any best practices concerning batch scripts?


  1. Bootstrapping your database with a user is OK. Just remember to update the script if 'jetty' ever leaves!
  2. GRANT ALL is not very restrictive, that is true. Locking down more tightly is usually a good thing.
  3. Checking IF NOT EXISTS means two things: (a) your script won't fail if the table already exists [good] and (b) you won't update an existing table [bad]. I like to DROP TABLE IF EXISTS and then CREATE TABLE.

Be sure to make a backup before DROPing those tables though.

Good luck.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜