Connect To MySQL Database
MySQL Tutorial



Connect To MySQL Database From Command Line Guide

This article describes how to connect to MySQL from the command line using the mysql program. You can use the mysql program as a quick and easy way to access your databases directly. Table of Contents Connect To MySQL Database From Command Line More Information Related Articles

Connect To MySQL Database From Command Line

To connect to MySQL from the command line, follow these steps: Log in to your A2 Hosting account using SSH. At the command line, type the following command, replacing USERNAME with your username: mysql -u USERNAME -p At the Enter Password prompt, type your password. When you type the correct password, the mysql> prompt appears. To display a list of databases, type the following command at the mysql> prompt: show databases; To access a specific database, type the following command at the mysql> prompt, replacing DBNAME with the database that you want to access: use DBNAME; After you access a database, you can run SQL queries, list tables, and so on. Additionally: To view a list of MySQL commands, type help at the mysql> prompt. To exit the mysql program, type \q at the mysql> prompt.

How to connect to MySQL using PHP

This article describes several methods to connect to a MySQL database using PHP: MySQL Improved (mysqli) PHP extension PDO (PHP Data Objects) Legacy MySQL (mysql_) functions Connect to remote MySQL database using PHP
The MySQL databases and users must already exist before you can use these methods. For information about how to manage MySQL databases using cPanel, please see this article.Table of Contents Method #1: Connect to MySQL using MySQL Improved Method #2: Connect to MySQL using PHP Data Objects (PDO) Method #3: Connect to MySQL using legacy PHP functions Connecting to remote MySQL databases using PHP More Information Related Articles

Method #1: Connect to MySQL using MySQL Improved

The MySQL Improved extension uses the mysqli class, which replaces the set of legacy MySQL functions. To connect to MySQL using the MySQL Improved extension, follow these steps: Use the following PHP code to connect to MySQL and select a database. Replace username with your username, password with your password, and dbname with the database name: <?php $mysqli = new mysqli("localhost", "username", "password", "dbname"); ?> After the code connects to MySQL and selects the database, you can run SQL queries and perform other operations. For example, the following PHP code runs a SQL query that extracts the last names from the employees table, and stores the result in the $result variable: <?php $result = $mysqli->query("SELECT lastname FROM employees"); ?>

Method #2: Connect to MySQL using PHP Data Objects (PDO)

The MySQL Improved extension can only be used with MySQL databases. PDO, on the other hand, abstracts database access and enables you to create code that can handle different types of databases. To connect to MySQL using PDO, follow these steps: Use the following PHP code to connect to MySQL and select a database. Replace username with your username, password with your password, and dbname with the database name: <?php $myPDO = new PDO('mysql:host=localhost;dbname=dbname', 'username', 'password');  ?> After the code connects to MySQL and selects the database, you can run SQL queries and perform other operations. For example, the following PHP code runs a SQL query that extracts the last names from the employees table, and stores the result in the $result variable: <?php $result = $myPDO->query("SELECT lastname FROM employees"); ?>

Method #3: Connect to MySQL using legacy PHP functions

The original PHP MySQL functions (whose names begin with mysql_) are deprecated in PHP 5.5, and will eventually be removed from PHP. Therefore, you should only use these functions when absolutely necessary for backward compatibility. If possible, use the MySQL Improved extension or PDO instead. To connect to MySQL using the legacy PHP MySQL functions, follow these steps: Use the following PHP code to connect to MySQL and select a database. Replace username with your username, password with your password, and dbname with the database name: <?php mysql_connect('localhost','username','password'); mysql_select_db("dbname"); ?> After the code connects to MySQL and selects the database, you can run SQL queries and perform other operations. For example, the following PHP code runs a SQL query that extracts the last names from the employees table, and stores the result in the $result variable: <?php $result = mysql_query('SELECT lastname FROM employees'); ?>

Connecting to remote MySQL databases using PHP

