温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

快速掌握MySQL基础教程方法

发布时间:2020-05-15 14:59:44 来源:网络 阅读:194 作者:三月 栏目:数据库

本文主要给大家介绍快速掌握MySQL基础教程方法,文章内容都是笔者用心摘选和编辑的,具有一定的针对性,对大家的参考意义还是比较大的,下面跟笔者一起了解下快速掌握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

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

看完以上关于快速掌握MySQL基础教程方法,很多读者朋友肯定多少有一定的了解,如需获取更多的行业知识信息 ,可以持续关注我们的行业资讯栏目的。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI