169、MYSQL基本操作

2710在7上面使用的mariadb,是5.5版本的,和MySQL的5.5版本是一样的


5.6之后版本号就和MySQL不一样了,是10版本了。

 

 3130在官网上面有3种类型的,tar.gz后缀表示的是源码包。

 

 3250要学会3种方式安装,源码,二进制, yum安装。最后一种最简单。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

第二种3420选择不带库的二进制的包,因为我们不做开发。这个是编译过的,所以解压缩目录不能随便放的。

 

 

 

 

 

 

 

 

 

 3550第三种yum包安装   For best results with RPM packages, use the  Repository Configuration Tool

 

 

 

 

 

 

 

 

4920安装MySQL或者Mariadb的方式:

1、源代码:编译安装

2、二进制格式的程序包:展开至特定路径,并经过简单配置后即可使用

3、程序包管理器管理的程序包CentOS安装光盘

项目官方:https://downloads.mariadb.org/mariadb/repositories/

 

 

 

 

To generate the entries select an item from each of the boxes below. Once an item is selected in each box, your customized repository configuration will appear below.

1. Choose a Distro
  • SLES

  • openSUSE

  • Arch Linux

  • Mageia

  • Fedora

  • CentOS

  • RedHat

  • Mint

  • Ubuntu

  • Debian

2. Choose a Release
  • CentOS 7 (ppc64le)

  • CentOS 7 (ppc64)

  • CentOS 7 (x86_64)

  • CentOS 6 (x86_64)

  • CentOS 6 (x86)

3. Choose a Version
  • 10.3 [Stable]

  • 10.4 [Alpha]

  • 10.2 [Stable]

  • 10.1 [Stable]

  • 10.0 [Stable]

  • 5.5 [Stable]

Here is your custom MariaDB YUM repository entry for CentOS. Copy and paste it into a file under /etc/yum.repos.d/ (we suggest naming the file MariaDB.repo or something similar).

#  MariaDB 10.2 CentOS repository list - created 2018-12-06 07:59 UTC  # http://downloads.mariadb.org/mariadb/repositories/  [mariadb] name = MariaDB baseurl =  http://yum.mariadb.org/10.2/centos7-amd64  gpgkey= https://yum.mariadb.org/RPM-GPG-KEY-MariaDB  gpgcheck=1

After the file is in place, install MariaDB with:

sudo yum install MariaDB-server MariaDB-client

If you haven't already accepted the MariaDB GPG key, you will be prompted to do so. See " Installing MariaDB with yum " for detailed information.

Please see  Installing OQGraph  for details on additional install steps needed for that storage engine.

 

 

 

复制代码


# MariaDB 10.2 CentOS repository list - created 2018-12-06 07:59 UTC
# http://downloads.mariadb.org/mariadb/repositories


只要在
[root@centos7 yum.repos.d]# vim mariadb.repo输入下面的内容就可以进行在线安装了。

我安装的是10.3,那么
baseurl = http://yum.mariadb.org/10.3/centos7-amd64


[mariadb] name = MariaDB baseurl = http://yum.mariadb.org/10.2/centos7-amd64 gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB gpgcheck=1

复制代码

 

 

 

 

 

 

 

 

 

 3650配置一下yum源,进行yum安装。 我们要安装服务器端的,后面加上server。 注意工作一般使用的是5.5,因为比较稳定

 


4110重点要掌握的是二进制和源码包安装。 二进制安装好,解压缩配一下就好了。编译的话时间就会比较长

 

 

 

[root@centos7 yum.repos.d]# yum install mariadb-server.x86_64 -y

 

 

 

 

4200数据库的体系结构

 

 

 

 

 

 

4210MySQL和mariadb的关系就是卖了大女儿,再养小女儿。

 


4230每一个代表的是功能模块。 mysql是对外提供服务,不是本机自己访问自己,所以要有一个管理连接的模块。


MySQL是多线程的,一启动就会出现很多的线程。所以有线程管理模块。

 


4310命令分发器,当一个用户通过远程连接过来的时候就有一个用户管理的模块


比如有一个用户发一个指令过来,在数据库里面查询一个表。而命令分发器来决定到底如何执行这个命令


其中可能会利用缓存,就是曾经或者刚刚执行过了这个查询语句了,就不要再重新解释执行一次了, 缓存里面的数据直接拿过来用就可以了。

 


4350我们平时敲外部命令会用HASH,会把命令记录到内存里面去了

下次访问就不要到磁盘上面找一圈了。

 

复制代码

[root@centos7 yum.repos.d]# vim mariadb.repo 
[root@centos7 yum.repos.d]# ls
dvd.repo  mariadb.repo  nginx.repo
[root@centos7 yum.repos.d]# hash 这个查询缓存模块的作用也是一样的,执行了一遍就不需要再次执行了。
hits    command
   3    /usr/bin/vim
   3    /usr/bin/ls

复制代码

 

 

4410日志记录功能,做了哪些命令会有日志记录。 命令解释器对用户发过来的命令进行解释执行


对表做什么操作,是查询还是表的变更。 用户访问就涉及到了访问控制模块。

 


4500不同是引擎其算法机制底层是不太一样的


MySQL5.1之前默认用的是MyISAM,5.5之后默认用的是innodb。

 

刚学习数据库,我们掌握innodb就可以了

 

 

4650myisam和innodb的区别

 

前者不支持事务,这样会造成数据的损坏。而且有锁,并发访问性能比较差的,


也就是只能一个人修改表,因为系统会自动往这个表加锁。

 

很多人同时访问不能修改表


而后者是基于行级锁,也就是一个人修改一条记录,只是把一条记录锁住。

 

而第二个人要修改的是第10行,和第一个人不冲突的


这两个区别也说明了我们为什么要使用innodb。

 

 

 

5620注意在工作中是不升级的。只有重新安装。因为升级的问题更多


包括Linux系统也是不升级的

 

复制代码


[root@centos7 yum.repos.d]# rpm -ql mariadb
package mariadb is not installed
[root@centos7 yum.repos.d]# rpm -ql M
MariaDB-client     MariaDB-common     MariaDB-compat     MariaDB-server     ModemManager-glib  
[root@centos7 yum.repos.d]# rpm -ql MariaDB-server 






复制代码

 

 

 


5650 etc/my.cnf.d这个是他的配置文件。 他的目录下面的文件也是配置文件,比如 /etc/my.cnf.d/server.cnf

 

5710下面是他的服务 /usr/lib/systemd/system/mariadb.service

 


在启动之前查看一下端口号,没有看到3306启动

 

复制代码

[root@centos7 ~]# ss -tnl
State      Recv-Q Send-Q                                                 Local Address:Port                                                                Peer Address:Port              
LISTEN     0      128                                                                *:111                                                                            *:*                  
LISTEN     0      5                                                      192.168.122.1:53                                                                             *:*                  
LISTEN     0      128                                                                *:22                                                                             *:*                  
LISTEN     0      128                                                        127.0.0.1:6010                                                                           *:*                  
LISTEN     0      128                                                        127.0.0.1:6011                                                                           *:*                  
LISTEN     0      128                                                               :::111                                                                           :::*                  
LISTEN     0      128                                                               :::22                                                                            :::*                  
LISTEN     0      128                                                              ::1:6010                                                                          :::*                  
LISTEN     0      128                                                              ::1:6011

复制代码

 

 

 

注意服务名和包名不一样,服务名是不带server而且.service是可以忽略的.


而包名是带client.这是在mariadb的情况下

 

复制代码

[root@centos7 ~]# systemctl start mariadb.service 
[root@centos7 ~]# ss -tnl
State      Recv-Q Send-Q                                                 Local Address:Port                                                                Peer Address:Port              
LISTEN     0      128                                                                *:111                                                                            *:*                  
LISTEN     0      5                                                      192.168.122.1:53                                                                             *:*                  
LISTEN     0      128                                                                *:22                                                                             *:*                  
LISTEN     0      128                                                        127.0.0.1:6010                                                                           *:*                  
LISTEN     0      128                                                        127.0.0.1:6011                                                                           *:*                  
LISTEN     0      80                                                                :::3306                                                                          :::*                  
LISTEN     0      128                                                               :::111                                                                           :::*                  
LISTEN     0      128                                                               :::22                                                                            :::*                  
LISTEN     0      128                                                              ::1:6010                                                                          :::*                  
LISTEN     0      128                                                              ::1:6011                                                                          :::*

复制代码

 

 


如果是MySQL的话,那么客户端的包名就是MySQL了。


在安装服务器的时候就默认把客户端安装好了

 

 

 

 

 



 

复制代码

[root@centos7 ~]# rpm -ql mariadb
package mariadb is not installed
[root@centos7 ~]# rpm -ql MariDB
package MariDB is not installed
[root@centos7 ~]# which mysql有个客户端工具叫做mysql,也是客户端的命令。/usr/bin/mysql
[root@centos7 ~]# rpm -qf /usr/bin/mysql
MariaDB-client-10.3.11-1.el7.centos.x86_64注意在mariadb里面不存在5.6版本,而是10版本了。在MySQL里面才叫5.5,5.6了。

[root@centos7 yum.repos.d]# rpm -ql MariaDB-server 
/etc/init.d/mysql
/etc/logrotate.d/mysql
/etc/my.cnf.d配置文件

 