The previous examples all assume that the PHP script runs on the same server where the MySQL database is located. But what if you want to use PHP to connect to a MySQL database from a remote location? For example, you may want to connect to your A2 Hosting database from a home computer or from another web server. To do this, you need to do two things: On the A2 Hosting server, enable the connecting IP address for remote access. For information about how to do this, please see this article. If you do not add your IP address to the list of permitted remote access hosts, you receive Access denied messages when you try to access a MySQL database remotely. In your PHP code, change the MySQL connection string to use the A2 Hosting server name instead of localhost. For example, the following PHP code uses mysqli to connect to the A2 Hosting server a2ss25.a2hosting.com: <?php $mysqli = new mysqli("a2ss25.a2hosting.com", "username", "password", "dbname"); ?>

Python MySQL Connector | How to Connect to MySQL Using Python

Python provides several ways to connect to a MySQL database and process data. This article describes three methods.
The MySQL databases and users must already exist before you can use any of the following methods. For information about how to manage MySQL databases using cPanel, please see this article.  Applications that require Python 3 should use the Python Selector. Please contact support if the Python Selector is not available on your server. Table of Contents Python MySQL Connector Setting up the Python virtual environment and installing a MySQL package Code sample More Information Related Articles

Python MySQL Connector

Before you can access MySQL databases using Python, you must install one (or more) of the following packages in a virtual environment: MySQL-python: This package contains the MySQLdb module, which is written in C. It is one of the most commonly used Python packages for MySQL. mysql-connector-python: This package contains the mysql.connector module, which is written entirely in Python. PyMySQL: This package contains the pymysql module, which is written entirely in Python. It is designed to be a drop-in replacement for the MySQL-python package. All three of these packages use Python's portable SQL database API. This means that if you switch from one module to another, you can reuse almost all of your existing code (the code sample below demonstrates how to do this).
Setting up the Python virtual environment and installing a MySQL package
To set up the Python virtual environment and install a MySQL package, follow these steps: Log in to your account using SSH. To create a virtual environment, type the following commands: cd ~ virtualenv -p /usr/bin/python2.7 sqlenv The virtualenv command creates a virtual environment named sqlenv, and subsequent commands in this procedure assume that the environment is named sqlenv. You can use any environment name you want, but make sure you replace all occurrences of sqlenv with your own environment name. To activate the virtual environment, type the following command: source sqlenv/bin/activate The command prompt now starts with (sqlenv) to indicate that you are working in a Python virtual environment. All of the following commands in this procedure assume that you are working within the virtual environment. If you log out of your SSH session (or deactivate the virtual environment by using the deactivate command), make sure you reactivate the virtual environment before following the steps below and running the sample code. Type the command for the package you want to install: To install the MySQL-python package, type the following command: pip install MySQL-python To install the mysql-connector-python package, type the following command: pip install mysql-connector-python To install the pymysql package, type the following command: pip install pymysql
Code sample
After you install a MySQL package in the virtual environment, you are ready to work with actual databases. The following sample Python code demonstrates how to do this, as well as just how easy it is to switch between the different SQL package implementations. In your own code, replace USERNAME with the MySQL database username, PASSWORD with the database user's password, and DBNAME with the database name: #!/usr/bin/python hostname = 'localhost' username = 'USERNAME' password = 'PASSWORD' database = 'DBNAME' # Simple routine to run a query on a database and print the results: def doQuery( conn ) : cur = conn.cursor() cur.execute( "SELECT fname, lname FROM employee" ) for firstname, lastname in cur.fetchall() : print firstname, lastname print "Using MySQLdb…" import MySQLdb myConnection = MySQLdb.connect( host=hostname, user=username, passwd=password, db=database ) doQuery( myConnection ) myConnection.close() print "Using pymysql…" import pymysql myConnection = pymysql.connect( host=hostname, user=username, passwd=password, db=database ) doQuery( myConnection ) myConnection.close() print "Using mysql.connector…" import mysql.connector myConnection = mysql.connector.connect( host=hostname, user=username, passwd=password, db=database ) doQuery( myConnection ) myConnection.close() This example creates a series of Connection objects that opens the same database using different MySQL modules. Because all three MySQL modules use the portable SQL database API interface, they are able to use the code in the doQuery() function without any modifications. When you have a Connection object associated with a database, you can create a Cursor object. The Cursor object enables you to run the execute() method, which in turn enables you to run raw SQL statements (in this case, a SELECT query on a table named employee). As you can see, Python's portable SQL database API makes it very easy to switch between MySQL modules in your code. In the sample above, the only code changes necessary to use a different module are to the import and connect statements.

