zoukankan      html  css  js  c++  java
  • MySQL产品简介及连接操作&SQL语句及客户端工具

    MySQL产品简介及连接操作

    MySQLs是一个真正的多线程,多用户的SQL数据库服务软件,凭借查询速度快,好性能,高可靠和易于使用等特性,成为服务领域中最受欢迎的开源关系型数据库系统。在2008年之前,MySQL醒目由MySQL AB公司进行开发,发布与支持,SUN在2008年以10亿美元收购了MySQL。4月,Oracle甲骨文宣布将以74亿美元并购SUN。目前MySQL项目有Oracle公司负责运营和维护。

    MySQL与MariaDB:
    MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可。开发这个分支的原因之一是:甲骨文公司收购了MySQL后,有将MySQL闭源的潜在风险,因此社区采用分支的方式来避开这个风险。MariaDB的目的是完全兼容MySQL,包括API和命令行,使之能轻松成为MySQL的代替品。

    MariaDB由MySQL的创始人麦克尔·维德纽斯主导开发,他早前曾以10亿美元的价格,将自己创建的公司MySQL卖给了SUN,此后,随着SUN被甲骨文收购,MySQL的所有权也落入Oracle的手中。MariaDB名称来自麦克尔·维德纽斯的女儿玛丽亚(英语:Maria)的名字。MariaDB直到5.5版本,均依照MySQL的版本。因此,使用MariaDB5.5的人会从MySQL5.5中了解到MariaDB的所有功能。从2012年11月12日起发布的10.0.0版开始,不再依照MySQL的版号。10.0.x版以5.5版为基础,加上移植自MySQL5.6版的功能和自行开发的新功能。CentOS7系列Linux操作系统中采用MariaDB替代了MySQL数据库产品。

    关系型数据库管理系统RDBMS:

    商业:Oracle , Sybase, Infomix, SQL Server, DB2
    开源:MySQL PostgreSQL , pgsql,EnterpriseDB

    银行软件:
    操作系统:AIX(IBM-->Unix小机)RedHat

    数据库:Oracle DB2
    中间件:Weblogic Websphere Jboss

    非关系型数据库 NoSQL:
    MongoDB, Redis, HBase ,Memcached
    MySQL版本:
    Community Edtion 社区版,免费,由社区人员维护,测试及更新

    Enterprise Edtion 企业版,收费,MySQL官方维护团队人员维护,测试及更新

    1、安装MySQL

    (1)二进制安装MySQL

    (2)用rpm安装mariadb步骤:

    yum -y install mariadb mariadb-server

    systemctl start mariadb

    它的端口是3306

    dhcp:服务端67,客户端68

    ftp:20,21还有端口范围

    dns:TCP/UDP53,9

    http:80

    https:43

    ssh:22

    telnet:23

    tomcat:8080,8005,8009

    mysql:3306

    redis:63,79

    mamached:11211

    Windows远程控制:3389

    MySQL默认通过TCP:3306端口提供服务,可通过编辑/etc/my.cnf文件中"port=3306"行进行修改。

    MySQL 数据库系统也是典型的C/S(客户端/服务器)架构的应用,连接时需要专用的客户端工具,Linux下通过mysql命令工具(如果是通过rpm格式安装软件需要安装mysql软件包)。

    连接并登录到MySQL操作环境

    mysql
    -u 指定用户名
    -p 指定密码(选项和密码之间不能有空格)
    -h 指定主机
    -P 指定端口
    -S 指定Socket文件
    -e 指定SQL命令语句(非交互模式)

    [root@localhost ~]# mysql -u root -p123456;history -c 默认root为Mysql管理用户

    MySQL语句及客户端工具:

    SQL语句分类:
    1、数据定义语言(DDL)
    创建、修改或删除数据库中各种对象,包括表、视图、索引等。

    命令 :CREATE TABLE , CREATE VIEW, CREATE INDEX、ALTER TABLE ,DROP TABLE , DROP VIEW, DROP INDEX
    2、查询语言(DQL)

    按照指定的组合、条件表达式或排序检索已存在的数据库中数据,不改变数据库中数据。

    命令:SELECT...FROM...WHERE...
    3、数据操纵语言(DML)
    对已经存在的数据库进行元组的插入、删除、修改等操作

    命令:INSERT、UPDATE、 DELETE

    4、数据控制语言(DCL)
    用来授予或收回访问数据库的某种特权、

    控制数据操纵事务的发生时间及效果、对数据库进行监视
    命令:GRANT、REVOKE、COMMIT、ROLLBACK

    设置数据库用户的初始密码(刚开始没有密码,给它设置一个密码):
    [root@localhost ~]# mysqladmin -u root password '123456

    后期修改数据库用户的密码:
    [root@localhost~]# mysqladmin -u root-p旧的密码 password'新的密码'

    [root@localhost~]# mysqladmin -u root -p'TvC:Rm1ZlxtG' password '123456'

    MySQL是一套关系型数据库管理系统,在每台MySQL服务器中,均支持运行多个数据库(文件系统中的目录)(分类,按照产品划分),每个库相当于一个容器,其中存放着许多数据表,表中的每一行包含一条具体的数据关系信息,这些信息被称为数据记录。

    数据库的基本概念
    数据
    描述事物的符号记录称为数据(Data),包括数字,文字、图形、图像、声音、档案记录等以“记录”形式按统一的格式进行存储。

    数据表
    将不同的记录组织在一起,就形成了“表”,是用来存储具体数据的

    数据库
    数据库就是表的集合,是存储数据表的仓库,以一定的组织方式存储的相互有关的数据

    查询当前服务器中有哪些库:

    MariaDB [(none)]> show databases;
    +--------------------+
    | Database |
    +--------------------+
    | information_schema |
    | auth |
    | mysql |
    | performance_schema |
    | test |
    +--------------------+
    5 rows in set (0.00 sec)

    产看数据库中的数据表信息:

    MariaDB [(none)]> use mysql

    Database changed

    查看有哪些表:

    MariaDB [mysql]> show tables;
    +---------------------------+
    | Tables_in_mysql |
    +---------------------------+
    | columns_priv |
    | db |
    | event |
    | func |
    | general_log |
    | help_category |
    | help_keyword |
    | help_relation |
    | help_topic |
    | host |
    | ndb_binlog_index |
    | plugin |
    | proc |
    | procs_priv |
    | proxies_priv |
    | servers |
    | slow_log |
    | tables_priv |
    | time_zone |
    | time_zone_leap_second |
    | time_zone_name |
    | time_zone_transition |
    | time_zone_transition_type |
    | user |
    +---------------------------+
    24 rows in set (0.00 sec)

    MySQL数据库的数据文件存放在/usr/local/mysql/data中,每个子目录对应一个数据库,在MyISAM存储引擎时每个表对应三个文件:
    [root@localhost~]# Is /usr/local/mysql/data/mysql |grep user

    user.frm         表的结构定义
    user.MYD      表的数据
    user.MYI        表的索引

    显示数据表的结构(字段(列)):

    MariaDB [mysql]> desc user;

    DESCRIBE [数据库名.]表名 = desc

    创建新的数据库:

    MariaDB [(none)]> create database crushlinux;

    创建新的数据表:

    MariaDB [crushlinux]> create table users(user_name char(18) not null,user_passwd char(48) default '',primary key(user_name));
    Query OK, 0 rows affected (0.02 sec)

    MariaDB [crushlinux]> show tables;
    +----------------------+
    | Tables_in_crushlinux |
    +----------------------+
    | users |
    +----------------------+
    1 row in set (0.00 sec)

    MariaDB [crushlinux]> desc users;
    +-------------+----------+------+-----+---------+-------+
    | Field | Type | Null | Key | Default | Extra |
    +-------------+----------+------+-----+---------+-------+
    | user_name | char(18) | NO | PRI | NULL | |
    | user_passwd | char(48) | YES | | | |
    +-------------+----------+------+-----+---------+-------+
    2 rows in set (0.01 sec)

    删表:

    MariaDB [crushlinux]> drop table users;
    Query OK, 0 rows affected (0.01 sec)

    删库:

    MariaDB [crushlinux]> drop database crushlinux;
    Query OK, 0 rows affected (0.01 sec)

    向数据表中插入新的数据记录:

    (1)MariaDB [crushlinux]> insert into users(user_name,user_passwd) values('zhangsan','123');
    Query OK, 1 row affected (0.01 sec)

    (2)MariaDB [crushlinux]> insert into users values('zhaosi','123');
    Query OK, 1 row affected (0.01 sec)

    (3)MariaDB [crushlinux]> insert into users values('lisi',password('123')),('liuneng',password('123'));
    Query OK, 2 rows affected (0.01 sec)
    Records: 2 Duplicates: 0 Warnings: 0

    查看表中的数据:

    (1)MariaDB [crushlinux]> select * from users;
    +-----------+-------------------------------------------+
    | user_name | user_passwd |
    +-----------+-------------------------------------------+
    | lisi | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
    | liuneng | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
    | zhangsan | 123 |
    | zhaosi | 123 |
    +-----------+-------------------------------------------+
    4 rows in set (0.00 sec)

    (2)MariaDB [crushlinux]> select user_name from users;
    +-----------+
    | user_name |
    +-----------+
    | lisi |
    | liuneng |
    | zhangsan |
    | zhaosi |
    +-----------+
    4 rows in set (0.00 sec)

    (3)MariaDB [crushlinux]> select * from users where user_name='zhangsan';
    +-----------+-------------+
    | user_name | user_passwd |
    +-----------+-------------+
    | zhangsan | 123 |
    +-----------+-------------+
    1 row in set (0.01 sec)

    修改、更新数据表中的数据信息:

    MariaDB [crushlinux]> update users set user_passwd=password('321') where user_name='zhangsan';
    Query OK, 1 row affected (0.01 sec)
    Rows matched: 1 Changed: 1 Warnings: 0

    MariaDB [crushlinux]> select * from users;
    +-----------+-------------------------------------------+
    | user_name | user_passwd |
    +-----------+-------------------------------------------+
    | lisi | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
    | liuneng | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
    | zhangsan | *7297C3E22DEB91303FC493303A8158AD4231F486 |
    | zhaosi | 123 |
    +-----------+-------------------------------------------+
    4 rows in set (0.00 sec)

    在数据库中修改root用户的密码:

    MariaDB [mysql]> update user set password=password('123123') where user='root';
    Query OK, 3 rows affected (0.01 sec)
    Rows matched: 3 Changed: 3 Warnings: 0

    刷新授权表并Crul+d退出:

    MariaDB [(none)]> flush privileges;
    Query OK, 0 rows affected (0.00 sec)

    [root@localhost ~]# mysql -uroot -p123123
    Welcome to the MariaDB monitor. Commands end with ; or g.
    Your MariaDB connection id is 10
    Server version: 5.5.41-MariaDB MariaDB Server

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

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

    MariaDB [(none)]>

    在数据表中删除指定的数据记录:

    MariaDB [crushlinux]> delete from users where user_name='zhaosi';
    Query OK, 1 row affected (0.00 sec)

    设置用户权限(用户不存在是,则新建用户)

    MySQL数据库的root用户账户拥有对所有库,表的全部权限,频繁使用root账号会给数据库服务器带来 定的安全风险,实际工作中,通常会建立一些低权限的用户,只负责一部分库,表的管理和维护操作,甚至可以对查询,修改,删除记录等各种操作做进一步的细化限制,从而降低数据库的风险。

    格式:grant 权限列表 on 数据库名.表名 to 用户名@来源地址[identified by‘密码’];

    权限列表:用于列出授权的各种数据库操作,通过逗号进行分割,如:select,insertupdate等,all表示所有权限,可以执行任意操作。
    库名.表名:用于指定授权操作的数据库和表的名称,可以使用通配符(*)表示所有用户名@来源地址:用于指定用户和允许访问的客户机地址;来源地址可以是IP地址,域名,%通配符表示所有(但不能表示localhost
    MySQL通配符:

    _:任意单个字符 192.168.1_
    %:任意长度的任意字符 192.168.1.%

    案例:(只授予了查看权限)

    MariaDB [crushlinux]> grant select on crushlinux.* to 'teacher'@'localhost' identified by '123';
    Query OK, 0 rows affected (0.01 sec)

    [root@localhost ~]# mysql -uteacher -p123
    Welcome to the MariaDB monitor. Commands end with ; or g.
    Your MariaDB connection id is 12
    Server version: 5.5.41-MariaDB MariaDB Server

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

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

    MariaDB [(none)]>

    MariaDB [(none)]> show databases;
    +--------------------+
    | Database |
    +--------------------+
    | information_schema |
    | crushlinux |
    | test |
    +--------------------+
    3 rows in set (0.00 sec)

    查看用户的权限:

    MariaDB [(none)]> show grants for 'teacher'@'localhost';
    +----------------------------------------------------------------------------------------------------------------+
    | Grants for teacher@localhost |
    +----------------------------------------------------------------------------------------------------------------+
    | GRANT USAGE ON *.* TO 'teacher'@'localhost' IDENTIFIED BY PASSWORD '*23AE809DDACAF96AF0FD78ED04B6A265E05AA257' |
    | GRANT SELECT ON `crushlinux`.* TO 'teacher'@'localhost' |
    +----------------------------------------------------------------------------------------------------------------+
    2 rows in set (0.00 sec)

     撤销用户的权限:

    MariaDB [crushlinux]> revoke select on crushlinux.* from 'teacher'@'localhost';
    Query OK, 0 rows affected (0.01 sec)

    MariaDB [crushlinux]> show grants for 'teacher'@'localhost';
    +----------------------------------------------------------------------------------------------------------------+
    | Grants for teacher@localhost |
    +----------------------------------------------------------------------------------------------------------------+
    | GRANT USAGE ON *.* TO 'teacher'@'localhost' IDENTIFIED BY PASSWORD '*23AE809DDACAF96AF0FD78ED04B6A265E05AA257' |
    +----------------------------------------------------------------------------------------------------------------+
    1 row in set (0.00 sec)

    用于显示广泛的服务器状态信息:

    MariaDB [(none)]> show status;

    显示创建特定数据库或表:

    help create databases;

    help create tables;

    显示授权用户的安全权限:

    MariaDB [(none)]> show grants;

    显示服务器错误或警告信息:

    show erroes;

    show warnings;

    显示当前的时间:

    MariaDB [(none)]> select now();

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

    | now()               |

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

    | 2019-10-13 18:13:47 |

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

    1 row in set (0.01 sec)

    显示当前用户及时间:

    (1)MariaDB [(none)]> select current_user(),current_timestamp;

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

    | current_user() | current_timestamp   |

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

    | root@localhost | 2019-10-13 18:14:52 |

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

    1 row in set (0.01 sec)

    (2)MariaDB [(none)]> select user(),now();

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

    | user()         | now()               |

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

    | root@localhost | 2019-10-13 18:15:32 |

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

    1 row in set (0.00 sec)

    授权Windows客户机地址拥有访问权限:

    MariaDB [(none)]> grant all on *.* to 'root'@'192.168.200.110' identified by '123';

    Query OK, 0 rows affected (0.00 sec)

    MariaDB [(none)]> flush privileges;

    Query OK, 0 rows affected (0.00 sec)

     

     可以在这里打命令。

    MariaDB [crushlinux]> show grants for 'teacher'@'localhost';+----------------------------------------------------------------------------------------------------------------+| Grants for teacher@localhost                                                                                   |+----------------------------------------------------------------------------------------------------------------+| GRANT USAGE ON *.* TO 'teacher'@'localhost' IDENTIFIED BY PASSWORD '*23AE809DDACAF96AF0FD78ED04B6A265E05AA257' |+----------------------------------------------------------------------------------------------------------------+1 row in set (0.00 sec)

  • 相关阅读:
    在Visual Studio 2012 Blue theme下使用Dark theme的文本编辑器颜色设置
    How to build the Robotics Library from source code on Windows
    解数独的小程序
    XCAT在虚拟机上部署系统
    在docker里部署网络服务
    初学python里的yield send next
    opencl初体验
    cuda计算的分块
    尽信书不如无书
    docker on centos
  • 原文地址:https://www.cnblogs.com/990624lty-jhc/p/11667135.html
Copyright © 2011-2022 走看看