MySQL涔嬪垵浣跨敤
Posted GrowthDBA
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL涔嬪垵浣跨敤相关的知识,希望对你有一定的参考价值。
浣跨敤瀹㈡埛绔▼搴弇ysql杩炴帴mysql
mysql -h[涓绘満鍚峕 -u[鐢ㄦ埛鍚峕 -p[瀵嗙爜]
闄や簡涓婅堪鐨勫弬鏁伴€夐」澶栵紝杩樻湁寰堝锛屽垪涓句竴浜涘父瑙侀€夐」骞惰祴鍚箟锛?/span>
鍙傛暟锛堢煭褰㈠紡锛?/span> | 鍚箟 |
---|---|
-h | 涓绘満鍚嶏紝琛ㄧず鍚姩MySQL鏈嶅姟绋嬪簭鐨勬湇鍔″櫒鍩熷悕鎴朓P鍦板潃锛屽鏋滄湇鍔$▼搴忓湪鏈満锛屽彲浠ョ渷鐣ユ寚瀹氾紝涔熷彲浠ュ~localhost鎴栬€?/span>127.0.0.1銆傚彲浠ュ啓浣滈暱褰㈠紡 --host=[涓绘満]銆?/span> |
-u | 鐢ㄦ埛鍚嶏紝鍙互鍐欎綔闀垮舰寮?nbsp;--user=[鐢ㄦ埛鍚峕銆?/span> |
-p鈥?/span>鈥?/span> | 瀵嗙爜锛屽彲浠ュ啓浣滈暱褰㈠紡 --password=[瀵嗙爜]銆?/span> |
-P鈥?/span>鈥?/span> | 绔彛锛屽彲浠ュ啓浣滈暱褰㈠紡 --port=[绔彛]銆?/span> |
-D鈥?/span>鈥?/span> | 搴撳悕锛屽彲浠ュ啓浣滈暱褰㈠紡 --database=[搴撳悕]銆?/span> |
鍙互浣跨敤涓嬮潰鐨勫懡浠ゅ惎鍔∕ySQL瀹㈡埛绔紝骞惰繛鎺ュ埌鏈嶅姟鍣細
mysql -hlocalhost -uroot -proot -P3306
# 鎴栬€?/span>
mysql --host=localhost --user=root --password=root --port=3306
杩炴帴鎴愬姛鍚庣殑鐣岄潰锛?/span>
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 12
Server version: 8.0.21 Source distribution
Copyright (c) 2000, 2020, 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.
[root@localhost][(none)]>
猸曪笍 灏忔彁绀?1
涓婅堪鐧诲綍鎴愬姛鐨勭晫闈腑鍑虹幇浜嗕竴鍙ヨ鍛婏細
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql -hlocalhost -uroot -p
# 鐐瑰嚮鍥炶溅涔嬪悗鎵嶄細鎻愮ず浣犺緭鍏ュ瘑鐮?/span>
Enter password:
# 杩欑鏂瑰紡浣犺緭鍏ョ殑瀵嗙爜涓嶄細琚樉绀哄嚭鏉ワ紝瀹夊叏寰椾互淇濊瘉
鍚屾椂锛岃繕鏄缓璁娇鐢ㄥ鏉傚瘑鐮侊紝鏇村姞瀹夊叏銆?/span>
猸曪笍 灏忔彁绀?2
濡傛灉浣犻潪瑕佸湪鏄惧紡鐨勬妸瀵嗙爜杈撳嚭鏉ワ紝閭e瘑鐮佸€煎拰-p涔嬮棿涓嶈兘鏈夌┖鐧藉瓧绗︼紝鍏朵粬鍙傛暟鍚嶄箣闂村彲浠ユ湁绌虹櫧瀛楃锛?/span>
mysql -h localhost -u root -proot
mysql -h localhost -u root -p root
猸曪笍 灏忔彁绀?3
mysql -p -u root -h localhost
猸曪笍 灏忔彁绀?4
鏈嶅姟鍣ㄥ拰瀹㈡埛绔畨瑁呭湪鍚屼竴鍙版満鍣ㄤ笂锛?span class="mq-163">-h鍙傛暟鍙互鐪佺暐锛?/span>
mysql -u root -p
猸曪笍 灏忔彁绀?nbsp;5
mysql -u admin -p
# 绛夊悓浜?/span>
mysql -p
鎵ц涓€浜涚畝鍗昐QL鍙婃敞鎰忎簨椤?/p>
缁忚繃婕暱鐨勮繛鎺ュ拰灏忔彁绀猴紝缁堜簬鍒颁簡婵€鍔ㄤ汉蹇冪殑鎵цSQL璇彞鐜妭锛屽啀鍥為【涓€涓嬩竵濂囧ぇ浣殑MySQL閫昏緫鏋舵瀯鍥撅細
-
鍛戒护缁撴潫绗︼細
;
\g
\G
-
鏌ヨ褰撳墠鐧诲綍鐢ㄦ埛鐨勫懡浠わ細
[root@localhost][(none)]> SELECT USER();
+----------------+
| USER() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
[root@localhost][(none)]> SELECT USER()\g
+----------------+
| USER() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
[root@localhost][(none)]> SELECT USER()\G
*************************** 1. row ***************************
USER(): root@localhost
1 row in set (0.00 sec)
SELECT鎰忓懗鐫€杩欐槸涓€涓煡璇㈠懡浠わ紝USER()鏄疢ySQL鍐呯疆鐨勫嚱鏁帮紝鐢ㄤ簬杩斿洖褰撳墠鐧诲綍鐢ㄦ埛锛岀粨鏋滀腑1 row in set (0.00 sec)鐨勬剰鎬濇槸缁撴灉鍙湁1琛屾暟鎹紝鐢ㄦ椂0.00绉掋€傚叾涓儚USER()杩欐牱鐨勫嚱鏁拌繕鏈夊緢澶氾紝姣斿DATABASE()杩斿洖褰撳墠鎵€鎿嶄綔鐨勫簱鍚嶃€丯OW()杩斿洖褰撳墠鏃堕棿銆乂ERSION()杩斿洖褰撳墠MySQL鐗堟湰銆?span>锛?span class="mq-249">\G鏈変竴鐐圭壒娈婏紝瀹冨苟涓嶄互琛ㄦ牸鐨勫舰寮忚繑鍥炴煡璇㈢粨鏋滐紝鑰屾槸浠?span class="mq-250">鍨傜洿鐨勫舰寮忓皢姣忎釜鍒楅兘灞曠ず鍦ㄥ崟鐙殑涓€琛屼腑锛?/strong>
-
鍛戒护娌℃湁缁撴潫涔嬪墠锛屽彲浠ラ殢鎰忔崲琛岋細
[root@localhost][(none)]> SELECT
-> NOW()
-> ;
+---------------------+
| NOW() |
+---------------------+
| 2021-08-11 16:38:55 |
+---------------------+
1 row in set (0.00 sec)
-
鍙互涓€娆℃彁浜ゅ涓懡浠わ細
[root@localhost][(none)]> SELECT NOW(); SELECT USER(); SELECT DATABASE();
+---------------------+
| NOW() |
+---------------------+
| 2021-08-11 16:39:50 |
+---------------------+
1 row in set (0.00 sec)
+----------------+
| USER() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
+------------+
| DATABASE() |
+------------+
| NULL |
+------------+
1 row in set (0.00 sec)
-
浣跨敤 \c 鏀惧純鏈鎿嶄綔锛?/span>
[root@localhost][(none)]> SELECT NOW()\c
[root@localhost][(none)]>
MySQL榛樿瀵瑰懡浠ょ殑澶у皬鍐欏苟娌℃湁闄愬埗锛岃繖鏍锋煡璇篃鍙互锛?/span>
[root@localhost][(none)]> select database();
+------------+
| database() |
+------------+
| NULL |
+------------+
1 row in set (0.00 sec)
-
瀛楃涓茬殑琛ㄧず銆傚湪鍛戒护閲屾湁鏃朵細浣跨敤鍒板瓧绗︿覆锛屾垜浠彲浠ヤ娇鐢ㄥ崟寮曞彿''鎴栬€呭弻寮曞彿""鎶婂瓧绗︿覆鍐呭寮曡捣鏉?/span>
[root@localhost][(none)]> SELECT 'mysql';
+-------+
| mysql |
+-------+
| mysql |
+-------+
1 row in set (0.00 sec)
[root@localhost][(none)]> SELECT "mysql";
+-------+
| mysql |
+-------+
| mysql |
+-------+
1 row in set (0.00 sec)
杩欎釜璇彞鍙槸绠€鍗曠殑鎶婂瓧绗︿覆'mysql'鍙堣緭鍑烘潵浜嗚€屽凡銆備絾涓€瀹氳鍦ㄥ瓧绗︿覆鍐呭涓婂姞涓婂紩鍙凤紝涓嶇劧鐨勮瘽MySQL鏈嶅姟鍣ㄤ細鎶婂畠褰撲綔鍒楀悕锛屾瘮濡傝繖鏍峰氨浼氳繑鍥炰竴涓敊璇細
[root@localhost][(none)]> SELECT mysql;
ERROR 1054 (42S22): Unknown column 'mysql' in 'field list'
[root@localhost][(none)]>
-
\s 鑾峰彇MySQL鏈嶅姟鍣ㄧ殑鐘舵€佷俊鎭細
-
\!鍦∕ySQL鍛戒护琛屼腑鎵цLinux鎿嶄綔绯荤粺鍛戒护锛?/span>
[root@localhost][(none)]> \! pwd
/mysql/mysql3306
[root@localhost][(none)]>
鏂紑瀹㈡埛绔笌鏈嶅姟鍣ㄧ殑杩炴帴骞朵笖鍏抽棴瀹㈡埛绔殑璇濓紝鍙互浣跨敤涓嬮潰鐨勫懡浠わ細
[root@localhost][(none)]> exit
Bye
[root@localhost][(none)]> quit
Bye
[root@localhost][(none)]> \q
Bye
SHOW ENGINES;鏌ョ湅MySQL鏀寔鐨勫瓨鍌ㄥ紩鎿庯細
[root@localhost][(none)]> SHOW ENGINES;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| FEDERATED | YES | Federated MySQL storage engine | NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| CSV | YES | CSV storage engine | NO | NO | NO |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.00 sec)
-
SHOW 鍛戒护鍚庨潰鍙互 璺熷緢澶氶€夐」 锛屾潵鏄剧ず涓嶅悓鐨勫姛鑳斤紝濡?/span> PLUGINS 鑾峰彇褰撳墠鎵€鏈夋椿璺冪殑鎻掍欢銆?/span> VARIABLES 鑾峰彇MySQL鏈嶅姟鍣ㄦ墍鏈夌殑鍙傛暟銆?/span> STATUS 鑾峰彇MySQL鏈嶅姟鍣ㄧ姸鎬佷俊鎭€?/span> ERRORS 鑾峰彇閿欒淇℃伅銆?/span> WARNINGS 鑾峰彇璀﹀憡淇℃伅銆?/span> GRANTS 鑾峰彇褰撳墠鐢ㄦ埛鐨勬巿鏉冧俊鎭瓑锛屽悓鏃讹紝 VARIABLES銆丼TATUS 鍙互閫氳繃 LIKE 鍏抽敭瀛楁潵鍖归厤鎯宠鐨勪釜鍒弬鏁帮細
[root@localhost][(none)]> SHOW STATUS LIKE 'Threads_running';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| Threads_running | 2 |
+-----------------+-------+
1 row in set (0.00 sec)
[root@localhost][(none)]> SHOW VARIABLES LIKE 'sql_mode';
+---------------+--------------------------------------------+
| Variable_name | Value |
+---------------+--------------------------------------------+
| sql_mode | STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION |
+---------------+--------------------------------------------+
1 row in set (0.01 sec)
猸曪笍 灏忔彁绀?/span>
鍦∕ySQL瀹㈡埛绔墽琛岃繃鐨勫懡浠わ紝浼氬湪Linux鎿嶄綔绯荤粺涓婃湁鎵€淇濆瓨锛屽瓨鍌ㄥ湪褰撳墠Linux鐢ㄦ埛鐨勫鐩綍涓嬬殑.mysql_history闅愯棌鏂囦欢閲岋細
[172.xx.x.xxx:root@sz-pg-backup-zookeeper-003:/root]# cd ~
[172.xx.x.xxx:root@sz-pg-backup-zookeeper-003:/root]# tail -n 8 .mysql_history
SELECT\040'mysql';
SELECT\040mysql;
\134s
\134!\040pwd
SHOW\040ENGINES;
SHOW\040STATUS\040LIKE\040'Threads_running';
SHOW\040VARIABLES\040LIKE\040'sql_mode';
exit
[172.xx.x.xxx:root@sz-pg-backup-zookeeper-003:/root]#
鏌ョ湅鏁版嵁搴撳拰琛?span class="mq-610">
-
鑾峰彇褰撳墠MySQL瀹炰緥涓墍鏈夌殑鏁版嵁搴擄細
[root@localhost][(none)]> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test_clone |
+--------------------+
5 rows in set (0.00 sec)
閫夋嫨瑕佹搷浣滅殑鏁版嵁搴擄紙褰撴垜浠鎿嶄綔鏌愪竴涓暟鎹簱鏃讹紝闇€瑕佸厛鍒囨崲鍒板搴旂殑搴撲笅锛夛細
[root@localhost][(none)]> USE mysql;
Database changed
[root@localhost][mysql]> SELECT DATABASE();
+------------+
| DATABASE() |
+------------+
| mysql |
+------------+
1 row in set (0.00 sec)
鏌ョ湅鍒涘缓鏁版嵁搴撶殑璇彞锛?/span>
[root@localhost][mysql]> SHOW CREATE DATABASE mysql;
+----------+---------------------------------------------------------------------------------------------------------------------------------+
| Database | Create Database |
+----------+---------------------------------------------------------------------------------------------------------------------------------+
| mysql | CREATE DATABASE `mysql` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci */ /*!80016 DEFAULT ENCRYPTION='N' */ |
+----------+---------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
鏌ョ湅鏌愪竴涓暟鎹簱鍐呯殑鎵€鏈夎〃鐨勫垪琛細
[root@localhost][mysql]> SHOW TABLES;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| component |
| db |
| default_roles |
| engine_cost |
| func |
| general_log |
| global_grants |
| gtid_executed |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| innodb_index_stats |
| innodb_table_stats |
| password_history |
| plugin |
| procs_priv |
| proxies_priv |
| role_edges |
| server_cost |
| servers |
| slave_master_info |
| slave_relay_log_info |
| slave_worker_info |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
33 rows in set (0.00 sec)
鏌ョ湅鏌愬紶琛ㄧ殑鎵€鏈夊瓧娈典俊鎭細
[root@localhost][mysql]> SHOW COLUMNS FROM db;
+-----------------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------------+---------------+------+-----+---------+-------+
| Host | char(255) | NO | PRI | | |
| Db | char(64) | NO | PRI | | |
| User | char(32) | NO | PRI | | |
| Select_priv | enum('N','Y') | NO | | N | |
| Insert_priv | enum('N','Y') | NO | | N | |
| Update_priv | enum('N','Y') | NO | | N | |
| Delete_priv | enum('N','Y') | NO | | N | |
| Create_priv | enum('N','Y') | NO | | N | |
| Drop_priv | enum('N','Y') | NO | | N | |
| Grant_priv | enum('N','Y') | NO | | N | |
| References_priv | enum('N','Y') | NO | | N | |
| Index_priv | enum('N','Y') | NO | | N | |
| Alter_priv | enum('N','Y') | NO | | N | |
| Create_tmp_table_priv | enum('N','Y') | NO | | N | |
| Lock_tables_priv | enum('N','Y') | NO | | N | |
| Create_view_priv | enum('N','Y') | NO | | N | |
| Show_view_priv | enum('N','Y') | NO | | N | |
| Create_routine_priv | enum('N','Y') | NO | | N | |
| Alter_routine_priv | enum('N','Y') | NO | | N | |
| Execute_priv | enum('N','Y') | NO | | N | |
| Event_priv | enum('N','Y') | NO | | N | |
| Trigger_priv | enum('N','Y') | NO | | N | |
+-----------------------+---------------+------+-----+---------+-------+
22 rows in set (0.01 sec)
猸曪笍 灏忔彁绀?/span>
灏忕粨
鏈珷浠嬬粛浜嗗浣曡繛鎺ュ拰鐧诲綍MySQL锛屽浣曚娇鐢ㄧ畝鍗曠殑SQL鍛戒护鏉ヨ幏鍙栨暟鎹簱鐨勪竴浜涗俊鎭紝濡備綍鐢║SE閫夋嫨鏁版嵁搴擄紝濡備綍鐢⊿HOW鏌ョ湅MySQL鏁版嵁搴撱€佽〃鍜屽唴閮ㄤ俊鎭瓑銆傛湰绔犺緝涓哄熀纭€锛屼负鍚庨潰鐨勬繁鍏ュ涔犲瀹氫簡鍩虹銆?/span>
end
以上是关于MySQL涔嬪垵浣跨敤的主要内容,如果未能解决你的问题,请参考以下文章