1.登陸成功以後退出的話,直接輸入quit或者exit便可。mysql
2.使用.tar.gz的包安裝MySQL時,進入MySQL須要使用絕對路徑sql
[root@winner ~]# /usr/local/mysql/bin/mysql -uroot
單獨只是輸入一個"mysql" 命令是不行的,由於"/usr/local/mysql/bin" 沒有在 PATH 這個環境變量裏(使用yum安裝不須要)。如何把它加入環境變量PATH中?shell
[root@winner ~]# PATH=$PATH:/usr/local/mysql/bin
這樣就能夠了,但重啓Linux後還會失效,因此須要讓它開機加載:數據庫
[root@winner ~]# echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile [root@winner ~]# source /etc/profile [root@localhost ~]# mysql -uroot
-u 用來指定要登陸的用戶,後邊能夠有空格,也能夠無空格。-p 後面能夠直接跟密碼,後面不能夠有空格,不過密碼最好用單引號括起來,不括也能夠,可是密碼中若是有特殊字符就會有問題了,因此最好是括起來。服務器
經過使用 mysql -u root -p
能夠鏈接數據庫,但這只是鏈接的本地的數據庫"localhost", 但是有不少時候都是去鏈接網絡中的某一個主機上的mysql。網絡
[root@winner ~]# mysql -uroot -p -h192.168.137.10 -P3306 Enter password:
其中後邊的 -P(大寫) 用來指定遠程主機MySQL的綁定端口,默認都是3306, -h 用來指定遠程主機的IP.ide
1. 查詢當前的庫ui
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | | winner | +--------------------+ rows in set (0.07 sec)
2. 查詢某個庫的表this
首先須要切換到某個庫中 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> show tables; +---------------------------+ | Tables_in_mysql | +---------------------------+ | columns_priv | | db | | event | | func | | general_log | | help_category | | help_keyword | | help_relation | | help_topic | | host | | ndb_binlog_index | | plugin | | proc | | procs_priv | | servers | | slow_log | | tables_priv | | time_zone | | time_zone_leap_second | | time_zone_name | | time_zone_transition | | time_zone_transition_type | | user | +---------------------------+ rows in set (0.00 sec)
3. 查看某個表的所有字段spa
mysql> desc host; +-----------------------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------------------+---------------+------+-----+---------+-------+ | Host | char(60) | NO | PRI | | | | Db | char(64) | 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 | | | Trigger_priv | enum('N','Y') | NO | | N | | +-----------------------+---------------+------+-----+---------+-------+ rows in set (0.00 sec)
也可使用另外一條命令,顯示比這個更詳細,並且能夠把建表語句所有列出來:
mysql> show create table host\G; *************************** 1. row *************************** Table: host Create Table: CREATE TABLE `host` ( `Host` char(60) COLLATE utf8_bin NOT NULL DEFAULT '', `Db` char(64) COLLATE utf8_bin NOT NULL DEFAULT '', `Select_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Insert_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Update_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Delete_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Create_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Drop_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Grant_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `References_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Index_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Alter_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Create_tmp_table_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Lock_tables_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Create_view_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Show_view_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Create_routine_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Alter_routine_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Execute_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Trigger_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', PRIMARY KEY (`Host`,`Db`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Host privileges; Merged with database privileges' row in set (0.00 sec)
4. 查看當前是哪一個用戶
mysql> select user(); +----------------+ | user() | +----------------+ | root@localhost | +----------------+ row in set (0.00 sec)
5. 查看當前所使用數據庫
mysql> select database(); +------------+ | database() | +------------+ | mysql | +------------+ row in set (0.00 sec)
6. 建立一個新庫
mysql> create database db1; Query OK, 1 row affected (0.02 sec)
7. 建立一個新表
mysql> create table t1 (`id` int(4), `name` char(40)); Query OK, 0 rows affected (0.02 sec)
8. 查看當前數據庫版本
mysql> select version(); +-----------+ | version() | +-----------+ | 5.1.73 | +-----------+ row in set (0.00 sec)
9. 查看當前MySQL狀態
mysql> show status; +-----------------------------------+----------+ | Variable_name | Value | +-----------------------------------+----------+ | Aborted_clients | 1 | | Aborted_connects | 2 | | Binlog_cache_disk_use | 0 | | Binlog_cache_use | 0 | | Bytes_received | 1194 | | Bytes_sent | 21732 | | Com_admin_commands | 0 | | Com_assign_to_keycache | 0 | | Com_alter_db | 0 | | Com_alter_db_upgrade | 0 | | Com_alter_event | 0 |
10. 查看MySQL的參數
mysql> show variables; +-----------------------------------------+---------------------+ | Variable_name | Value | +-----------------------------------------+---------------------+ | auto_increment_increment | 1 | | auto_increment_offset | 1 | | autocommit | ON | | automatic_sp_privileges | ON | | back_log | 50 | | basedir | /usr/local/mysql/ |
11. 修改MySQL的參數
mysql> show variables like 'max_connect%'; +--------------------+-------+ | Variable_name | Value | +--------------------+-------+ | max_connect_errors | 10 | | max_connections | 151 | +--------------------+-------+ rows in set (0.00 sec) mysql> set global max_connect_errors = 1000; Query OK, 0 rows affected (0.00 sec) mysql> show variables like 'max_connect_errors'; +--------------------+-------+ | Variable_name | Value | +--------------------+-------+ | max_connect_errors | 1000 | +--------------------+-------+ row in set (0.00 sec)
在MySQL命令行, "%"相似於shell下的 *
, 表示萬能匹配。使用 "set global" 能夠臨時修改某些參數,可是重啓mysqld服務後還會變爲原來的,因此要想恆久生效,須要在配置文件 my.cnf 中定義。
12. 查看當前MySQL服務器的隊列
這個在平常的管理工做中使用最爲頻繁,由於使用它能夠查看當前mysql在幹什麼,能夠發現是否有鎖表:
mysql> show processlist; +----+------+-----------+------+---------+------+-------+------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+-----------+------+---------+------+-------+------------------+ | 9 | root | localhost | db1 | Query | 0 | NULL | show processlist | +----+------+-----------+------+---------+------+-------+------------------+ row in set (0.00 sec)
13. 建立一個普通用戶並受權
mysql> grant all on *.* to user1 identified by '123456'; Query OK, 0 rows affected (0.01 sec)
all 表示全部的權限(讀、寫、查詢、刪除等等操做), *.*
前面的 *
表示全部的數據庫,後面的 *
表示全部的表,identified by 後面跟密碼,用單引號括起來。這裏的user1指的是localhost上的user1,若是是給網絡上的其餘機器上的某個用戶受權則這樣:
mysql> grant all on db1.* to 'user2'@'10.0.2.100' identified by '111222'; Query OK, 0 rows affected (0.01 sec)
用戶和主機的IP之間有一個@,另外主機IP那裏能夠用%替代,表示全部主機,例如:
mysql> grant all on db1.* to 'user3'@'%' identified by '231222'; Query OK, 0 rows affected (0.00 sec)
備份:
[root@winner ~]# mysqldump -uroot -p'root' mysql >/tmp/mysql.sql 不是在mysql>環境
使用 mysqldump 命令備份數據庫,-u 和 -p 兩個選項使用方法和前面說的 mysql 一樣,然後面的 「mysql」 指的是庫名,而後重定向到一個文本文檔裏。備份完後,你能夠查看 /tmp/mysql.sql 這個文件裏的內容。
恢復和備份正好相反:
[root@localhost ~]# mysql -uroot -p'yourpassword' mysql </tmp/mysql.sql