文章目录
一、MySQL简介
1.1 MySQL 系列
MySQL 的三大主要分支:
版本演变
MySQL:5.1 --> 5.5 --> 5.6 --> 5.7 -->8.0
MariaDB:5.1 -->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开始。
官网社区版
二、MySQL安装
以下有三种安装方式:包管理安装、二进制安装、docker安装
2.1 包管理安装
2.1.1 Ubuntu22.04
Ubuntu 22.04存储库中可用的MySQL当前版本为8.0。直接安装即可。
2.1.1.1 安装
···
apt update && apt upgrade
apt install -y mysql-server
···
2.1.1.2 安全初始化
二进制安装后也需要执行这一步安全加固功能。
mysql_secure_installation
houor@IIP03:~$ sudo mysql_secure_installation
Securing the MySQL server deployment.
Connecting to MySQL using a blank password.
# 为root用户设置密码
VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?
Press y|Y for Yes, any other key for No: y
# 可以设置三种密码验证策略
There are three levels of password validation policy:
LOW Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2
Please set the password for root here.
# 输入密码
New password:
Re-enter new password:
Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
# 是否删除匿名用户
# 生产环境中一般要删除匿名用户
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.
# 是否运行root用户远程登录
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) :
... skipping.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
# 是否删除test数据库
Remove test database and access to it? (Press y|Y for Yes, any other key for No) :
... skipping.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
# 开始刷新授权表,使设置生效
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.
All done!
2.1.2 Rocky9
参考以下图片下载rpm包
2.1.2.1 配置仓库
# 执行wget命令下载rpm包
[root@rocky9 ~]$ wget https://dev.mysql.com/get/mysql80-community-release-el9-5.noarch.rpm
[root@rocky9 ~]$ yum install -y mysql80-community-release-el9-1.noarch.rpm
# 查看是否添加到仓库
[root@rocky9 ~]$ yum repolist enabled
repo id repo name
appstream Rocky Linux 9 - AppStream
baseos Rocky Linux 9 - BaseOS
extras Rocky Linux 9 - Extras
mysql-connectors-community MySQL Connectors Community
mysql-tools-community MySQL Tools Community
mysql80-community MySQL 8.0 Community Server
2.1.2.2 安装
[root@rocky9 ~]$ yum install -y mysql-community-server
[root@rocky9 ~]$ systemctl start mysqld
# 获取密码
[root@rocky9 log]$ cat /var/log/mysqld.log
# 测试
[root@rocky9 log]$ mysql -uroot -p
Enter password:
[root@rocky9 ~]$ mysql -V
mysql Ver 8.0.35 for Linux on x86_64 (MySQL Community Server - GPL)
2.1.2.3 安全初始化同Ubuntu22.04
2.2 二进制安装 MySQL8.0
2.2.1 Rocky9 安装MySQL8.0
# 安装相关包
[root@rocky9 ~]$ yum -y install libaio numactl-libs ncurses-devel
# 用户和组
[root@rocky9 ~]$ groupadd mysql
[root@rocky9 ~]$ useradd -r -g mysql -s /bin/false mysql
# 准备程序文件,用上述图片下载的二进制包,也可以使用以下命令下载
[root@rocky9 ~]$ wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.35-linux-glibc2.17-x86_64.tar
[root@rocky9 ~]$ tar -xf mysql-8.0.35-linux-glibc2.17-x86_64.tar -C /usr/local/
[root@rocky9 ~]$ cd /usr/local/
[root@rocky9 local]$ tar -xf mysql-8.0.35-linux-glibc2.17-x86_64.tar.xz
[root@rocky9 local]$ ln -s mysql-8.0.35-linux-glibc2.17-x86_64 mysql
[root@rocky9 local]$ chown -R root.root /usr/local/mysql/
[root@rocky9 local]$ ll
total 809196
lrwxrwxrwx 1 root root 35 Dec 18 23:34 mysql -> mysql-8.0.35-linux-glibc2.17-x86_64
drwxr-xr-x 9 root root 129 Dec 18 23:33 mysql-8.0.35-linux-glibc2.17-x86_64
# 准备环境变量
[root@rocky9 local]$ echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@rocky9 local]$ . /etc/profile.d/mysql.sh
# 准备配置文件
[root@rocky9 local]$ mkdir -p /data/mysql
[root@rocky9 local]$ vim /etc/my.cnf
[mysqld]
datadir=/data/mysql
skip_name_resolve=1
socket=/data/mysql/mysql.sock
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
[client]
socket=/data/mysql/mysql.sock
# 生成数据库文件,并提取root密码
[root@rocky9 local]$ mysqld --initialize --user=mysql --datadir=/data/mysql
[root@rocky9 local]$ grep password /data/mysql/mysql.log
2023-12-18T15:38:36.577598Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: #m9adkJ*WOk.
# 准备服务脚本和启动
# 需要写MySQL组和用户名,不然会报以root用户启动的错
[root@rocky9 ~]$ vim /etc/systemd/system/mysql.service
[Unit]
Description=MySQL Server
After=network.target
[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf
ExecStop=/usr/local/mysql/bin/mysqladmin -u root -p shutdown
Restart=always
[Install]
WantedBy=multi-user.target
# 启动
[root@rocky9 ~]$ systemctl start mysql
[root@rocky9 ~]$ ss -tnlp
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=1654,fd=3))
LISTEN 0 70 *:33060 *:* users:(("mysqld",pid=2951,fd=20))
LISTEN 0 151 *:3306 *:* users:(("mysqld",pid=2951,fd=22))
# 修改口令
[root@rocky9 ~]$ mysqladmin -uroot -p'#m9adkJ*WOk.' password My2024SQL..
# 测试登录
[root@rocky9 ~]$ mysql -uroot -pMy2024SQL..
这里有个报错:
[root@rocky9 bin]$ mysql -uroot -p#m9adkJ*WOk.
mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory
mysql: error while loading shared libraries: libtinfo.so.5: cannot open shared object file: No such file or directory
依赖5版本,但目前Rocky9 只有6,暂且使用这种方式过渡一下
find / -name 'libncurses*'
/usr/lib/x86_64-linux-gnu/libncurses.so.6.2
ln -s libncurses.so.6.2 libncurses.so.5
[root@rocky9 ~]$ find / -name 'libtinfo.so.*'
/usr/lib64/libtinfo.so.6
/usr/lib64/libtinfo.so.6.2
[root@rocky9 ~]$ cd /usr/lib64/
[root@rocky9 lib64]$ ln -s libtinfo.so.6 libtinfo.so.5
2.2.2 Ubuntu22.04 安装 MySQL8.0
# 以下没有写明的同rocky9步骤
# 安装相关包
apt install -y numactl libaio-dev libtinfo5
# 用户和组
# 准备程序文件
root@dev:~# wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.35-linux-glibc2.17-x86_64.tar
root@dev:~# tar -xf mysql-8.0.35-linux-glibc2.17-x86_64.tar -C /usr/local/
root@dev:/usr/local# tar -xf mysql-8.0.35-linux-glibc2.17-x86_64.tar.xz
root@dev:/usr/local# ln -s mysql-8.0.35-linux-glibc2.17-x86_64 mysql
root@dev:/usr/local# chown -R root.root /usr/local/mysql/
# 准备环境变量
# 准备配置文件
# 生成数据库文件,并提取root密码
root@dev:/usr/local# grep password /data/mysql/mysql.log
2023-12-18T16:56:39.187525Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: m.yF0RsViWnS
# 准备服务脚本和启动
# 修改口令
root@dev:/usr/local/mysql# mysqladmin -uroot -p'm.yF0RsViWnS' password My2024SQL..
# 测试登录
root@dev:/usr/local/mysql# mysql -uroot -pm.yF0RsViWnS
2.3 初始化脚本提高安全性
运行脚本:mysql_secure_installation
设置数据库管理员root口令
禁止root远程登录
删除anonymous用户帐号
删除test数据库
2.4 docker-compose 安装MySQL
2.4.1 准备my.cnf文件
[mysqld]
# 设置时区和SQL模式
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
# default-time-zone = '+08:00'
# 设置不区分数据库表名大小写
lower_case_table_names=1
# 设置字符集
character-set-server=utf8mb4
collation-server=utf8mb4_general_ci
user=root
# Allow root remote login
bind-address=0.0.0.0
# 设置InnoDB为默认存储引擎
# default-storage-engine = InnoDB
# 设置线程缓存数量,这个数值设置得太大可能会导致线程的创建和销毁变慢
# thread_cache_size = 8
# 设置最大连接数,这个数值应该根据你的应用并发连接的需求来设定
max_connections = 500
# 设置表缓存,这个数值应该根据你的应用的表的数量来设定
table_open_cache = 2000
# 设置排序和临时表的大小
sort_buffer_size = 4M
read_buffer_size = 4M
read_rnd_buffer_size = 8M
join_buffer_size = 4M
tmp_table_size = 64M
max_heap_table_size = 64M
# 设置查询缓存,但请注意在某些场景下(如高并发写入)查询缓存可能会导致性能下降
#query_cache_type = 1
#query_cache_size = 64M
#query_cache_limit = 2M
# 设置InnoDB的缓冲池大小,这个数值通常应该设置为系统内存的50%到75%
# innodb_buffer_pool_size = 4G
# 设置InnoDB的日志文件大小,这个数值取决于你的写入负载,但一般不应该超过innodb_buffer_pool_size的25%
# innodb_log_file_size = 1G
# 设置InnoDB的日志缓冲大小
# innodb_log_buffer_size = 64M
# 关闭InnoDB的双写,可以提高写入性能,但在某些情况下可能会导致数据损坏
#innodb_doublewrite = 0
# 启用InnoDB的自适应哈希索引,可以提高读取性能
innodb_adaptive_hash_index = 1
2.4.2 编写docker-compose.yml 文件
version: '3'
services:
mysql:
restart: "always"
image: mysql:5.7
container_name: docker-mysql57
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: 'MySQL57'
SET_CONTAINER_TIMEZONE: true
CONTAINER_TIMEZONE: Asia/Shanghai
TZ: Asia/Shanghai
volumes:
- ./data/mysql:/var/lib/mysql
- ./my.cnf:/etc/mysql/conf.d/my.cnf
- /etc/localtime:/etc/localtime:ro
- /etc/timezone:/etc/timezone:ro
2.5 一键安装脚本
2.5.1 MySQL 5.7 脚本
2.5.2 MySQL 8.0 脚本
脚本地址 Ubuntu22.04可以使用 Rocky9有问题
三、MySQL组成
3.1 客户端程序
- mysql: 交互式的CLI工具
- mysqldump:备份工具,基于mysql协议向mysqld发起查询请求,并将查得的所有数据转换成insert等写操作语句保存文本文件中
- mysqladmin:基于mysql协议管理mysqld
- mysqlimport:数据导入工具
3.2 服务器端程序
- mysqld_safe
- mysqld
- mysqld_multi 多实例 ,示例:mysqld_multi --example
3.3 用户账号
mysql用户账号由两部分组成:'USERNAME'@'HOST'
说明:
HOST:限制此用户可通过哪些远程主机连接mysql服务器
支持使用通配符:
% 匹配任意长度的任意字符
172.16.0.0/255.255.0.0 或 172.16.%.%
_ 匹配任意单个字符
3.4 mysql 客户端命令
3.4.1 mysql 运行命令类型
客户端命令:本地执行,每个命令都完整形式和简写格式
mysql> \h, help
mysql> \u,use
mysql> \s,status
mysql> \!,system
服务端命令:通过mysql协议发往服务器执行并取回结果,命令末尾都必须使用命令结束符号,默认为分号
#示例:
mysql>SELECT VERSION();
3.4.2 mysql 使用模式
- 交互模式
- 脚本模式:
mysql -uUSERNAME -pPASSWORD < /path/somefile.sql
cat /path/somefile.sql | mysql -uUSERNAME -pPASSWORD
mysql>source /path/from/somefile.sql
3.4.3 mysql命令使用格式
mysql [OPTIONS] [database]
mysql客户端常用选项:
-A, --no-auto-rehash 禁止补全
-u, --user= 用户名,默认为root
-h, --host= 服务器主机,默认为localhost
-p, --passowrd= 用户密码,建议使用-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的配置文件,修改提示符
# 查看mysql版本
[root@dev ~]# mysql -V
mysql Ver 8.0.35-0ubuntu0.22.04.1 for Linux on x86_64 ((Ubuntu))
# 临时修改mysql提示符
[root@dev ~]# mysql -uroot -proot --prompt="\\r:\\m:\\s(\\u@\\h) [\\d]>\\_"
# 临时修改mysql提示符
[root@dev ~]# export MYSQL_PS1="\\r:\\m:\\s(\\u@\\h) [\\d]>\\_"
# 持久修改mysql提示符
[root@dev ~]# vim /etc/my.cnf.d/mysql-clients.cnf
[mysql]
prompt="\\r:\\m:\\s(\\u@\\h) [\\d]>\\_"
[root@dev ~]# mysql --print-defaults -v
mysql would have been started with the following arguments:
--prompt=\r:\m:\s(\u@\h) [\d]>\_ -v
[root@dev ~]$ mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 77
Server version: 8.0.35-0ubuntu0.22.04.1 (Ubuntu)
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
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.
(root@localhost) [(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
(root@localhost) [mysql]> exit
Bye
范例:配置客户端mysql的自动登录
vim /etc/my.cnf.d/client.conf
[client]
user=root
password=root
3.4.4 mysqladmin命令
mysqladmin 命令格式
mysqladmin [OPTIONS] command command....
范例:
#查看mysql服务是否正常,如果正常提示mysqld is alive
mysqladmin -uroot -proot ping
#关闭mysql服务,但mysqladmin命令无法开启
mysqladmin -uroot -proot shutdown
#创建数据库testdb
mysqladmin -uroot -prootcreate testdb
#删除数据库testdb
mysqladmin -uroot -proot drop testdb
#修改root密码
mysqladmin -uroot -proot password 'root'
#日志滚动,生成新文件/var/lib/mysql/mariadb-bin.00000N
mysqladmin -uroot -proot flush-logs
3.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意义相同,无区分大小写
3.4.6 socket地址
服务器监听的两种socket地址:
- ip socket: 监听在tcp的3306端口,支持远程通信 ,侦听3306/tcp端口可以在绑定有一个或全部接口IP上
- unix sock: 监听在sock文件上,仅支持本机通信, 如:
/var/lib/mysql/mysql.sock
说明:host为localhost 时自动使用unix sock
3.4.7 关闭mysqld网络连接
只侦听本地客户端, 所有客户端和服务器的交互都通过一个socket文件实现,socket的配置存放
在/var/lib/mysql/mysql.sock) 可在/etc/my.cnf修改
范例:
vim /etc/my.cnf
[mysqld]
skip-networking=1
bind_address=127.0.0.1