阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

10分钟学会MySQL基础教程

116次阅读
没有评论

共计 4947 个字符,预计需要花费 13 分钟才能阅读完成。

10 分钟学会 MySQL 基础操作

1 分钟安装

Part1:写在最前

MySQL 安装的方式有三种:

rpm 包安装

二进制包安装

源码安装

这里我们推荐二进制包安装,无论从安装速度还是用于生产库安装环境来说,都是没问题的。现在生产库一般采用 MySQL5.6,测试库采用 MySQL5.7。

MySQL5.6 安装看这里 http://www.linuxidc.com/Linux/2016-09/135422.htm

MySQL5.7 安装看这里  http://www.linuxidc.com/Linux/2016-09/135423.htm

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 CONVERTTO CHARACTER SETutf8;

 

添加主键

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

 

——总结——

操作 MySQL 数据库是一项较为复杂的工作,限于文章篇幅原因,这里仅仅介绍冰山一角。由于笔者的水平有限,编写时间也很仓促,文中难免会出现一些错误或者不准确的地方,不妥之处恳请读者批评指正。

本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-11/136961.htm

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-22发表,共计4947字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中