How to create a database from shell command?

前端 未结 8 1954
难免孤独
难免孤独 2021-01-29 17:24

I\'m looking for something like createdb in PostgreSQL or any other solution that would allow me to create database with a help of a shell command. Any hints?

8条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-29 18:06

    The ist and 2nd answer are good but if anybody is looking for having a script or If you want dynamic i.e (db/username/password in variable) then here:

    #!/bin/bash
    
    
    
    DB="mydb"
    USER="user1"
    PASS="pass_bla"
    
    mysql -uroot -prootpassword -e "CREATE DATABASE $DB CHARACTER SET utf8 COLLATE utf8_general_ci";
    mysql -uroot -prootpassword -e "CREATE USER $USER@'127.0.0.1' IDENTIFIED BY '$PASS'";
    mysql -uroot -prootpassword -e "GRANT SELECT, INSERT, UPDATE ON $DB.* TO '$USER'@'127.0.0.1'";
    

提交回复
热议问题