curl command to connect to mysql database


The Curl command is a very powerful tool capable of interacting with many different types of services via the command line. Among them, by interacting with the database through the Curl command, you can quickly complete some simple data operations without opening the database client. Let's introduce how to use the Curl command to connect to the MySQL database.

Install curl and mysql-client

First, you need to make sure curl and mysql-client are installed before using the curl command.

ubuntu system:

sudo apt-get install curl mysql-client

centos system:

sudo yum install curl mysql-client

curl connects to MySQL

Then, we can use the following command to connect to MySQL, where username is your MySQL username, password is your MySQL password, and database is the name of the database you want to connect to.

curl https://localhost:3306 \
--user username:password \
--data-binary @-<< EOF
USE database;
SHOW TABLES;
EOF

Note that the MySQL default port number 3306 is used here.

After executing the above command, you will see all the tables in the database you are connected to.

When performing more complex operations, you can use SQL commands like SELECT, INSERT, UPDATE, etc.

curl https://localhost:3306 \
--user username:password \
--data-binary @-<< EOF
USE database;
INSERT INTO table (column1, column2, column3)
VALUES ('value1', 'value2', 'value3');
SELECT column1, column2 FROM table WHERE column3 = 'value3';
EOF

The above command will insert a piece of data into the table in the database you are connected to, and then output the data that meets the conditions.

hint

Finally, it is worth reminding that the Curl command is not the best way to connect to MySQL because it cannot determine syntax, logic errors, etc., and it is not convenient to use. However, when you want to quickly execute some simple commands, the Curl command is still a pretty good choice.

Guess you like

Origin blog.csdn.net/weixin_44816664/article/details/132745284