haishi 发表于 2018-9-27 06:59:31

10分钟学会MySQL基础教程


10分钟学会MySQL基础操作
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
  # mysql -uroot -pMANAGER
  mysql: Using a password on the command line interface can be insecure.
  Welcome to the MySQL monitor.Commands end with ; or \g.

  Your MySQL connection>  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: 1Changed: 1Warnings: 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:启停数据库
  # /etc/init.d/mysqld status
  SUCCESS! MySQL running (3173)
  # /etc/init.d/mysqld stop
  Shutting down MySQL.... SUCCESS!
  # /etc/init.d/mysqld start
  Starting MySQL.. SUCCESS!
附录
  Part1:常用SQL
  创建和授权用户

  CREATE USER 'helei'@'%'>  GRANT SELECT,insert,update,delete ON *.* TO 'helei'@'%';
  创建数据库:
  CREATE DATABASEwww CHARACTER SET utf8 COLLATE utf8_bin;
  密码变更:
  SETPASSWORD FOR 'root'@'localhost' = PASSWORD('MANAGER');
  
  统计哪些ip连接
  mysql> selectsubstring_index(host,':', 1) from information_schema.processlist;
  统计每个IP连接数:
  mysql> selectsubstring_index(host,":", 1) ip, count(*)from information_schema.processlistgroup by ip;
  到库级别的ip连接数查看:
  mysql> select db,substring_index(host,":", 1) ip, count(*)from information_schema.processlistgroup by db, ip;
  查看当前连接数
mysql>show status like 'Threads%';
  粗略统计每张表的大小
  mysql> selecttable_schema,table_name,table_rows from tables order by table_rows desc;
  要想知道每个数据库的大小的话,步骤如下:
  1、进入information_schema 数据库(存放了其他的数据库的信息)
  useinformation_schema;
  2、查询所有数据的大小:
  selectconcat(round(sum(data_length/1024/1024),2),'MB') as data from tables;
  3、查看指定数据库的大小:
  比如查看数据库home的大小
  selectconcat(round(sum(data_length/1024/1024),2),'MB') as data from tables wheretable_schema='home';
  4、查看指定数据库的某个表的大小
  比如查看数据库home中 members 表的大小
  selectconcat(round(sum(data_length/1024/1024),2),'MB') as data from tables wheretable_schema='home' and table_name='members';
  
  无法更新或删除数据。可以通过设置FOREIGN_KEY_CHECKS变量来避免这种情况。
  SETFOREIGN_KEY_CHECKS = 0;
  删除完成后设置
  SETFOREIGN_KEY_CHECKS = 1;
  其他:
  关闭唯一性校验
  setunique_checks=0;
  setunique_checks=1;
  变更字符集
  ALTER TABLE tbl_name CONVERT TO CHARACTER SET utf8;
  添加主键
  alter table `helei` add column `id` int(10) not null auto_incrementprimary key comment '主键' first;   但会锁表,先在测试库中测试时间,如果时间长,尝试利用pt工具
  重命名表
altertable helei rename to helei_old;

  锁表(用户退出则失效)
  flushtables with read lock;unlocktable;
  锁某张表
  lock tableshelei read;
  找出id是奇数和偶数

  select * from t where>
  select * fromt where>  查看数据库已运行时间
showglobal status like 'uptime';
  
  ——总结——
  操作MySQL数据库是一项较为复杂的工作,限于文章篇幅原因,这里仅仅介绍冰山一角。由于笔者的水平有限,编写时间也很仓促,文中难免会出现一些错误或者不准确的地方,不妥之处恳请读者批评指正。

页: [1]
查看完整版本: 10分钟学会MySQL基础教程