MySQL入门02:关于MySQL连接的ABC

勤奋不是嘴上说说而已,而是实际的行动,在勤奋的苦度中持之以恒,永不退却。业精于勤,荒于嬉;行成于思,毁于随。在人生的仕途上,我们毫不迟疑地选择勤奋,她是几乎于世界上一切成就的催产婆。只要我们拥着勤奋去思考,拥着勤奋的手去耕耘,用抱勤奋的心去对待工作,浪迹红尘而坚韧不拔,那么,我们的生命就会绽放火花,让人生的时光更加的闪亮而精彩。

导读:本篇文章讲解 MySQL入门02:关于MySQL连接的ABC,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

连接命令

可以通过如下命令连接和断开MySQL服务器。

--连接远程的数据库
shell> mysql -h host -u user -p
--连接本地数据库
shell> mysql -u user -p

例:

-bash-4.1$ mysql -uroot -pMyNewPass4!
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 18
Server version: 8.0.23 MySQL Community Server - GPL

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

断开命令

可以通过输入exit或者quit(\q) 或者Control+D退出MySQL连接。

例:

mysql> exit
Bye

或者
mysql> quit
Bye

或者
mysql> \q
Bye


或者 Control+D
mysql> ^DBye

常见问题及解决

错误1:ERROR 2002 Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’(2)

连接MySQL可能会报如下错误:

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

这个可能是由于MySQL服务没有启动导致的,可以通过service或者systemctl(Linux7以后)启动服务。

例:

-bash-4.1$ sudo service mysqld stop
Stopping mysqld:                                           [  OK  ]

--连接报错
-bash-4.1$ mysql -uroot -pMyNewPass4!
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

--启动mysql
-bash-4.1$ sudo service mysqld start
Starting mysqld:                                           [  OK  ]

--正常连接
-bash-4.1$ mysql -uroot -pMyNewPass4!
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.23 MySQL Community Server - GPL

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

错误2:远程无法连接 ERROR 1130 (HY000): Host ’ ’ is not allowed to connect to this MySQL server

远程不能访问或者通过host名连接报错:

-bash-4.1$ mysql -h <hostname>  -uroot -pMyNewPass4!
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1130 (HY000): Host '<hostname>' is not allowed to connect to this MySQL server

这是由于MySQL初装后,默认是禁止远程连接数据库的。

解决方法:
更改 “mysql” 数据库里的 “user” 表里的 “host” 列值,从”localhost”改为”%”。

本地登录MySQL后,执行如下:

$ mysql -u root -p
mysql> show databases;
mysql> use mysql;
mysql> select host,user from user;
mysql> update user set host = '%' where user = 'root';
mysql> flush privileges; 或者重启MySQL

--成功登录
$ mysql -h <hostname> -uroot -p

例:

-bash-4.1$ mysql -u root -p -p
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select host,user from user;
+-----------+------------------+
| host      | user             |
+-----------+------------------+
| localhost | mysql.infoschema |
| localhost | mysql.session    |
| localhost | mysql.sys        |
| localhost | root             |
+-----------+------------------+
4 rows in set (0.01 sec)

由于默认的user表中的host是localhost,所以仅允许本地连接。

mysql> update user set host = '%' where user = 'root';
Query OK, 1 row affected (0.04 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select host,user from user;
+-----------+------------------+
| host      | user             |
+-----------+------------------+
| %         | root             |
| localhost | mysql.infoschema |
| localhost | mysql.session    |
| localhost | mysql.sys        |
+-----------+------------------+
4 rows in set (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

mysql> exit
Bye

--成功登录
-bash-4.1$ mysql -h <hostname> -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 22
Server version: 8.0.23 MySQL Community Server - GPL

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

错误3:ERROR 1251 (08004): Client does not support authentication protocol requested by server; consider upgrading MySQL client

远程连接MySQL报错。

-bash-4.1$ mysql -h <hostname> -uroot -p
Enter password:
ERROR 1251 (08004): Client does not support authentication protocol requested by server; consider upgrading MySQL client

解决方法:
升级客户端

其他常见连接错误

其他常见连接错误可以参考如下官方文档。
https://dev.mysql.com/doc/refman/8.0/en/common-errors.html

B.3.2 Common Errors When Using MySQL Programs
B.3.2.1 Access denied
B.3.2.2 Can’t connect to [local] MySQL server
B.3.2.3 Lost connection to MySQL server
B.3.2.4 Password Fails When Entered Interactively
B.3.2.5 Too many connections
B.3.2.6 Out of memory
B.3.2.7 MySQL server has gone away
B.3.2.8 Packet Too Large
B.3.2.9 Communication Errors and Aborted Connections
B.3.2.10 The table is full
B.3.2.11 Can’t create/write to file
B.3.2.12 Commands out of sync
B.3.2.13 Ignoring user
B.3.2.14 Table ‘tbl_name’ doesn’t exist
B.3.2.15 Can’t initialize character set
B.3.2.16 File Not Found and Similar Errors
B.3.2.17 Table-Corruption Issues

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/141600.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!