npm MySQL | node.js MySQL Connection Guide

This article demonstrates how to connect to a MySQL database using Node.js.
Node.js must already be installed on your account. For information about how to install Node.js, please see this article. A MySQL database and user must already exist before you can follow the procedures in this article. For information about how to manage MySQL databases using cPanel, please see this article. Table of Contents MySQL Node.js Connection Via npm Installing the node-mysql package (npm) Code sample More Information Related Articles

MySQL Node.js Connection Via npm

The node-mysql package enables you to easily connect to a MySQL database using Node.js. Before you can do this, however, you must install the node-mysql package on your account.
Installing the node-mysql package (npm)
To install the node-mysql package on your account, follow these steps: Log in to your account using SSH. Type the following commands: cd ~ npm install mysql
Code sample
After you install the node-mysql package, you are ready to work with actual databases. The following sample Node.js code demonstrates how to do this. In your own code, replace dbname with the database name, username with the MySQL database username, and password with the database user's password. Additionally, you should modify the SELECT query to match a table in your own database: var mysql = require('mysql'); var connection = mysql.createConnection({ host : 'localhost', database : 'dbname', user : 'username', password : 'password', }); connection.connect(function(err) { if (err) { console.error('Error connecting: ' + err.stack); return; } console.log('Connected as id ' + connection.threadId); }); connection.query('SELECT * FROM employee', function (error, results, fields) { if (error) throw error; results.forEach(result => { console.log(result); }); }); connection.end(); This example creates a MySQL connection object that connects to the MySQL database. After the database connection is established, you can use the query method to run raw SQL statements (in this case, a SELECT query on a table named employee).

More Information

For more information about the node-mysql package, please visit https://github.com/mysqljs/mysql.

How to import and export a MySQL database

This article describes how to import MySQL databases and export MySQL databases. You can import and export databases for a variety of scenarios, including: Transferring a MySQL database from one web hosting account or provider to another. Importing a third-party MySQL database. Backing up a MySQL database. Table of Contents How to export MySQL database MySQL Export Database Method #1: Use phpMyAdmin MySQL Export Database Method #2: Use the mysqldump program Creating a new MySQL database and assigning a user How To Import MySQL database MySQL Import Database Method #1: Use phpMyAdmin MySQL Import Database Method #2: Use the mysql program Troubleshooting the MySQL database import More Information Related Articles

How to export MySQL database

You can export a MySQL database to a file by using phpMyAdmin or the mysqldump database command line program.
MySQL Export Database Method #1: Use phpMyAdmin
You can export a MySQL database using the phpMyAdmin web interface. To do this, follow these steps: Log in to cPanel.If you do not know how to log in to your cPanel account, please see this article. In the DATABASES section of the cPanel home screen, click phpMyAdmin: cPanel - Databases - phyMyAdmin icon The phpMyAdmin administration page appears in a new window. In the left pane of the phpMyAdmin page, click the database that you want to export.Click the Export tab. Under Export method, confirm that Quick is selected. If you are using an older version of phpMyAdmin that does not have the Quick option, follow these steps instead: In the Export section, click Select All. Select the Save as file checkbox, and then click Go. The export process runs. Under Format, confirm that SQL is selected.Click Go.In the Save File dialog box, type the filename and select the directory where you want to save the exported database on your local computer.Click Save. The export process runs.
MySQL Export Database Method #2: Use the mysqldump program
You can export a MySQL database from the command line using the mysqldump database program. To do this, follow these steps: Access the command line on the computer where the database is stored. For example, if the database is on another web hosting account or with another web hosting provider, log in to the account using SSH. If you have physical access to the computer, you can open a DOS or terminal window to access the command line. Type the following command, and then press Enter. Replace username with your username, and dbname with the name of the database that you want to export: mysqldump -u username -p dbname > dbexport.sql This example uses the dbexport.sql filename for the exported database, but you can name the file whatever you want.Type your password at the Enter password prompt.The dbexport.sql file now contains all of the data for the dbname database. If the dbexport.sql file is on a remote computer, download the file to your local computer.

