Mysql入门与基础命令

1.env

最小化安装了rhel7.2

yum install -y net-tools vim

 

2.数据库

数据库(database):  保存有组织的数据的容器(通常是一个文件或一组文件)。

数据库管理系统(DBMS): 顾名思义,既然是数据管理系统,就是用来管理数据的数据库软件

 

文本文件方式:读取速度慢

数据库:通过索引,读取数据速度快

 

3.Mysql

MySQL 是采用客户/服务器模型的开放源码关系型 SQL 数据库管理系统,它可以在多种操作系统上运行,它是由 MySQL Ab 公司开发、发布并支持的,后被sun收购,现在在oracle公司旗下,现在有一个知名的分支MariaDB。”

 

 

# 数据库管理系统

# 客户/服务器模型:

c/s                 b/s

需要安装客户端软件 浏览器/服务器,多种操作系统一般都自带浏览器

 不方便                 方便

安全                不安全

 

# 开放源码

1996年Monty和David GPL协议 GNU <GNU is not Unix>

20080116 10 mysql5.1 sun公司收购

20101024 2000 74 oracle公司收购sun 5.5

 

mysql 5.5 5.6 5.7 (8.0)

mariadb 5.5 10.0 10.1 10.2

 

IOE革命

 

# 关系型

 

# SQL

结构化查询语言

 

# 多种操作系统

 

4.简单的安装

 

#服务器端 server

 

软件名software mariadb-server

服务名service mariadb

守护进程名daemon mysqld

配置文件configure /etc/my.cnf

数据文件data /var/lib/mysql

日志文件log /var/log/mariadb/mariadb.log

监听端口号port 3306

------------------------------------------------------------

#客户端client

软件名software mariadb

程序 mysql

mysqladmin

=======================================

命令的使用

mysql

1.服务启动后,mariadb5.5直接登陆,不需要密码

2.退出 \q exit ctrl+d

mysql -uroot -puplooking

-u用户名 空格有或无

-p密码   不能有空格

mysqladmin

1.无密码情况下添加密码

mysqladmin -uroot password 'uplooking'

 -u 用户名 可以有空格可无

 password 必须有空格 新密码

2.有密码情况下修改密码

mysqladmin -uroot -puplooking password redhat

课堂实验:

安装mariadb-server 5.5 启动服务,给本地root用户设置密码uplooking,并登录;将uplooking密码该为redhat,并登录。

-------------------------------------------------------------------

## 详细过程

[root@mastera0 ~]# yum install -y net-tools vim

[root@mastera0 ~]# yum install -y mariadb-server

[root@mastera0 ~]# systemctl start mariadb

[root@mastera0 ~]# ps -ef|grep mysqld

mysql    11774     1  0 11:54 ?        00:00:00 /bin/sh /usr/bin/mysqld_safe --basedir=/usr

mysql    11932 11774  0 11:54 ?        00:00:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock

root     11966  2367  0 11:55 pts/0    00:00:00 grep --color=auto mysqld

[root@mastera0 ~]# ps -ef|grep mariadb

mysql    11932 11774  0 11:54 ?        00:00:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock

root     11968  2367  0 11:55 pts/0    00:00:00 grep --color=auto mariadb

 

[root@mastera0 ~]# mysql

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 2

Server version: 5.5.44-MariaDB MariaDB Server

 

Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.

 

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

 

MariaDB [(none)]> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

| test               |

+--------------------+

4 rows in set (0.00 sec)

MariaDB [(none)]> \q

Bye

[root@mastera0 ~]# cd /var/lib/mysql

[root@mastera0 mysql]# ll

total 28700

-rw-rw----. 1 mysql mysql    16384 Oct 21 11:54 aria_log.00000001

-rw-rw----. 1 mysql mysql       52 Oct 21 11:54 aria_log_control

-rw-rw----. 1 mysql mysql 18874368 Oct 21 11:54 ibdata1

-rw-rw----. 1 mysql mysql  5242880 Oct 21 11:54 ib_logfile0

-rw-rw----. 1 mysql mysql  5242880 Oct 21 11:54 ib_logfile1

drwx------. 2 mysql mysql     4096 Oct 21 11:54 mysql

srwxrwxrwx. 1 mysql mysql        0 Oct 21 11:54 mysql.sock

drwx------. 2 mysql mysql     4096 Oct 21 11:54 performance_schema

drwx------. 2 mysql mysql        6 Oct 21 11:54 test

# 修改密码

[root@mastera0 ~]# mysqladmin -uroot password 'uplooking'

[root@mastera0 ~]# mysql

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

[root@mastera0 ~]# mysql -uroot -puplooking

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 8

Server version: 5.5.44-MariaDB MariaDB Server

 

Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.

 

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

 

MariaDB [(none)]> exit

Bye

[root@mastera0 ~]# mysqladmin -uroot -puplooking password redhat

[root@mastera0 ~]# mysql -uroot -predhat

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 11

Server version: 5.5.44-MariaDB MariaDB Server

 

Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.

 

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

 

