MySQL 客户端工具使用

MySQL 客户端工具使用

1、MySQL程序组成

  • 客户端
    • mysql:CLI交互式客户端程序
    • mycli:CLI交互式客户端程序;使用sql语句时会有提示信息
    • mysql_secure_installation:安全初始化,强烈建议安装完以后执行此命令
    • mysqldump:mysql备份工具
    • mysqladmin:官方提供的shell命令行工具
  • 服务器端
    • mysqld

2、MySQL监听地址

服务器监听的两种socket地址:

socket类型说明
ip socket默认监听在tcp的3306端口,支持远程通信
unix sock监听在sock文件上(/tmp/mysql.sock,/var/lib/mysql/mysql.sock)仅支持本地通信

3、数据库配置文件

数据库配置文件为:/etc/my.cnf 和 /etc/my.cnf.d 目录下的配置文件。

//修改配置文件,配置字符编码
[root@localhost ~]# vim /etc/my.cnf
    [mysqld]
    character-set-server=utf8mb4
    collation-server=utf8mb4_general_ci

    [client]
    default-character-set=utf8mb4

    [mysql]
    default-character-set=utf8mb4

4、客户端工具使用

//语法:mysql [OPTIONS] [database]
//常用的OPTIONS:
    -uUSERNAME      //指定用户名,默认为root
    -hHOST          //指定服务器主机,默认为localhost,推荐使用ip地址
    -pPASSWORD      //指定用户的密码
    -P              //指定数据库监听的端口,如-P3307
    -S              //指定套接字文件位置,多实例部署MySQL时需要使用
    -V              //查看当前使用的mysql版本
    -e              //不登录mysql执行sql语句后退出,常用于脚本
    -e              //不登录mysql执行sql语句后退出,常用于脚本
    --defaults-file=配置文件	//指定MySQL配置文件位置,用于加载客户端配置

[root@localhost ~]# mysql -V
mysql  Ver 14.14 Distrib 5.7.23, for Linux (x86_64) using  EditLine wrapper

[root@localhost ~]# mysql -uroot -phzz123! -h127.0.0.1
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 4
Server version: 5.7.23 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

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> 

//注意,不推荐直接在命令行里直接用-pPASSWORD的方式登录,而是使用-p选项,然后交互式输入密码

[root@localhost ~]# mysql -uroot -p -h127.0.0.1
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.23 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

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> 

//不进入数据库执行sql语句
[root@localhost ~]# mysql -uroot -p -h 127.0.0.1 -e 'SHOW DATABASES;'
Enter password:
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| hzz                |
+--------------------+
# MySQL  

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×