Creating a new MySQL database and assigning a user

Before you can import the database, you must create a new database in cPanel and assign a user to it. To do this, follow these steps: Log in to cPanel.If you do not know how to log in to your cPanel account, please see this article. In the DATABASES section of the cPanel home screen, click MySQL® Databases: cPanel - MySQL Databases icon Under Create New Database, in the New Database text box, type the name of the database.Click Create Database. cPanel creates the database.When the database is created, click Go Back.Under Add User to Database, in the User list box, select the user that you want to add.In the Database list box, select the new database.Click Add.Select the check boxes to grant the user specific privileges, or select the ALL PRIVILEGES check box to grant the user all permissions to the database.Click Make Changes. cPanel adds the user to the database.

How To Import MySQL database

After you have created a new database in cPanel, you can import the database's contents by using phpMyAdmin or the mysql command line program.
If the exported database file contains any CREATE DATABASE statements, you must remove them or comment them out. Otherwise, the import process will fail.
MySQL Import Database Method #1: Use phpMyAdmin
You can import a MySQL database using the phpMyAdmin web interface. To do this, follow these steps: Log in to cPanel.If you do not know how to log in to your cPanel account, please see this article. In the DATABASES section of the cPanel home screen, click phpMyAdmin: cPanel - Databases - phyMyAdmin icon The phpMyAdmin administration page appears in a new window. In the left pane of the phpMyAdmin page, click the database that you want to import the data into.Click the Import tab.Under File to Import, click Browse, and then select the dbexport.sql file on your local computer.Click Go. The import process runs.The database should now contain the data that is in the dbexport.sql file.
MySQL Import Database Method #2: Use the mysql program
You can import a MySQL database from the command line using the mysql program. To do this, follow these steps: Transfer the dbexport.sql file to your A2 Hosting account using SCP, SFTP, or FTP. Log in to your A2 Hosting account using SSH. Change to the directory where you uploaded the dbexport.sql file. For example, if you uploaded the dbexport.sql file to your home directory, type cd ~. Type the following command, and then press Enter. Replace username with your username and dbname with the name of the database that you want to import the data into: mysql -u username -p dbname < dbexport.sql The dbname database should now contain the data that is in the dbexport.sql file.
Troubleshooting the MySQL database import
You may receive one of the following error messages when you try to import a MySQL database using either cPanel or the mysql program: ERROR 1044: Access denied for user 'username1'@'localhost' to database 'username2_database' This error message occurs when the import file contains an SQL statement that attempts to access a database for the wrong username. Note in this example that username2 in username2_database does not match username1 in 'username1'@'localhost'. You must edit the import file and change username2 to your new username1. ERROR 1049: Unknown database 'username_database' This error message occurs when the target database does not exist. Make sure you create the database first as described above, and then try to it import again. ERROR 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'username_database' at line x. This error message occurs when the import file does not contain backup data for a database or there is a MySQL syntax error in the file. Alternatively, the import file may be altered, corrupt, or in an unsupported format. (Import files must contain SQL statements; other file formats such as CSV do not work with the mysql program.) Try exporting the database again, and then try to import it.

More Information

For more information about the mysqldump database command line program, please visit http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html. For more information about the mysql command line program, please visit http://dev.mysql.com/doc/refman/5.1/en/mysql.html. For more information about phpMyAdmin, please visit http://www.phpmyadmin.net.

How to set up a remote MySQL connection

This article describes how to use a local computer to connect to your MySQL databases stored remotely on A2 Hosting servers. Table of Contents Choose a remote MySQL connection method Method #1: Set up an SSH tunnel Microsoft Windows Mac OS X and Linux Method #2: Set up a direct connection Example ODBC connection Troubleshooting More Information Related Articles

