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';

转自    http://suifu.blog.51cto.com/9167728/1870989?zk

十分钟学会mysql数据库操作的更多相关文章

  1. PHP学习过程_Symfony_(3)_整理_十分钟学会Symfony

    这篇文章主要介绍了Symfony学习十分钟入门教程,详细介绍了Symfony的安装配置,项目初始化,建立Bundle,设计实体,添加约束,增删改查等基本操作技巧,需要的朋友可以参考下 (此文章已被多人 ...

  2. (转)十分钟了结MySQL information_schema

    十分钟了结MySQL information_schema  原文:http://www.cnblogs.com/shengdimaya/p/6920677.html information_sche ...

  3. Python/MySQL(四、MySQL数据库操作)

    Python/MySQL(四.MySQL数据库操作) 一.数据库条件语句: case when id>9 then ture else false 二.三元运算: if(isnull(xx)0, ...

  4. 【转载】一个小时学会MySQL数据库

    一个小时学会MySQL数据库   目录 一.数据库概要 1.1.发展历史 1.1.1.人工处理阶段 1.1.2.文件系统 1.1.3.数据库管理系统 1.2.常见数据库技术品牌.服务与架构 1.3.数 ...

  5. Python进行MySQL数据库操作

    最近开始玩Python,慢慢开始喜欢上它了,以前都是用shell来实现一些自动化或者监控的操作,现在用Python来实现,感觉更棒,Python是一门很强大的面向对象语言,所以作为一个运维DBA或者运 ...

  6. [指南] 15分钟学会MySQL(Linux版)

    原文链接:http://www.mysqlpub.com/thread-348-1-1.html 原创出处:MySQLpub.com  , 作者:kider  ,转载请注明作者和出处,并不能用于商业用 ...

  7. (转载)常用的Mysql数据库操作语句大全

    打开CMD,进入数据库命令:mysql -hlocalhost -uroot -p 退出数据库:exit 用户管理: 1.新建用户: >CREATE USER name IDENTIFIED B ...

  8. php : mysql数据库操作类演示

    设计目标: 1,该类一实例化,就可以自动连接上mysql数据库: 2,该类可以单独去设定要使用的连接编码(set names XXX) 3,该类可以单独去设定要使用的数据库(use XXX): 4,可 ...

  9. php MySQL数据库操作类源代码

    php MySQL数据库操作类源代码: <?php class MySQL{ private $host; //服务器地址 private $name; //登录账号 private $pwd; ...

随机推荐

  1. [LintCode] Intersection of Two Arrays 两个数组相交

    Given two arrays, write a function to compute their intersection.Notice Each element in the result m ...

  2. js变量

    由于undefined和null两个值的比较是相等的,所以,未初始化的变量和赋值为null的变量会相等.这时,可以采用typeof变量的类型进行比较.但,建议还是养成编码的规范,不要忘记初始化变量. ...

  3. webform连接ACCESS数据库

    1.先建立一个名叫mydb.accdb的access数据库 2.他它复制到webform中,放在App_Data文件夹下. 3.在App_Code文件夹下建好封装语句,查询方法,连接语句,其中stud ...

  4. 汇编基础知识之二debug的使用

    DEBUG的使用 (要在win32位习题下进行,win7 64位需要安装DosBox和debug这2个软件): 1:win64位下debug的使用教程: 下载debug.exe,这里我把debug放在 ...

  5. SPFA导读及介绍(转载)

    适用范围:给定的图存在负权边,这时类似Dijkstra等算法便没有了用武之地,而Bellman-Ford算法的复杂度又过高,SPFA算法便派上用场了. 我们约定有向加权图G不存在负权回路,即最短路径一 ...

  6. javascript遍历数组最优写法

    var arr = [];//这样定义的数组,是null,等待开辟空间 var arr = new Array();//不建议使用,会占用一块内存空间 var i=0,len=arr.length; ...

  7. Chrome浏览器插件推荐大全

    如何下载:http://www.cnplugins.com/devtool/ 提示:下载可能版本过旧,推荐搜索喜爱的插件之后前往官网或者github(编译的时候确保node和npm都是最新的版本).或 ...

  8. 一个链接直接打开APP

    http://www.cnblogs.com/jzm17173/p/4569574.html 这是IOS http://www.jianshu.com/p/af211f2a990e

  9. spidermark sensepostdata ntp_monlist.py

    试NTP 时间服务器用的,ntp_ip_enum.py,源码如下:#!/usr/bin/env python"""Basic script to pull address ...

  10. Bootstrap 固定定位(Affix)

    来自:慕课网 http://www.imooc.com/code/5396 Affix 效果常见的有以下三种: ☑ 顶部固定 ☑ 侧边栏固定 ☑ 底部固定 固定定位--声明式触发固定定位 Affix ...