MySQL client connection tool mysql

MySQL version information:

[root@db02 ~]# mysql --version
mysql  Ver 14.14 Distrib 5.6.36, for Linux (x86_64) using  EditLine wrapper

Usage:     

    mysql [OPTIONS] [database]

Options 

-u, --user=name specify username
-p, --password[=pwd] specify password
-h, --host=ip Specify the server IP or domain name
-P, --port=3306 Specify the connection port

The default is to connect to port 3306 on the local machine (localhost)

[root@db02 ~]# mysql -uroot -p123

[root@db02 ~]# mysql -uroot -p
Enter password: 

[root@db02 ~]# mysql --user=root --password=123

[root@db02 ~]# mysql --user=root --password
Enter password: 

View currently logged in user information root @localhost

[root@db02 ~]# mysql -uroot -p123

mysql> select current_user();
+----------------+
| current_user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.03 sec)

Specify which server to connect to mysql, specify the port number

[root@db02 ~]# mysql -uroot -p123 -hlocalhost -P3306

[root@db02 ~]# mysql -uroot -p123 -h10.0.0.52 -P3306

 View currently logged in user information [email protected].%

[root@db02 ~]# mysql -uroot -p123 -h10.0.0.52 -P3306

mysql> select current_user();
+----------------+
| current_user() |
+----------------+
| [email protected].%  |
+----------------+
1 row in set (0.00 sec)

Set client character set  --default-character-set=gbk

[root@db02 ~]# mysql -uroot -p123 

mysql> show variables like '%char%';
+--------------------------+-------------------------------------------+
| Variable_name            | Value                                     |
+--------------------------+-------------------------------------------+
| character_set_client     | utf8                                      |
| character_set_connection | utf8                                      |
| character_set_database   | utf8                                      |
| character_set_filesystem | binary                                    |
| character_set_results    | utf8                                      |
| character_set_server     | utf8                                      |
| character_set_system     | utf8                                      |
| character_sets_dir       | /application/mysql-5.6.36/share/charsets/ |
+--------------------------+-------------------------------------------+
8 rows in set (0.00 sec)

add parameter connection 

[root@db02 ~]# mysql -uroot -p123 --default-character-set=gbk

mysql> show variables like '%char%';
+--------------------------+-------------------------------------------+
| Variable_name            | Value                                     |
+--------------------------+-------------------------------------------+
| character_set_client     | gbk                                       |
| character_set_connection | gbk                                       |
| character_set_database   | utf8                                      |
| character_set_filesystem | binary                                    |
| character_set_results    | gbk                                       |
| character_set_server     | utf8                                      |
| character_set_system     | utf8                                      |
| character_sets_dir       | /application/mysql-5.6.36/share/charsets/ |
+--------------------------+-------------------------------------------+
8 rows in set (0.00 sec)

execute option -e

[root@db02 ~]# mysql -uroot -p123 -e 'show databases'
Warning: Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| world              |
+--------------------+

Separate multiple SQL statements with a semicolon (;) 

[root@db02 ~]# mysql -uroot -p123 -e 'show tables from mysql;select host,user from mysql.user;'
Warning: Using a password on the command line interface can be insecure.
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| event                     |
| func                      |
.............................
.............................
| user                      |
+---------------------------+
+-----------+------+
| host      | user |
+-----------+------+
| 10.0.0.%  | root |
| 127.0.0.1 | root |
| localhost | root |
+-----------+------+

formatting options

-E, --vertical Display the output method vertically in the order of the fields
-s, --silent Remove the line box display in mysql
[root@db02 ~]# mysql -uroot -p123 -e 'show databases' -E

*************************** 1. row ***************************
Database: information_schema
*************************** 2. row ***************************
Database: mysql
*************************** 3. row ***************************
Database: performance_schema
*************************** 4. row ***************************
Database: test
*************************** 5. row ***************************
Database: world
[root@db02 ~]# mysql -uroot -p123 -e 'show databases' -s

Database
information_schema
mysql
performance_schema
test
world
[root@db02 ~]# mysql -uroot -p123 -e 'select user,host from mysql.user' -s

user	host
root	10.0.0.%
root	127.0.0.1
root	localhost

mysql -uroot -p123 -s

[root@db02 ~]# mysql -uroot -p123 -s

mysql> select user,host from mysql.user;
user	host
root	10.0.0.%
root	127.0.0.1
root	localhost

error handling options

-f, --force enforce sql
-v, --verbose show more information
--show-warnings show warning message

These three are often used together

If there is an error executing the script, you can use -f to force execution instead of terminating on error

sql测试文本
[root@db02 ~]# cat a.sql 
insert into stu values(1);
insert into stu values(2aa);
insert into stu values(3);

Directly terminate the operation when encountering an error 

[root@db02 ~]# mysql -uroot -p123 test < a.sql 
ERROR 1054 (42S22) at line 2: Unknown column '2aa' in 'field list'

[root@db02 ~]# mysql -uroot -p123 test -e 'select * from stu'
Warning: Using a password on the command line interface can be insecure.
+------+
| id   |
+------+
|    1 |
+------+

Use the -f parameter

[root@db02 ~]# mysql -uroot -p123 test -f < a.sql 
ERROR 1054 (42S22) at line 2: Unknown column '2aa' in 'field list'

[root@db02 ~]# mysql -uroot -p123 test -e 'select * from stu'
Warning: Using a password on the command line interface can be insecure.
+------+
| id   |
+------+
|    1 |
|    1 |
|    3 |
+------+

Use -f -v parameters

[root@db02 ~]# mysql -uroot -p123 test -f -v < a.sql 
--------------
insert into stu values(1)
--------------
--------------
insert into stu values(2aa)
--------------
ERROR 1054 (42S22) at line 2: Unknown column '2aa' in 'field list'
--------------
insert into stu values(3)
--------------

Use the -f -v --show-warnings parameter

[root@db02 ~]# cat a.sql 
insert into stu values(1);
insert into stu values(222222222222222222222);
insert into stu values(3);

[root@db02 ~]# mysql -uroot -p123 test -f -v --show-warnings < a.sql 
--------------
insert into stu values(1)
--------------
--------------
insert into stu values(222222222222222222222)
--------------
ERROR 1264 (22003) at line 2: Out of range value for column 'id' at row 1
Error (Code 1264): Out of range value for column 'id' at row 1
Error (Code 1264): Out of range value for column 'id' at row 1
--------------
insert into stu values(3)
--------------

Note: This blog post reference is for reference only

Reference book: "In-depth MySQL Database Development, Optimization and Management and Maintenance" (Second Edition)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325138476&siteId=291194637