How to create a database from shell command?

前端 未结 8 1710
难免孤独
难免孤独 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:16

    If you create a new database it's good to create user with permissions only for this database (if anything goes wrong you won't compromise root user login and password). So everything together will look like this:

    mysql -u base_user -pbase_user_pass -e "create database new_db; GRANT ALL PRIVILEGES ON new_db.* TO new_db_user@localhost IDENTIFIED BY 'new_db_user_pass'"
    

    Where:
    base_user is the name for user with all privileges (probably the root)
    base_user_pass it's the password for base_user (lack of space between -p and base_user_pass is important)
    new_db is name for newly created database
    new_db_user is name for the new user with access only for new_db
    new_db_user_pass it's the password for new_db_user

    0 讨论(0)
  • 2021-01-29 18:27
    mysqladmin -u$USER -p$PASSWORD create $DB_NAME
    

    Replace above variables and you are good to go with this oneliner. $USER is the username, $PASSWORD is the password and $DB_NAME is the name of the database.

    0 讨论(0)
提交回复
热议问题