文章目录
-
- MySQL工作模型及实例
-
- [MySQL 客户端/服务器工作模型(C/S)](#MySQL 客户端/服务器工作模型(C/S))
- 服务端:实例
- MySQLd的程序结构
- MySQL的逻辑结构
- MySQL的物理存储结构
- MySQL基础管理
MySQL工作模型及实例
MySQL 客户端/服务器工作模型(C/S)

1.本地socket链接方式
socket=/tmp/mysql.sock
shell
mysql -uroot -p -S /tmp/mysql.sock

说明:只能在本地使用,不依赖于IP和端口
2.远程TCPIP链接方式
shell
mysql -uroot -p -h 127.0.0.1 -P 3306

服务端:实例
实例:mysqld+工作线程+预分配的内存结构
功能:管理数据

MySQLd的程序结构

查看连接线程
mysql
show processlist;

MySQL的逻辑结构
表+库
查看mysql中所有的数据库
mysql
show databases;

切换到某个数据库
mysql
use mysql;

查看库中所有的表
mysql
show tables;

查看表结构
mysql
desc user;

MySQL的物理存储结构
段:一个表就是一个段,可以由1个或者多个区构成
区:一个区(簇)默认1M,由连续的64个page构成
页:一个页,默认16KB,由连续的4个OS block构成
OS 块:一个块,4KB,由连续的8个扇区构成
扇区:一个扇区,512B

MySQL基础管理
用户管理
1.用户的定义 mysql@'白名单'
robin@'localhost' : robin用户能够通过本地登录MySQL(socket)
robin@'192.168.0.130': robin用户能够通过192.168.0.130远程登录MySQL服务器
robin@'192.168.0.%': robin用户能够通过192.168.0.xx/24远程登录MySQL服务器
robin@'192.168.0.5%': robin用户能够通过192.168.0.50-59远程登录MySQL服务器
robin@'192.168.0.130/255.255.254.0': robin用户能够通过192.168.0.130远程登录MySQL服务器 23位子网掩码
robin@'%': robin用户能够通过所有IP远程登录MySQL服务器
robin@'vmcode': robin用户能够通过主机名为vmcode远程登录MySQL服务器
robin@'vmcode.cn': robin用户能够通过域名为vmcode.cn远程登录MySQL服务器
2.用户管理
查询用户信息
mysql
select user,host,authentication_string from mysql.user;

创建用户
mysql
create user robin@'localhost';

创建用户并设置密码
mysql
create user robin@'192.168.0.%' identified by 'admin123';

mysql所有用户存储位置:cd /data/mysql/3306/data/mysql
中的user.MYD
文件

执行命令:strings user.MYD

修改用户密码
mysql
alter user robin@'localhost' identified by 'admin123';

删除用户
mysql
drop user robin@'localhost';

注意:8.0版本以前,可以通过grant命令建立用户和授权
权限管理
1.查看权限:
mysql
show privileges;

mysql
+-------------------------+---------------------------------------+---------------+
| Privilege | Context | Comment |
+-------------------------+---------------------------------------+---------------+
| Alter | Tables | To alter the table |
| Alter routine | Functions,Procedures | To alter or drop stored functions/procedures |
| Create | Databases,Tables,Indexes | To create new databases and tables |
| Create routine | Databases | To use CREATE FUNCTION/PROCEDURE |
| Create temporary tables | Databases | To use CREATE TEMPORARY TABLE |
| Create view | Tables | To create new views |
| Create user | Server Admin | To create new users |
| Delete | Tables | To delete existing rows |
| Drop | Databases,Tables | To drop databases, tables, and views |
| Event | Server Admin | To create, alter, drop and execute events |
| Execute | Functions,Procedures | To execute stored routines |
| File | File access on server | To read and write files on the server |
| Grant option | Databases,Tables,Functions,Procedures | To give to other users those privileges you possess |
| Index | Tables | To create or drop indexes |
| Insert | Tables | To insert data into tables |
| Lock tables | Databases | To use LOCK TABLES (together with SELECT privilege) |
| Process | Server Admin | To view the plain text of currently executing queries |
| Proxy | Server Admin | To make proxy user possible |
| References | Databases,Tables | To have references on tables |
| Reload | Server Admin | To reload or refresh tables, logs and privileges |
| Replication client | Server Admin | To ask where the slave or master servers are |
| Replication slave | Server Admin | To read binary log events from the master |
| Select | Tables | To retrieve rows from table |
| Show databases | Server Admin | To see all databases with SHOW DATABASES |
| Show view | Tables | To see views with SHOW CREATE VIEW |
| Shutdown | Server Admin | To shut down the server |
| Super | Server Admin | To use KILL thread, SET GLOBAL, CHANGE MASTER, etc. |
| Trigger | Tables | To use triggers |
| Create tablespace | Server Admin | To create/alter/drop tablespaces |
| Update | Tables | To update existing rows |
| Usage | Server Admin | No privileges - allow connect only |
+-------------------------+---------------------------------------+---------------------------+
2.授权、回收权限操作
语法:
8.0-: grant 权限1,权限2,... on 对象 to 用户 identified by '密码';
8.0+:create user 用户 identified by '密码';
grant 权限1,权限2,... on 对象 to 用户;
grant 权限1,权限2,.... on 对象 to 用户 identified by '密码' with grant option;
ALL : 管理员(不包括grant option权限)
权限1,权限2,... : 普通用户(业务用户,开发用户)
Grant option : 给别的用户授权
对象:
*.* :- -----> chmod -R 755 /
robin.* -----> chmod -R 755 /robin
robin.t1 -----> chmod -R 755 /robin/t1
(1).创建并授权管理员用户robin,能够通过192.168.0.%网段登录并管理数据库
mysql
grant all on *.* to robin@'192.168.0.%' identified by 'admin123' with grant option;

(2).查看权限
mysql
show grants for robin@'192.168.0.%';

(3).查看全部用户权限
mysql
select * from mysql.user \G -- \G竖排显示

(4).创建并授权一个test@'192.168.0.%'业务用户,能够对test库下所有对象进行create,select,update,delete,insert操作
mysql
grant create,select,update,delete,insert on test.* to test@'192.168.0.%' identified by 'admin123';

(5)查看权限
mysql
select * from mysql.db \G

(6).各权限表说明
mysql.db: 基于库结构的权限 test.*
mysql.tables_priv: 基于表结构的权限 test.t1
mysql.columns_priv: 基于列结构的权限 列
mysql.procs_priv: 基于存储过程结构的权限
(7).回收权限
MySQL不同通过重复授权的方式修改权限,只能通过回收权限方式进行修改
mysql
revoke create on test.* to test@'192.168.0.%';

扩展:超级管理员密码忘记解决方案
--skip-grant-tables :跳过授权表
--skip-networking :跳过TCP/IP连接
1.关掉数据库
shell
systemctl stop mysqld
2.使用安全模式启动
shell
service mysqld start --skip-grant-tables --skip-networking
#或者
mysqld_safe --skip-grant-tables --skip-networking &

3.登录数据库并修改密码
mysql> alter user root@'localhost' identified by 'admin123';
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
mysql> flush privileges; #手工加载授权表
Query OK, 0 rows affected (0.00 sec)
mysql> alter user root@'localhost' identified by 'admin123';
Query OK, 0 rows affected (0.00 sec)
4.重启数据库
shell
service mysqld restart
连接管理
1.mysql自带客户端
mysql:
参数列表:
-u 用户名
, -p 密码
,-S 本地socket文件位置
,-h 数据库IP地址
,
-P 数据库端口号
,-e 免交互执行数据库命令
,< 导入sql脚本

Socket:
前提:数据中必须实现授权robin@'lcoalhost'用户 也就是哪个localhost授权了就可以登录
shell
mysql -urobin -padmin123 -S /tmp/mysql.sock
mysql -urobin -p -S /tmp/mysql.sock
mysql -padmin123 -S /tmp/mysql.sock #root用户
mysql #默认会去找/tmp/mysql.sock
mysql -uroot -padmin123
TCP/IP:
前提:必须提前创建好可以远程连接的用户(例如:robin@'192.168.0.%')
shell
mysql -urobin -p -h 192.168.0.130 -P 3306
MySQL区分连接是远程还是本地连接
mysql
show processlist;

MySQL免交互运行命令
shell
mysql -uroot -padmin123 -e "select @@datadir"

MySQL导入SQL脚本
shell
yum install -y lrzsz
rz -e
mysql -uroot -p < /opt/my.sql



初始化配置
1.三种方式:
源码安装------>编译过程中设置初始化参数
配置文件------>数据库启动之前,设定配置文件参数 /etc/my.cnf
启动脚本命令行
2.配置文件读取顺序
/etc/my.cnf ---->/etc/mysql/my.cnf ----> /usr/local/mysql/etc/my.cnf ---->~/.my.cnf
shell
mysqld --help --verbose | grep my.cnf

3.配置
[服务器端] :影响数据库服务端运行
[mysqld]
[mysql_safe]
[server] :代表服务端的所有
[客户端]:影响本地客户端连接,不影响远程客户端
[mysql]
[mysqldump]
[client] :代表客户端的所有
启动关闭
启动
systemctl start mysqld ------>mysql.server ------>mysql_safe ------>mysqld
mysqld_safe和mysqld,可以在启动数据库时,加入自己执行的参数,例如--defaults-file=/usr/lcoal/mysql/etc/my.cnf
关闭
shell
systemctl stop mysqld
service mysqld stop
/etc/init.d/mysqld stop
mysqladmin -uroot -padmin123 shutdwon
mysql -uroot -padmin123 -e "shutdwon"
多实例
一.同版本多实例
(1).规划
配置文件 3份 :/data/mysql/330{7...9}/my.cnf
数据目录 3份:/data/mysql/330{7...9}/data
日志目录 3份:/data/mysql/330{7...9}/binlog
Socket 3份: /tmp/mysql330{7...9}.sock
端口 3份:prot=3307,3308,3309
server_id 3份:server_id=7,8,9
(2).配置过程
1.创建需要的目录
shell
mkdir -p /data/mysql/330{7..9}/{data,binlog}
2.创建配置文件
shell
cat>/data/mysql/3307/my.cnf<<EOF
[mysqld]
basedir=/usr/local/mysql
datadir=/data/mysql/3307/data
socket=/tmp/mysql3307.sock
log_error=/data/mysql/3307/mysql.log
port=3307
server_id=7
log_bin=/data/mysql/3307/binlog
EOF
cat>/data/mysql/3308/my.cnf<<EOF
[mysqld]
basedir=/usr/local/mysql
datadir=/data/mysql/3308/data
socket=/tmp/mysql3308.sock
log_error=/data/mysql/3308/mysql.log
port=3308
server_id=8
log_bin=/data/mysql/3308/binlog
EOF
cat>/data/mysql/3309/my.cnf<<EOF
[mysqld]
basedir=/usr/local/mysql
datadir=/data/mysql/3309/data
socket=/tmp/mysql3309.sock
log_error=/data/mysql/3309/mysql.log
port=3309
server_id=9
log_bin=/data/mysql/3309/binlog
EOF
3.初始化数据
shell
mv /etc/my.cnf /etc/my.cnf.bak
#World-writable config file '/data/mysql/3307/my.cnf' is ignored.
#mysql担心这种文件被其他用户恶意修改,所以忽略掉这个配置文件
chmod 644 /data/mysql/330{7..9}/my.cnf
chown -R mysql.mysql /data/mysql
mysqld --initialize-insecure --user=mysql --datadir=/data/mysql/3307/data --basedir=/usr/local/mysql
mysqld --initialize-insecure --user=mysql --datadir=/data/mysql/3308/data --basedir=/usr/local/mysql
mysqld --initialize-insecure --user=mysql --datadir=/data/mysql/3309/data --basedir=/usr/local/mysql
4.准备启动脚本
shell
cat>/usr/lib/systemd/system/mysqld3307.service<<EOF
[Unit]
Description=MySQL Server 7
Documentation=man:mysqld(7)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/3307/my.cnf
LimitNOFILE = 5000
EOF
cat>/usr/lib/systemd/system/mysqld3308.service<<EOF
[Unit]
Description=MySQL Server 8
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/3308/my.cnf
LimitNOFILE = 5000
EOF
cat>/usr/lib/systemd/system/mysqld3309.service<<EOF
[Unit]
Description=MySQL Server 9
Documentation=man:mysqld(9)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/3309/my.cnf
LimitNOFILE = 5000
EOF
5.启动多实例
shell
systemctl start mysqld3307
systemctl start mysqld3308
systemctl start mysqld3309
6.启动完成
shell
netstat -tulnp | grep mysqld

二.不同版本多实例
1.下载
shell
mkdir -p /usr/local/src/{mysql56,mysql80}
wget -P /usr/local/src/mysql56 https://repo.huaweicloud.com/mysql/Downloads/MySQL-5.6/mysql-5.6.51-linux-glibc2.12-x86_64.tar.gz
cd /usr/local/src/mysql56
tar -zxvf mysql-5.6.51-linux-glibc2.12-x86_64.tar.gz
ln -s /usr/local/src/mysql56/mysql-5.6.51-linux-glibc2.12-x86_64 /usr/lcoal/mysql56
wget -P /usr/local/src/mysql80 https://repo.huaweicloud.com/mysql/Downloads/MySQL-8.0/mysql-8.0.23-linux-glibc2.12-x86_64.tar
cd /usr/local/src/mysql80
tar -xvf mysql-8.0.23-linux-glibc2.12-x86_64.tar
ln -s /usr/local/src/mysql56/mysql-8.0.23-linux-glibc2.12-x86_64
vim /etc/profile
#注释掉mysql
source /etc/profile
# 打开新窗口 因为source没用
mkdir -p /data/mysql/331{7..9}/{data,binlog}
chown -R mysql.mysql /data/mysql/
2.准备配置文件
shell
cat>/data/mysql/3317/my.cnf<<EOF
[mysqld]
basedir=/usr/local/mysql56
datadir=/data/mysql/3317/data
socket=/tmp/mysql3317.sock
log_error=/data/mysql/3317/mysql.log
port=3317
server_id=17
log_bin=/data/mysql/3317/binlog
EOF
cat>/data/mysql/3318/my.cnf<<EOF
[mysqld]
basedir=/usr/local/mysql80
datadir=/data/mysql/3318/data
socket=/tmp/mysql3318.sock
log_error=/data/mysql/3318/mysql.log
port=3318
server_id=18
log_bin=/data/mysql/3318/binlog
EOF
3.初始化数据
shell
#5.6
/usr/local/mysql56/scripts/mysql_install_db --user=mysql --datadir=/data/mysql/3317/data --basedir=/usr/local/mysql56
/usr/local/mysql80/bin/mysqld --initialize-insecure --user=mysql --datadir=/data/mysql/3318/data --basedir=/usr/lcoal/mysql80
#5.6报错
Installing MySQL system tables.../usr/local/mysql56/bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
yum install -y libaio
4.准备启动脚本
shell
cat>/usr/lib/systemd/system/mysqld3317.service<<EOF
[Unit]
Description=MySQL Server 17
Documentation=man:mysqld(17)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql56/bin/mysqld --defaults-file=/data/mysql/3317/my.cnf
LimitNOFILE = 5000
EOF
cat>/usr/lib/systemd/system/mysqld3318.service<<EOF
[Unit]
Description=MySQL Server 18
Documentation=man:mysqld(18)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql80/bin/mysqld --defaults-file=/data/mysql/3318/my.cnf
LimitNOFILE = 5000
EOF

5**.启动mysql5.7和测试**
shell
vim /etc/profile
#还原mysql配置
source /etc/profile
mv /etc/my.cnf.bak /etc/my.cnf
/etc/init.d/mysqld start
mysql -S /tmp/mysql3318.sock
