2.1 MySQL介绍
2.1.1 MySQL 历史
1979年:TcX公司 Monty Widenius, Unireg
1996年:发布MySQL1.0,Solaris版本,Linux版本
1999年:MySQL AB公司,瑞典
2003年:MySQL 5.0版本,提供视图、存储过程等功能
2008年:Sun 10亿美元收购MySQL
2009年:Oracle亿美元收购Sun
2009年:Monty成立MariaDB
2.1.2 MySQL系列
2.1.2.1 MySQL的三大主要分支
- mysql
- mariadb
- percona Server
2.1.2.2 官方地址
2.1.2.3 官方文档
https://www.percona.com/software/mysql-database/percona-server
2.1.2.4 版本演变
MySQL:5.1 –> 5.5 –> 5.6 –> 5.7 –> 8.0
MariaDB:5.5 –> 10.0 –> 10.1 –> 10.2 –> 10.3 –> 10.4 –> 10.5
MySQL被Sun收购后,搞了个过渡的6.0版本,没多久就下线了,后来被Oracle收购后,终于迎来了像样的5.6版本,之后就是5.7、8.0版本。由于6.0版本号已被用过,7.x系列版本专用于NDB Cluster,因而新版本号从8.0开始。
2.1.3 MySQL的特性
- 插件式存储引擎:也称为“表类型”,存储管理器有多种实现版本,功能和特性可能均略有差别;用户可根据需要灵活选择,MySQL5.5.5开始innoDB引擎是MySQL默认引擎
- MyISAM –> Aria
- InnoDB –> XtraDB
- 单进程,多线程
- 诸多扩展和新特性
- 提供了较多测试组件
- 开源
2.2 MySQL 安装方式介绍和快速安装
2.2.1 安装方式介绍
- 源代码:编译安装
- 二进制格式的程序包:展开至特定路径,并经过简单配置后即可使用
- 程序包管理器管理的程序包
2.2.2 RPM包安装MySQL
CentOS 安装光盘
项目官方: https://downloads.mariadb.org/mariadb/repositories
国内镜像:https://mirrors.tuna.tsinghua.edu.cn/mariadb/yum
https://mirrors.tuna.tsinghua.edu.cn/mysql/yum
CentOS8:安装光盘直接提供
- mysql-server:8.0
- mariadb-server:10.3.17
CentOS7:安装光盘直接提供
- mariadb-server:5.5 服务器包
- mariadb 客户端工具包
CentOS6:
- mysql-server:5.1 服务器包
- mysql 客户端工具包
2.3 初始化脚本提高安全性
运行脚本:mysql_secure_installation
设置数据库管理员root口令
禁止root远程登录
删除anonymous用户账号
删除test数据库
2.4 MySQL 组成
2.4.1 客户端程序
- mysql:交互式的CLI工具
- mysqldump:备份工具,基于mysql协议向mysqld发起查询请求,并将查得的所有数据转换成insert等写操作语句保存文本文件中
- mysqladmin:基于mysql协议管理mysqld
- mysqlimport:数据导入工具
MyISAM存储引擎管理工具
- myisamchk:检查MyISAM库
- myiampack:打包MyISAM表,只读
2.4.2 服务器端程序
- mysqld_safe
- mysqld
- mysqld_multi 多实例,示例:mysqld_multi –example
2.4.3 用户账号
mysql用户账号由两部分组成:’USERNAME’@’HOST’
说明:
- HOTS限制此用户可通过哪些远程主机连接mysql服务器
- 支持使用通配符
- % 匹配任意长度的任意字符:172.16.0.0/255.255.0.0或172.16.%.%
- _ 匹配任意单个字符
2.4.4 mysql 客户端命令
2.4.4.1 mysql运行命令类型
- 客户端命令:本地执行,每个命令都是完整形式和简写格式
mysql> \h, help
mysql> \u, use
mysql> \s, status
mysql> \!, system
- 服务端命令:通过mysql协议发往服务器执行并取回结果,命令末尾都必须使用命令结束符号,默认为分号
#示例:
mysql> SELECT VERSION();
2.4.4.2 mysql 使用模式
- 交互模式
- 脚本模式:
mysql -uSUERNAME -pPASSWORD < /path/somefile.sql
cat /path/somefile.sql | mysql -uUSERNAME -pPASSWORD
mysql>source /path/from/somefile.sql
2.4.4.3 mysql 命令使用格式
mysql [OPTIONS] [database]
mysql客户端常用选项:
-A, --no-auto-rehash 禁止补全
-u, --user= 用户名,默认为root
-h, --host= 服务器主机,默认为localhost
-p, --password= 用户密码,建议使用-p,默认为空密码
-P, --port= 服务器端口
-S, --socket= 指定连接socket文件路径
-D, --database= 指定默认数据库
-C, --compress 启用压缩
-e "SQL" 执行SQL命令
-V, --version 显示版本
-v, --verbose 显示详细信息
--print-defaults 获取程序默认使用的配置
登陆系统:
#默认空密码登陆
mysql -uroot -p
运行mysql命令:
mysql>USE mysql;
mysql>SELECT user(); #查看当前用户
mysql>SELECT user,host,password from user;
mysql>system clear; #清屏
mysql>SELECT database(); #查看当前数据库
范例: mysql的配置文件,修改提示符
#查看mysql版本
[root@centos8-2 ~]# mysql -V
mysql Ver 15.1 Distrib 10.3.28-MariaDB, for Linux (x86_64) using readline 5.1
#临时修改mysql提示符
[root@centos8-2 ~]# mysql -uroot --prompt="\\r:\\m:\\s(\\u@\\h) [\\d]>\\_"
04:14:00(root@localhost) [(none)]>
#临时修改mysql提示符
[root@centos8-2 ~]# export MYSQL_PS1="(\\u@\\h) [\\d]>\\_"
(root@localhost) [(none)]>
#永久修改mysql提示符
[root@centos8-2 ~]# vim /etc/my.cnf.d/mysql-clients.cnf
[mysql]
prompt="(\\u@\\h) [\\d]>\\_"
[root@centos8-2 ~]# mysql --print-defaults -v
mysql would have been started with the following arguments:
--prompt=(\u@\h) [\d]>\_ -v
[root@centos8-2 ~]# mysql -uroot
(root@localhost) [(none)]> use mysql;
(root@localhost) [mysql]>
范例:配置客户端mysql的自动登陆
[root@centos8-2 ~]# vim /etc/my.cnf.d/mysql-clients.cnf
[mysql]
prompt="(\\u@\\h) [\\d]>\\_"
user=hao
password=123456
#或者
[root@centos8-2 ~]# vim /etc/my.cnf.d/client.cnf
[client]
user=hao
password=123456
2.4.4.4 mysqladmin 命令
mysqladmin命令格式
mysqladmin [OPTIONS] command command ....
范例:
#查看mysql服务是否正常,如果正常提示mysqld is alive
mysqladmin -uroot -pxxxxxxx ping
#关闭mysql服务,但mysqladmin命令无法开启
mysqladmin -uroot -pxxxxxxx shutdown
#创建数据库testdb
mysqladmin -uroot -pxxxxxxx create testdb
#删除数据库testdb
mysqladmin -uroot -pxxxxxxx drop testdb
#修改root密码
mysqladmin -uroot -pxxxxxxx password redhat
#日志滚动,生成新文件/var/lib/mysql/mariadb-bin.00000N
mysqladmin -uroot -pxxxxxxx flush-logs
2.4.4.5 服务器端配置
服务器端(mysqld):工作特性有多种配置方式
1、命令行选项
2、配置文件:类ini格式,集中式的配置,能够为mysql的各应用程序提供配置信息
服务端配置文件:
/etc/my.cnf #Global选项
/etc/mysql/my.cnf #Global选项
~/.my.cnf #User-specific选项
配置文件格式:
[mysqld]
[mysqld_safe]
[mysqld_multi]
[mysql]
[mysqldump]
[server]
[client]
格式:
parameter = value
说明:
_和- 相同
1,ON,TRUE 意义相同
0,OFF,FALSE 意义相同
2.4.4.6 socket地址
官方说明
服务器监听的两种socket地址:
- ip socket:监听在tcp的3306端口,支持远程通信,侦听3306/tcp端口可以绑定在有一个或全部接口IP上
- unix sock:监听在sock文件上,仅支持本机通信,如:/var/lib/mysql/mysql.sock
- 说明:host为localhost时自动使用unix sock
范例:MySQL的端口
(root@localhost) [(none)]> SHOW VARIABLES LIKE 'port';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port | 3306 |
+---------------+-------+
1 row in set (0.001 sec)
#MySQL8.0增加了一个33060/tcp端口
#Port 33060 is the default port for the MySQL Database Extended Interface (the MySQL X Protocol).
mysql> SHOW VARIABLES LIKE 'mysqlx_port';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| mysqlx_port | 33060 |
+---------------+-------+
1 row in set (0.01 sec)
2.4.4.7 关闭mysqld网络连接
只侦听本地客户端,所有客户端和服务器的交互都通过一个socket文件实现,(socket的配置存放在/var/lib/mysql/mysql.sock)可在/etc/my.cnf修改
范例:
vim /etc/my.cnf
[mysqld]
skip-networking=1
I have read so many posts about the blogger lovers however this post is really a good piece of writing, keep it up
whoah this blog is wonderful i really like reading your articles. Keep up the great paintings! You realize, a lot of people are hunting round for this info, you could help them greatly.
I have read so many posts about the blogger lovers however this post is really a good piece of writing, keep it up
whoah this blog is wonderful i really like reading your articles. Keep up the great paintings! You realize, a lot of people are hunting round for this info, you could help them greatly.
This is very useful post for me. This will absolutely going to help me in my project.