1分鐘安裝
Part1:寫在最前
MySQL安裝的方式有三種:
①rpm包安裝
②二進制包安裝
③源碼安裝
這裏我們推薦二進制包安裝,無論從安裝速度還是用於生產庫安裝環境來說,都是沒問題的。現在生產庫一般採用MySQL5.6,測試庫採用MySQL5.7。
MySQL5.6安裝看這裏
http://suifu.blog.51cto.com/9167728/1846671
MySQL5.7安裝看這裏
http://suifu.blog.51cto.com/9167728/1855415
8分鐘數據庫操作
Part1:登錄
MySQL的登錄方式爲:
-u爲用戶名,-p爲密碼,如果您用了上述本文的安裝腳本,默認密碼爲MANAGER
[root@HE3 ~]# mysql -uroot -pMANAGER
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 11
Server version: 5.7.16-log MySQL Community Server (GPL)
Copyright (c) 2000, 2016, 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>
Part2:表基礎操作
①查看數據庫中有哪些庫
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| he1 |
| he3 |
| maxscale |
| mysql |
| performance_schema |
| sys |
+--------------------+
7 rows in set (0.00 sec)
②增刪改查
同Excel一樣,數據庫中也是要增刪改查的主要涉及的語法如下:
查:
首先選擇相應的庫
mysql> use maxscale
Database changed
select * from 表名;是查詢這張表所有內容的意思;
select 列名,列名 from 表名;是查詢這張表想看的列內容;
mysql> select a,b from helei;
+--------+------+
| a | b |
+--------+------+
| HE3 | a |
| 寫入 | b |
| 測試 | c |
| 於浩 | d |
| 賀磊 | e |
+--------+------+
6 rows in set (0.00 sec)
增:
insert into 表名 values('想插入的內容'); 往表中插入一條記錄的意思;
mysql> insert into helei values('插入','f');
Query OK, 1 row affected (0.01 sec)
mysql> select a,b from helei;
+--------+------+
| a | b |
+--------+------+
| HE3 | a |
| 寫入 | b |
| 測試 | c |
| 於浩 | d |
| 賀磊 | e |
| 插入 | f |
+--------+------+
6 rows in set (0.00 sec)
我這裏表名叫helei;
刪:
delete from helei where b='f';刪除helei表中b列是f的所有記錄;
mysql> delete from helei where b='f';
Query OK, 1 row affected (0.01 sec)
mysql> select * from helei;
+--------+------+
| a | b |
+--------+------+
| HE3 | a |
| 寫入 | b |
| 測試 | c |
| 於浩 | d |
| 賀磊 | e |
+--------+------+
5 rows in set (0.00 sec)
可以看到這裏b列爲f的整個一行就被刪除掉了。
改:
update 表名 set 列名='改成所需內容' where 限定條件。
mysql> update helei set b='改' where a='賀磊';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from helei;
+--------+------+
| a | b |
+--------+------+
| HE3 | a |
| 寫入 | b |
| 測試 | c |
| 於浩 | d |
| 賀磊 | 改 |
+--------+------+
5 rows in set (0.00 sec)
③表級操作
創建表
創建表t,這裏用生產庫的來做例子,id列自增主鍵,log爲varchar類型,可以存30個字符;
mysql> CREATE TABLE `t` (
-> `id` int UNSIGNED NOT NULL AUTO_INCREMENT ,
-> `log` varchar(30) NOT NULL DEFAULT '' ,
-> PRIMARY KEY (`id`)
-> )
-> ;
Query OK, 0 rows affected (0.01 sec)
刪除表
刪除表t,整表刪除;
mysql> drop table t;
Query OK, 0 rows affected (0.02 sec)
Part3:庫基礎操作
創建庫
mysql> CREATE DATABASE helei DEFAULT CHARACTER SET utf8 COLLATE utf8_bin;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| he1 |
| he3 |
| helei |
| maxscale |
| mysql |
| performance_schema |
| sys |
+--------------------+
8 rows in set (0.00 sec)
刪除庫
刪除名爲helei的庫,注意,這一操作會刪除掉helei庫中所有的表;
mysql> drop database helei;
Query OK, 0 rows affected (0.00 sec)
1分鐘系統級操作
Part1:啓停數據庫
[root@HE3 ~]# /etc/init.d/mysqld status
SUCCESS! MySQL running (3173)
[root@HE3 ~]# /etc/init.d/mysqld stop
Shutting down MySQL.... SUCCESS!
[root@HE3 ~]# /etc/init.d/mysqld start
Starting MySQL.. SUCCESS!
附錄
Part1:常用SQL
創建和授權用戶
CREATE USER 'helei'@'%' IDENTIFIED BY 'MANAGER';
GRANT SELECT,insert,update,delete ON *.* TO 'helei'@'%';
創建數據庫:
CREATE DATABASE www CHARACTER SET utf8 COLLATE utf8_bin;
密碼變更:
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('MANAGER');
統計哪些ip連接
mysql> select substring_index(host,':', 1) from information_schema.processlist;
統計每個IP連接數:
mysql> select substring_index(host,":", 1) ip, count(*) from information_schema.processlist group by ip;
到庫級別的ip連接數查看:
mysql> select db, substring_index(host,":", 1) ip, count(*) from information_schema.processlist group by db, ip;
查看當前連接數
mysql> show status like 'Threads%';
粗略統計每張表的大小
mysql> select table_schema,table_name,table_rows from tables order by table_rows desc;
要想知道每個數據庫的大小的話,步驟如下:
1、進入information_schema 數據庫(存放了其他的數據庫的信息)
use information_schema;
2、查詢所有數據的大小:
select concat(round(sum(data_length/1024/1024),2),'MB') as data from tables;
3、查看指定數據庫的大小:
比如查看數據庫home的大小
select concat(round(sum(data_length/1024/1024),2),'MB') as data from tables where table_schema='home';
4、查看指定數據庫的某個表的大小
比如查看數據庫home中 members 表的大小
select concat(round(sum(data_length/1024/1024),2),'MB') as data from tables where table_schema='home' and table_name='members';
無法更新或刪除數據。可以通過設置FOREIGN_KEY_CHECKS變量來避免這種情況。
SET FOREIGN_KEY_CHECKS = 0;
刪除完成後設置
SET FOREIGN_KEY_CHECKS = 1;
其他:
關閉唯一性校驗
set unique_checks=0;
set unique_checks=1;
變更字符集
ALTER TABLE tbl_name CONVERT TO CHARACTER SET utf8;
添加主鍵
alter table `helei` add column `id` int(10) not null auto_increment primary key comment '主鍵' first; 但會鎖表,先在測試庫中測試時間,如果時間長,嘗試利用pt工具
重命名錶
alter table helei rename to helei_old;
鎖表(用戶退出則失效)
flush tables with read lock;unlock table;
鎖某張表
lock tables helei read;
找出id是奇數和偶數
select * from t where id &1
select * from t where id=(id>>1)<<1
查看數據庫已運行時間
show global status like 'uptime';