MySQL provides different methods in order to insert data into tables or databases. We can use the command line interface in order to insert data into the table or database. The mysql command can be used to connect local or remote MySQL servers and run SQL queries to insert data into the table.
Connect MySQL via Command Line Interface
First, we need to connect to the MySQL server. The MySQL server can be a local or remote database server. We can provide the remote server IP address or specify the 127.0.0.1 for the local host. We use the
-u to specify the MySQL server user. In the following example, we use the default root user to connect the MySQL server. The
-p is used to specify passwords but is not specified it is prompted interactively during connection which is a more secure way.
$ mysql -h 192.168.1.10 -u root -p
If we need to connect local MySQL server we can use the following command without specifying the host IP address.
$ mysql -u root -p
After connecting to the MySQL server via command line interface we should select the database we want to work. The
use statement is used to specify the default database we want to work and run the query. In the following example, we set the default database as “customers”.
mysql> use customers;
Run SQL Query To Insert Data
We can run SQL queries in order to insert data into the MySQL server in the MySQL interactive command line interface. In the following example, we add a new user to the table named users.
mysql> INSERT INTO users (name,surname) VALUES ("ismail","baydan");
List Inserted Data
We can list the insert data with the SELECT query like below.
mysql> SELECT * FROM users;