google-templatesmysql

Create a new MySQL database and user

Log in to the database server using the MySQL client and the correct credentials. Then, follow the steps below to create a new database and user for your applications.

The commands below create both a local user and a remote user. The local user can be used only for local connections (connections originating from the same host), while the remote user can be used for external connections (connections originating outside the host).

  • Create a new database:

      mysql> create database DATABASE_NAME;
    
  • Create a new user (only with local access) and grant privileges to this user on the new database.

    If your stack ships MySQL v8.x:

      mysql> create user 'USER_NAME'@'localhost' identified by 'PASSWORD';
    
      mysql> grant all privileges on DATABASE_NAME.* TO 'USER_NAME'@'localhost';
    

    If your stack ships an older version of MySQL:

      mysql> grant all privileges on DATABASE_NAME.* TO 'USER_NAME'@'localhost' identified by 'PASSWORD';
    

    TIP: Check the MySQL version with the command /opt/bitnami/mysql/bin/mysqladmin --version or /opt/bitnami/mysql/bin/mysqld --version

  • Create a new user (with remote access) and grant privileges to this user on the new database.

    If your stack ships MySQL v8.x:

      mysql> create user 'USER_NAME'@'%' identified by 'PASSWORD';
    
      mysql> grant all privileges on DATABASE_NAME.* TO 'USER_NAME'@'%';
    

    If your stack ships an older version of MySQL:

      mysql> grant all privileges on DATABASE_NAME.* TO 'USER_NAME'@'%' identified by 'PASSWORD';
    
  • After modifying the MySQL grant tables, execute the following command in order to apply the changes:

      mysql> flush privileges;
    

Some applications require specific privileges in the database. Check the MySQL official getting started guide.

Last modification December 21, 2022