This article describes how to do common MySQL database administration tasks from the command line using the mysql program. These common tasks include creating and deleting databases, users, and tables.
To create a MySQL database and user, follow these steps:
mysql -u root -p
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:
GRANT ALL PRIVILEGES ON *.* TO 'username'@'localhost' IDENTIFIED BY 'password';
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:
GRANT SELECT ON *.* TO 'username'@'localhost';
To grant the user all permissions only on the database named dbname, you would use the following command:
GRANT ALL PRIVILEGES ON dbname.* TO 'username'@'localhost';
For more information about setting MySQL database permissions, please visit https://dev.mysql.com/doc/refman/5.5/en/grant.html.
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:
mysql -u username -p
To create a database, type the following command. Replace dbname with the name of the database that you want to create:
CREATE DATABASE dbname;
To work with the new database, type the following command. Replace dbname with the name of the database you created in step 7:
USE dbname;
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:
CREATE TABLE example ( id smallint unsigned not null auto_increment, name varchar(20) not null, constraint pk_example primary key (id) ); INSERT INTO example ( id, name ) VALUES ( null, 'Sample data' );
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:
mysql -u root -p GRANT ALL PRIVILEGES ON *.* TO 'username'@'localhost' IDENTIFIED BY 'password'; \q
Create a file named example.sql and open it in your preferred text edtior. Copy and paste the following text into the file:
CREATE DATABASE dbname; USE dbname; CREATE TABLE tablename ( id smallint unsigned not null auto_increment, name varchar(20) not null, constraint pk_example primary key (id) ); INSERT INTO tablename ( id, name ) VALUES ( null, 'Sample data' );
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.
To process the SQL script, type the following command. Replace username with the name of the user you created in step 1:
mysql -u username -p < example.sql
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.
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:
DROP TABLE tablename;
Similarly, to delete an entire database, type the following command from the mysql> prompt. Replace dbname with the name of the database that you want to delete:
DROP DATABASE dbname;
To view a list of all users, type the following command from the mysql> prompt:
SELECT user FROM mysql.user GROUP BY user;
To delete a specific user, type the following command from the mysql> prompt. Replace username with the name of the user that you want to delete:
DELETE FROM mysql.user WHERE user = 'username';
Subscribe to receive weekly cutting edge tips, strategies, and news you need to grow your web business.
No charge. Unsubscribe anytime.
Did you find this article helpful? Then you'll love our support. Experience the A2 Hosting difference today and get a pre-secured, pre-optimized website. Check out our web hosting plans today.
We use cookies to personalize the website for you and to analyze the use of our website. You consent to this by clicking on "I consent" or by continuing your use of this website. Further information about cookies can be found in our Privacy Policy.