复制代码


注意不带server的是客户端。


[root@centos7 ~]# yum info mariadb Loaded plugins: fastestmirror, langpacks Loading mirror speeds from cached hostfile Available Packages Name        : mariadb Arch        : x86_64 Epoch       : 1 Version     : 5.5.56 Release     : 2.el7 Size        : 8.7 M Repo        : dvd Summary     : A community developed branch of MySQL URL         : http://mariadb.org License     : GPLv2 with exceptions and LGPLv2 and BSD Description : MariaDB is a community developed branch of MySQL.            : MariaDB is a multi-user, multi-threaded SQL database server.            : It is a client/server implementation consisting of a server daemon (mysqld)            : and many different client programs and libraries. The base package            : contains the standard MariaDB/MySQL client programs and generic MySQL files.

复制代码

 

 

 

 

 

复制代码

 



 

 



 

 

 

 

=0420查看MySQL的官网。

 

 

=0810这个是自己连自己,以交互式的方式执行一些命令.

 

复制代码

[root@centos7 ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.3.11-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

复制代码

 

 

复制代码


查看帮助


MariaDB [(none)]> \h General information about MariaDB can be found at http://mariadb.org List of all MySQL commands: Note that all text commands must be first on line and end with ';' ?         (\?) Synonym for `help'. clear     (\c) Clear the current input statement. connect   (\r) Reconnect to the server. Optional arguments are db and host. delimiter (\d) Set statement delimiter. edit      (\e) Edit command with $EDITOR. ego       (\G) Send command to mysql server, display result vertically. exit      (\q) Exit mysql. Same as quit. go        (\g) Send command to mysql server. help      (\h) Display this help. nopager   (\n) Disable pager, print to stdout. notee     (\t) Don't write into outfile. pager     (\P) Set PAGER [to_pager]. Print the query results via PAGER. print     (\p) Print current command. prompt    (\R) Change your mysql prompt. quit      (\q) Quit mysql. rehash    (\#) Rebuild completion hash. source    (\.) Execute an SQL script file. Takes a file name as an argument. status    (\s) Get status information from the server. system    (\!) Execute a system shell command. tee       (\T) Set outfile [to_outfile]. Append everything into given outfile. use       (\u) Use another database. Takes database name as argument. charset   (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets. warnings  (\W) Show warnings after every statement. nowarning (\w) Don't show warnings after every statement.

复制代码

 

 

 

复制代码

查看数据库的基本信息
MariaDB [(none)]>-------------- Distrib .-MariaDB,  Linux (x86_64) using readline :        .-/var/lib/mysql/ min   Questions:   Slow queries:   Opens:   Flush tables:   Open tables:   Queries per second avg: --------------

复制代码

 

 

1100更改提示符

prompt (\R) Change your mysql prompt.

 

MariaDB [(none)]> 中的none表示我们没有在任何的数据库里面

 

1140查看数据库的列表

复制代码

MariaDB [(none)]> show databases;+--------------------+
| Database           |
+--------------------+
| information_schema |
| mydb               |
| mysql              |
| performance_schema |
| test               |
| testdb             |
+--------------------+6 rows in set (0.035 sec)

复制代码

 

复制代码

数据库是放在这里的 /var/lib/mysql

[root@centos7 .repos.d]# cd  /var/lib/mysql/  centos7.pid     ibdata1      ib_logfile1  multi-master.

 




复制代码

 

 

数据库是放在这里的 /var/lib/mysql。 有6个数据库,但是 information_schema是系统内置的数据库没有显示出。 有些是虚拟数据库,不一定是真实存在的。

 


1230数据库是一个文件夹,我们可以打开看到里面相应的文件

 

 

 

 

 

1330查询表

查看所有的字段*, 要指定数据库

 

 

复制代码

MariaDB [(none)]> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [mysql]> select * from user;+-----------------------+------+----------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+------------+--------------+------------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+--------+-----------------------+
| Host                  | User | Password | Select_priv | Insert_priv | Update_priv | Delete_priv | Create_priv | Drop_priv | Reload_priv | Shutdown_priv | Process_priv | File_priv | Grant_priv | References_priv | Index_priv | Alter_priv | Show_db_priv | Super_priv | Create_tmp_table_priv | Lock_tables_priv | Execute_priv | Repl_slave_priv | Repl_client_priv | Create_view_priv | Show_view_priv | Create_routine_priv | Alter_routine_priv | Create_user_priv | Event_priv | Trigger_priv | Create_tablespace_priv | ssl_type | ssl_cipher | x509_issuer | x509_subject | max_questions | max_updates | max_connections | max_user_connections | plugin | authentication_string |


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/31563263/viewspace-2284615/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/31563263/viewspace-2284615/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值