Choose a remote MySQL connection method

You can use either of the following methods to access your MySQL databases remotely: SSH tunnel: This is the more secure method. You set up an SSH tunnel that forwards a port on your local computer to the remote MySQL server. The MySQL traffic is encrypted by the SSH tunnel. Direct connection: You can set up a direct connection between your local computer and the remote MySQL server. Although it is easier to configure, it is not as secure. After you have set up a remote MySQL connection, you can use a MySQL client application to manage your databases. For more information, please see this article.

Method #1: Set up an SSH tunnel

The procedure you follow to set up an SSH tunnel between your local computer and the A2 Hosting server depends on the local computer's operating system.
Microsoft Windows
In order to use an SSH tunnel on a computer running Microsoft Windows, you need an SSH client program. A2 Hosting recommends using PuTTY, which you can download here. After you have downloaded the PuTTY executable to your local computer, you can set up an SSH tunnel. To set up an SSH tunnel on a computer running Microsoft Windows: Start PuTTY. In the Category pane, expand Connection, expand SSH, and then click Tunnels. In the Source port text box of the Port Forwarding section, type 3306.  This is the local port to forward.In the Destination text box, type localhost:3306. Confirm that the Local and Auto radio buttons are selected. Click Add: Putty configuration window for tunnel. In the Category pane, click Session.In the Host Name (or IP address) text box, type your web site's domain name or IP address.In the Port text box, type 7822.Confirm that the Connection type radio button is set to SSH. Click Open: Putty configuration panel for session. If a PuTTY security alert about the server's host key appears, click Yes.When the login as prompt appears, type your A2 Hosting username, and then type your password. When the remote server's command line prompt appears, the SSH tunnel is established and you can use your MySQL client applications on the local computer.
To verify that PuTTY is forwarding ports correctly, you can click the icon in the top-left corner of the PuTTY session window, and then click Event Log. If port forwarding is working correctly, you see a line similar to: Local port 3306 forwarding to localhost:3306
Mac OS X and Linux
To establish an SSH tunnel on a computer running Mac OS X or Linux: Open a terminal window and type the following command at the command line. Replace username with your A2 Hosting username, and replace example.com with your site's domain name: ssh -p 7822 username@example.com -L 3306:localhost:3306 Type your password, and then press Enter. When the remote server's command line prompt appears, the SSH tunnel is established and you can use your MySQL client applications on the local computer.

Method #2: Set up a direct connection

To set up a direct connection between your local computer and the MySQL server, you must enable remote MySQL access in cPanel. For information about how to do this, please see this article.

Example ODBC connection

The following procedure demonstrates how to configure a remote ODBC connection: Launch the ODBC Data Source Administrator. Click Add to add a new data source. In the Create New Data Source dialog box, click the MySQL ODBC driver. Click Finish to start the MySQL Connector/ODBC Data Source Configuration. On the MySQL Connector/ODBC Data Source Configuration dialog box, in the Data Source Name text box, type a descriptive name for the data source. Confirm that TCP/IP Server is selected. Do one of the following: If you are using an SSH tunnel, in the TCP/IP Server text box, type localhost. If you are using a direct connection, in the TCP/IP Server text box, type the domain or IP address of the remote server. In the Port text box, type 3306.In the User text box, type a database username that can connect to the remote database.In the Password text box, type the password for the username. To test the connection, click Test: Dialog box for MySQL Connector/ODBC Data Source Configuration Optionally, you can use the Database list box to select an initial database for the connection.To complete the configuration, click OK.

Troubleshooting

