As we all know, there are many ways to log in to the mysql database. Below, the editor will introduce several methods to log in to the mysql database. Friends in need can refer to it.
Several ways to log in to the mysql database
The first one (through the client that comes with mysql, MySQL 5.5 Command Line Client) This is not recommended Method
##Note: This login method, Only applicable to root users, not flexible enough! (Only suitable for root user login, only for root user. We may have many other users in the future, then other users will not be able to log in this way, so this way to log in to the mysql database has limitations), so it is not recommended. Use this method to log in to the mysql databaseTo log out, you can use the exit command or ctrl c as shown below: Type 2 (using Windows dos Command window, use mysql command) This method is recommended, as shown in the screenshot: win key r and then enter cmd## as shown below : Enter the command mysql -h localhost -P 3306 -u root -proot
Next, I will explain the mysql command in detail
mysql -h localhost -P 3306 -u root -proot
(1.) You can understand the mysql at the front as a keyword or a fixed command. It is a fixed writing method, similar to the javac command or java command in java and jdk
(2 .) -h means host, which is the IP address of the host
(3.) -P means port, port. The default port of the mysql database is 3306. Of course, you can change the port number yourself. I don’t have it here. Change the port number (note: this is the capital letter P)
(4.) -u represents the user name
(5.) -p represents the password (note: this is lowercase The letter p)
Let’s talk about the precautions for the mysql command:
The uppercase P represents the port number, the lowercase p represents the password, everyone remembers it
Everyone Remember, the lowercase p represents the password. There must be no space between -p and the password. Others, such as -u, -h, -P, can have spaces or no spaces
Note: If it is the local machine, the host ip and port number do not need to be written (that is, the host ip and port number can be omitted), directly write mysql -u root -proot
If it is the local machine, but the port You changed the port number to another port number, not the default 3306. For example, if you changed the port number to 6688, then you add the port number, that is, mysql -P 6688 -u root -proot
The following three syntaxes are correct. I will give examples and screenshots in order
The username I use here is root, and the password is also root
Syntax 1: mysql -h host ip address- P port number -u username -p password (there is a space between -h and the host ip address, there is a space between -P and the port number, there is a space between -u and the username, there must be no space between -p and the password There are spaces)
mysql -h localhost -P 3306 -u root -proot
If it is the local machine, -h localhost -P 3306 can be omitted and written directly as mysql -u root -proot or mysql -uroot -proot
or
mysql -h 127.0.0.1 -P 3306 -u root -proot
or
mysql -h 192.168.117.66 -P 3306 -u root -proot (连接远程的主机,必须写-h 远程主机的ip)
If the mysql database port of the remote host is 3306 by default , the port number can be omitted, but the IP address of the remote host must be written
mysql -h 192.168.117.66 -u root -proot
If the mysql database port of the remote host is not the default 3306, the port is changed to For example, 6655, then the remote host IP address and port number must be written
mysql -h 192.168.117.66 -P 6655 -u root -proot
If it is the local machine, the host IP address and port number (the default is 3306) (Bottom) You can omit it.
mysql -u root -proot或者mysql -uroot -proot
If it is a local machine, you can omit the host IP address or just write the port number.
mysql -P 3306 -u root -proot
如果是本机的话,端口号可以省略不写,就写主机ip地址也可以
mysql -h localhost -u root -proot或者mysql -h 127.0.0.1 -u root -proot或者mysql -h 192.168.117.66 -u root -proot
.
如果是本机,但是端口你之前改成了其他的,比如端口你改成了8801,不是默认的3306端口了,那么主机ip地址可以省略不写,但是要写上端口号
mysql -P 8801 -u root -proot
参数顺序是没关系的,-h和-P放在后面也是可以的,如下
mysql -u root -proot -h 192.168.117.66 -P 3306
语法2:mysql -h主机ip地址 -P端口号 -u用户名 -p密码 (-h和主机ip地址之间无空格,-P和端口号之间无空格,-u和用户名之间无空格,-p和密码之间一定不能有空格)
mysql -h192.168.117.66 -P3306 -uroot -proot
语法3:mysql -h主机ip地址 -P端口号 -u用户名 -p (最后一个-p,小写字母p后面不写密码)
mysql -h 192.168.117.66 -P 3306 -u root -p或者mysql -h192.168.117.66 -P3306 -uroot -p
如下图:小写字母p后面不写密码,这样的话,密码就不会显示暴露出来了,输入密码的时候也是显示成****
如果我们使用小写字母p后面写密码的方式的话,密码就显示出来了,如下图:
警告你,密码显示出来不安全
mysql: [Warning] Using a password on the command line interface can be insecure
在命令行输入密码,就会提示这些安全警告信息
大家再来看下错误的写法是怎么样的
有的人写成mysql -h 192.168.117.66 -P 3306 -u root -p root
注意:小p和密码之间有个空格,这种写法是错误的,如下图:
有的人写成mysql -h 192.168.117.66 -P 3306 -u root
注意:少写-p(小p),这种写法也出错了,如下图:
大家根据具体的实际情况,灵活的使用mysql命令!
总之的总之,小p表示密码,小p和密码之间一定不能有空格,其他的参数-u、-h、-P(大写字母P)等可以有空格,也可以没有空格
如果大家怕会忘记小p和密码之间一定不能有空格这句话,那就全部统一写成所有的参数都不要加空格,这样写就不太容易出错了,统一写成mysql -h192.168.117.66 -P3306 -uroot -proot
就ok了,如下图:
退出登录,可以使用exit命令
注意:mysql这个关键字是mysql数据库中的命令,而不是windows操作系统中自带的dos命令,就像javac和java这2个关键字一样,javac和java是jdk中自带的命令,而不是windows操作系统中自带的dos命令。
还有一点要说一下,javac和java这2个命令之所以可以在dos窗口中的任何路径下执行,是因为在windows操作系统中配置了java、JDK的环境变量!
如果你们在执行mysql -h localhost -P 3306 -u root -proot
命令的时候,发现无法执行mysql命令,那你们可以去看一下windows中的环境变量,是否有配置mysql数据库的环境变量
我在安装mysql数据库的时候就自动配置好了mysql数据库的环境变量!如果你们没有配置mysql数据库的环境变量,你们自己去配一下。
Of course, you don’t have to. If not, if you want to use the mysql command, you must first switch to the bin directory under the installation path of the mysql database in the dos window, and then execute the mysql command, so If so, it would be more troublesome.
Every time, you must switch to the bin directory where the mysql database is installed before executing the mysql command. Therefore, it is recommended that friends configure the bin directory where the mysql database is installed into the environment variables in the operating system. Go, in this case, it will be very convenient to use the mysql command!
As shown below: This is the configuration when I installed the mysql database
You can also set the configuration of the mysql database when you install the mysql database. Double-click MySQLInstanceConfig.exe in the bin directory, as shown below:
Or you can directly configure the environment variables in Windows
Under normal circumstances, there is no problem
If there is a problem, you can put D:\Software\mysql5. Put the sentence 5\bin at the front and add a semicolon after it. It is actually similar to configuring Java environment variables, as shown below:
Configured After setting the environment variable of the bin directory of the mysql database, close the original dos window, reopen a dos window and enter the mysql -h localhost -P 3306 -u root -proot
command.
We want to log out of the mysql database, how to log out? Enter exit and press Enter
Related recommendations: " mysql tutorial》
The above is the detailed content of How to log in to mysql database. For more information, please follow other related articles on the PHP Chinese website!