Creating users and databases
To create a MySQL database and user, follow these steps:-
At the command line, log in to MySQL as the root user:
- Type the MySQL root password, and then press Enter.
-
To create a database user, type the following command. Replace username with the user you want to create, and replace password with the user’s password:
📘 Note The previous command grants the user all permissions on all databases. However, you can grant specific permissions to maintain precise control over database access. For example, to explicitly grant only the SELECT permission for the specified user, you would use the following command:
To grant the user all permissions only on the database named dbname, you would use the following command:For more information about setting MySQL database permissions, please visit https://dev.mysql.com/doc/refman/5.5/en/grant.html. - Type \q to exit the mysql program.
-
To log in to MySQL as the user you just created, type the following command. Replace username with the name of the user you created in step 3:
- Type the user’s password, and then press Enter.
-
To create a database, type the following command. Replace dbname with the name of the database that you want to create:
-
To work with the new database, type the following command. Replace dbname with the name of the database you created in step 7:
-
You can now work with the database. For example, the following commands demonstrate how to create a basic table named example, and how to insert some data into it:
Using SQL script files
The previous procedure demonstrates how to create and populate a MySQL database by typing each command interactively with the mysql program. However, you can streamline the process by combining commands into a SQL script file. The following procedure demonstrates how to use a SQL script file to create and populate a database:-
As in the previous procedure, you should first create a user for the database. To do this, type the following commands:
-
Create a file named example.sql and open it in your preferred text editor. Copy and paste the following text into the file:
-
Replace dbname with the name of the database that you want to create, and tablename with the name of the table that you want to create.
📘 Note You can modify the sample script file to create multiple databases and tables all at once. Additionally, the sample script creates a very simple table. You will likely have additional data requirements for your tables.
- Save the changes to the example.sql file and exit the text editor.
-
To process the SQL script, type the following command. Replace username with the name of the user you created in step 1:
The mysql program processes the script file statement by statement. When it finishes, the database and table are created, and the table contains the data you specified in the INSERT statements.
Deleting tables and databases
To delete a table, type the following command from the mysql> prompt. Replace tablename with the name of the table that you want to delete:This command assumes that you have already selected a database by using the USE statement.