If you are doing development work on the local computer, a local copy of MySQL may already be running on port 3306. In this case, the following connection error appears: Error message with local port conflict. To work around this issue, create a tunnel with an alternate local port, and then connect to the remote MySQL server on that port. If you use Putty to create the tunnel, use an alternate Source port on the Tunnels configuration page. In this example, port 3307 is being used: Putty tunnel configuration with alternate port. The session configuration does not change. For Mac OS X or Linux, use the following SSH command to create the tunnel with a local port of 3307. Replace username with your A2 Hosting username, and replace example.com with your site's domain name: ssh -p 7822 username@example.com -L 3307:localhost:3306 Create the ODBC connection as previously described, but type 3307 in the Port text box (instead of 3306). Dialog box for MySQL Connector/ODBC Data Source Configuration with alternate port. You should now be able to connect.

More Information

To view the online documentation for PuTTY, please visit http://the.earth.li/~sgtatham/putty/0.60/htmldoc/index.html.

Using MySQL client applications

This article describes how to use MySQL client applications to access and manage your MySQL databases. Table of Contents MySQL client applications MySQL Workbench More Information Related Articles

MySQL client applications

MySQL provides GUI (graphical user interface) client applications that you can use for database management: MySQL Workbench: This integrated tools environment enables you to manage databases, run queries, and much more. It replaces the older MySQL GUI Tools application bundle, which is no longer actively developed. MySQL Workbench is free to download and use. There are versions for Microsoft Windows, Mac OS X, Linux, and other operating systems.

MySQL Workbench

MySQL Workbench enables you to quickly connect to your MySQL databases. To do this, follow these steps: Start MySQL Workbench. From the workspace page, click the + icon next to MySQL Connections. The Setup New Connection dialog appears. In the Connection Name text box, type a name for the connection. In the Connection Method list box, select Standard (TCP/IP). On the Parameters tab, in the Hostname text box, type example.com, where example.com represents your domain name. Confirm that the Port text box is set to 3306. In the Username text box, type one of the following usernames: Your A2 Hosting (cPanel) account username: With this username, you can access all of the MySQL databases on your hosting account. A MySQL database username you created in cPanel: With this username, you can access all of the MySQL databases for which the specified user has been granted privileges. In the Default Schema text box, type the name of the MySQL database that you want to access. Alternatively, you can leave this text box blank to select the database later. Click Test Connection, and then in the Password text box, type the password for the username you specified in step 7.
To have MySQL Workbench remember your password, select the Save password in keychain check box. Click OK. If you receive a connection warning about incompatible or nonstandard server versions, click Continue Anyway.If the connection is successful, you receive a Successfully made the MySQL connection message. If you do not receive this message, check the values you specified in steps 4 to 9, and then try again.Click OK. MySQL Workbench saves the new connection on the workspace page.To open the connection, click it on the workspace page. You can now run queries, create tables, and more.    

More Information

To download MySQL Workbench and view the online documentation, please visit http://dev.mysql.com/downloads/tools/workbench.

How to Create & Manage MySQL Databases, Tables & Users

This article describes how to do common MySQL database administration tasks from the command line using the mysql program.
This article only applies to the products listed in the Article Details sidebar. If your account includes cPanel, you should use it instead to manage MySQL databases and users. For information about how to do this, please see this article.Table of Contents Create MySQL Databases and Users Using SQL script files Delete MySQL Tables and Databases Delete MySQL Users More Information Related Articles

Create MySQL Databases and Users

To create MySQL database and users, follow these steps: At the command line, log in to MySQL as the root user: mysql -u root -p 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: GRANT ALL PRIVILEGES ON *.* TO 'username'@'localhost' IDENTIFIED BY 'password'; This command grants the user all permissions. However, you can grant specific permissions to maintain precise control over database access. For example, to explicitly grant the SELECT permission, you would use the following command: GRANT SELECT ON *.* TO 'username'@'localhost'; 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: mysql -u username -p 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: 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' );
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: 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. 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: 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.

Delete MySQL 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: DROP TABLE tablename; This command assumes that you have already selected a database by using the USE statement. 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;
The mysql program does not ask for confirmation when you use this command. As soon as you press Enter, MySQL deletes the database and all of the data it contains.

Delete MySQL Users

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';

More Information

To view the official MySQL documentation, please visit https://dev.mysql.com/doc/refman/5.5/en/index.html.