MySQL is one of the most popular database engines on the internet. It is robust, interoperable, has a quick learning curve, and can support a considerable workload. Today we will see how to make a mysql connection from the terminal with different client options.
The fastest way to make a connection is to execute the
mysql command with the name of the database you wish to enter.
To connect in this way, you need to have a default user configured in the mysql configuration file. This file is usually called my.cfn (Unix) or default.cfn (Windows). The location of this file depends on how mysql was installed and on what operating system. This file has some configurations such as the default authentication plugin, data directory, among others, and may look similar to this:
# set basedir to your installation path
# set datadir to the location of your data directory
All you have to do is add the following configuration.
If the user you parameterized by default has no password, you can skip the second line of code.
Connection with User, Password and Database
The most common way to connect is to explicitly indicate the user and database you wish to enter.
mysql --user=user_name --password db_name
--user parameter can be replaced with
-u and the
--password parameter with
-p. The latter indicates that the user's password must be entered once the command has been executed (the console will request it).
Connection and Execution of a Command
In some cases, it is useful to make a connection in order to execute a specific command and terminate the session immediately. It can be, for example, a scheduled task or an execution from a programming language.
mysql -u user_name --p db_name --execute='select now()'
--execute parameter can be replaced with
-e. The output of the command will be displayed on the console.
Connection and Execution of a File
Another common way to connect is to execute a script. To do this, you must indicate the path of the file with the
mysql -u user_name -p < your_file
Although there are a couple more options, the above are the most common in most cases. I hope this post has been a great help to you. See you soon!.