MySQL 版本信息:
[root@db02 ~]# mysql --version mysql Ver 14.14 Distrib 5.6.36, for Linux (x86_64) using EditLine wrapper
Usage:
mysql [OPTIONS] [database]mysql
選項
-u, --user=name 指定用戶名 -p, --password[=pwd] 指定密碼 -h, --host=ip 指定服務器IP或者域名 -P, --port=3306 指定鏈接端口
默認爲鏈接本機(localhost)上的3306端口sql
[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:查看當前登陸的用戶信息 root@localhost數據庫
[root@db02 ~]# mysql -uroot -p123 mysql> select current_user(); +----------------+ | current_user() | +----------------+ | root@localhost | +----------------+ 1 row in set (0.03 sec)
指定鏈接哪臺服務器上的mysql,指定端口號服務器
[root@db02 ~]# mysql -uroot -p123 -hlocalhost -P3306 [root@db02 ~]# mysql -uroot -p123 -h10.0.0.52 -P3306查看當前登陸的用戶信息 root@10.0.0.%app
[root@db02 ~]# mysql -uroot -p123 -h10.0.0.52 -P3306 mysql> select current_user(); +----------------+ | current_user() | +----------------+ | root@10.0.0.% | +----------------+ 1 row in set (0.00 sec)
設定客戶端字符集 --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)加上參數鏈接 測試
[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)
執行選項-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 | +--------------------+多個sql語句中間用英文分號(;)隔開 優化
[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 | +-----------+------+
格式化選項
-E, --vertical 將輸出方式按照字段順序豎着顯示 -s, --silent 去掉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 localhostmysql -uroot -p123 -sspa
[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
錯誤處理選項
-f, --force 強制執行sql -v, --verbose 顯示更多信息 --show-warnings 顯示警告信息 這三個常常是一塊兒使用的.net
若是執行腳本有錯,可使用 -f 強制執行,而不是在遇到錯誤時直接終止code
sql測試文本 [root@db02 ~]# cat a.sql insert into stu values(1); insert into stu values(2aa); insert into stu values(3);遇到錯誤直接終止運行
[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 | +------+使用 -f 參數
[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 | +------+使用-f -v參數
[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) --------------使用-f -v --show-warnings參數
[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) --------------
注:此博文參考僅供參考
參考書籍:《深刻淺出MySQL 數據庫開發、優化與管理維護》(第二版)