MariaDB [(none)]> exit

Bye

 

 

# 如何破解mariadb-server5.5root密码

1)停止服务 systemctl stop mariadb

2)启动服务 mysqld_safe --skip-grant-tables &

3)修改密码 mysql进入数据库 update mysql.user set password=password('uplooking') where user='root';

4)停止服务 kill

5)启动服务 systemctl start mariadb

 

 

=======================================================================

 

5. SQL语句

DDL 数据库定义语言 create drop alter

DML 数据库操作语言 insert delete update

DCL 数据库控制语言 grant revoke

DQL 数据库查询语言 select

 

本章要求掌握sql语句的基本用法,包括create database,create table, drop database,drop table,insert into,update,delete from,grant,revoke; 其他sql语句作为拓展。

 

MySQL中的语法格式

1. 默认以;提交

2. 关键字大小写不敏感;库名、表名、数据都是区分大小写;

3. 使用空格

 

 

 

DQL 数据库查询语言 select

查询数据库 show databases;

查询表 show tables;

   show tables from test;

查看表的结构 desc mysql.user;

查看表中的数据 select host,user,password from mysql.user where user='root';

------------------------------------------------------

DDL 数据库定义语言 create drop alter

创建库 create database dbname;

删除库 drop database dbmame;

创建表 create table tbname (id int,name char(20),age int);

删除表 drop table tbname;

库名表名命名规则

英文字母、数字、下划线

以英文字母开头

<=32个字符

 

----------------------------------------------------------------

DML 数据库操作语言 insert delete update

插入一行 insert into dbname.tbname values (1,'superman',21);

insert into dbname.tbanme set id=1,name='superman',age=21;

插入多行  insert into dbname.tbname values (2,'batman',21),(),(),();

insert into dbname.tbname (id,age) values (2,23);

update update booboo.t1 set age=14 where id=2 or id=3;

delete delete from dbname.tbname where id=2;

------------------------------------------------------------------------

DCL 数据库控制语言 grant revoke

grant all on *.* to 'batman'@'172.25.0.12' identified by 'uplooking';

grant 关键字

all 所有权限除了grant之外

on *.*  所有的库所有的表

to user@host 权限是给谁的,从哪里来的

identified by 密码

flush privileges; 刷新授权表

 

mysql.user----->硬盘

启动服务之后,就会将mysql.user加载到内存中去。

 

revoke all on *.* from 'batman'@'172.25.0.12';

课堂练习:

mastera设置权限all,user1@'172.25.X.%' uplooking

  user1@'172.25.X.12' uplooking123

masterb 访问user1

授权越精确越优先

========================================================================================

6. 表的存储引擎

 

存储引擎  数据库读写数据的方式,决定了读写数据的快慢

 

事务 锁机制 适用场景

myisam  no 表锁 分析型

innodb  yes 行锁 金钱+线上高并发

 

 

 

事务(Transaction)是并发控制的基本单位。

 

所谓事务,它是一个操作序列,这些操作要么都执行,要么都不执行, 它是一个不可分割的工作单位。例如,银行转帐工作:从一个帐号扣款并 使另一个帐号增款,这两个操作要么都执行,要么都不执行。

 

事务的四大特性ACID

 

 

读锁

写锁

 

锁精度:表锁和行锁

*读

*不写

 

*不写

*不读

 

查看所有的存储引擎 show engines;

创建表指定存储引擎 create table tbname () engine=myisam;

查看表的属性 use booboo;show table status where name='t1';

 

 

=====================================================================================

7.如何获得MySQL相关资源

 

    为了学习更多的MySQL知识,请访问MySQL官网 http://dev.mysql.com/。

    为了下载服务器的一个副本,请访问MySQL官网 http://dev.mysql.com/downloads/。

 

8.MySQL在企业中的应用场景

 

MySQL 是世界上 ** 最受欢迎的开源数据库 **

 

MySQL 拥有庞大的用户群,国外的有 ** Facebook、Flickr、eBay ** 等,国内的有 ** 阿里、腾讯、新浪、百度等 ** 。而这些互联网和大部分传统公司的服务需要 ** 7×24 ** 小时连续工作。当此类型网站的部分数据库服务器宕机时,就需要高可用技术将流量牵引至备份主机,从而此时这些公司需要通过 ** 备份和恢复 ** 手段来产生备机,并通过 ** 复制 ** 来同步主备机间的状态,同时部署各种 ** 监控 ** 软件来监控服务器状态。当异常数据库服务器宕机时,通过手工或自动化手段将主机流量切换至备机,这个动作叫作 ** failover ** 。而一些大型公司在面对成千上万台 MySQL 服务器时,通常使用 ** 自动化运维脚本 ** 或程序完成上述种种动作。

 

 

=====================================================

总结:

1.什么是mysql?

2.sql语句的分类

3.mariadb-server5.5安装,修改密码,破解root密码

4.存储引擎

5.事务的四大特性ACID

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值