https://www.jianshu.com/p/6d017ac5e685
本课程,适合具备一定Linux运维或者开发基础的朋友,课程定级中、高级DBA。
只要掌握80%,轻松助力薪资15k-25K。
课程内容均来自与MySQL官网+MySQL源码。
配套精品视频(2021 5月全新录制,版权所有:郭加磊 oldguo。),获取方法私聊。
Clinet : mysql mysqldump ,sqlyog,API
Server : mysqld守护进程
实例: mysqld + 线程(Master thread , IO ,SQL ,purge...) + 预分配内存(数据\日志\线程)
公司: boss + 员工(总经理+普通员工) + 办公室
库 : 库名,库属性(字符集,校对规则)
表 : 列+行+表属性+表名
库---> 目录
表---> xx.ibd ---> 区(extents)---->页(pages)
登陆数据库
管理数据库对象
名字@'白名单'
白名单? ----> 在白名单中的IP才能连MySQL
oldguo@'localhost' --> 本地用户
oldguo@'10.0.0.2' --> 单一IP
oldguo@'10.0.0.%' --> 范围IP
oldguo@'10.0.0.5%' --> 范围IP
oldguo@'10.0.0.0/255.255.254.0' --> 范围IP
oldguo@'%' --> 范围IP
a. 白名单尽量小,最好细化到单一IP, %要不得.
b. 用户名有特点.
c. 无用的用户要删除或者锁定.
d. 密码超过三种复杂度,12位以上.
mysql> desc mysql.user;
mysql> select user,host ,authentication_string ,plugin from mysql.user;
mysql> select user as "用户",host as "白名单" ,authentication_string as "密码",plugin as "插件" from mysql.user;
mysql> select user as "用户",host as "白名单" ,plugin as "插件" from mysql.user;
mysql> create user oldboy@'10.0.0.%' identified by '123';
mysql> create user oldguo@'10.0.0.%' identified with mysql_native_password by '123';
命令不会,看帮助.
mysql> help create user;
- 8.0 之后必须先建用户后授权,grant不再支持建用户功能和密码修改功能;
- 密码插件,8.0之前使用mysql_native_password,8.0 之后使用caching_sha2_password
- 导致的问题: 使用老的客户端程序,连接不了8.0版本
解决方法:
a. 建用户时,指定mysql_native_password插件进行密码加密.
b. 修改用户时,可以修改插件
c. 配置文件中指定默认加密插件为mysql_native_password
mysql> alter user oldboy@'10.0.0.%' identified with mysql_native_password by '123';
mysql> select user as "用户",host as "白名单" ,plugin as "插件" from mysql.user;
mysql> alter user oldboy@'10.0.0.%' ACCOUNT LOCK;
mysql> alter user oldboy@'10.0.0.%' ACCOUNT UNLOCK;
mysql> drop user oldboy@'10.0.0.%';
约束用户能够对数据库对象(库,表)干啥(SQL).
mysql> SHOW PRIVILEGES;
ALL ? 除了Grant option所有权限.
grant 权限 on 权限范围 to 用户;
权限范围?*.* 全局范围,包含了所有库表 chmod 777 -R /
wordpress.* 单库范围 chmod 777 -R /wordpress
wordpress.t1 单表
mysql> create user root@'10.0.0.%' identified with mysql_native_password by '123';
mysql> grant all on *.* to root@'10.0.0.%';
mysql> create user wp_user@'10.0.0.%' identified with mysql_native_password by '123';
mysql> grant select,update,delete,insert on wordpress.* to wp_user@'10.0.0.%';
mysql> show grants for wp_user@'10.0.0.%';
use mysql ;
user : user host auth plugin 全局授权(*.*)的权限
db : user host 单库范围授权(wordpress.* )的用户权限
table_priv : user host 单表范围授权(wordpress.t1)的用户权限
mysql> revoke delete ON `wordpress`.* from `wp_user`@`10.0.0.%`;
mysql> show grants for wp_user@'10.0.0.%';
需求
oldguo.* rw(insert,update,delete,select) --->oldguo_rw
oldguo.* r (select) --->oldguo_r
mysql> create role oldguo_rw,oldboy_r;
mysql> grant select on oldguo.* to oldboy_r;
mysql> grant select,update,insert,delete on oldguo.* to oldguo_rw;
mysql> create user user1@'%' identified by '123';
mysql> create user user2@'%' identified by '123';
mysql> grant oldguo_r to user1@'%';
mysql> grant oldguo_rw to user2@'%';
a. 重启数据库到无验证模式
[root@db01 ~]# /etc/init.d/mysqld stop
[root@db01 ~]# mysqld_safe --skip-grant-tables --skip-networking &
b. 登录改密码
[root@db01 ~]# mysql
mysql> flush privileges;
mysql> alter user root@'localhost' identified by '123';
c. 正常启动数据库
[root@db01 ~]# /etc/init.d/mysqld restart
(1) 本地 socket文件连接
条件: localhost用户需要提前创建.
[root@db01 ~]# mysql -uroot -p123 -S /tmp/mysql.sock
(2) 网络连接串(TCP/IP)
条件: 远程连接用户必须出现在白名单.
[root@db01 ~]# mysql -uoldguo -p123 -h10.0.0.51 -P3306
sqlyog
navicat
workbench
php
python
go
java
等。
a. 源码包,编译时配置一些参数(CMAKE)
b. 配置文件
c. 命令行指定
[root@db01 ~]# mysqld --help --verbose |grep my.cnf
/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cnf
注意:
a. 多个文件会依次从左到右读取,如果有重复,最后读取的生效.
b. 启动时,强制指定读取哪个配置文件(--defaults-file=/opt/aa.txt),mysqld mysqld_safe 程序能够调用
[mysqld]
user=mysql
basedir=/usr/local/mysql
datadir=/data/3306/data
port=3306
socket=/tmp/mysql.sock
[client]
socket=/tmp/mysql.sock
[标签] :
[服务端]: [mysqld] [mysqld_safe] [server]
影响: 数据库启动,初始化.
[客户端]: [mysql] [mysqldump] [client]
影响: 只影响到本机客户端程序运行.
mysqld &
mysqld_safe &
mysql.server start
service start systemd start
mysql.server stop
sys-v systemd stop
mysqladmin -uroot -p123 shutdown
shutdown
mkdir -p /data/330{7..9}/data
chown -R mysql. /data
cat >/data/3307/my.cnf <<EOF
[mysqld]
user=mysql
basedir=/usr/local/mysql
datadir=/data/3307/data
port=3307
socket=/tmp/mysql3307.sock
EOF
cat >/data/3308/my.cnf <<EOF
[mysqld]
user=mysql
basedir=/usr/local/mysql
datadir=/data/3308/data
port=3308
socket=/tmp/mysql3308.sock
EOF
cat >/data/3309/my.cnf <<EOF
[mysqld]
user=mysql
basedir=/usr/local/mysql
datadir=/data/3309/data
port=3309
socket=/tmp/mysql3309.sock
EOF
mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/3307/data
mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/3308/data
mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/3309/data
mysqld_safe --defaults-file=/data/3307/my.cnf &
mysqld_safe --defaults-file=/data/3308/my.cnf &
mysqld_safe --defaults-file=/data/3309/my.cnf &
cat >/etc/systemd/system/mysqld3307.service <<EOF
[Unit]
Description=MySQL Server
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/3307/my.cnf
LimitNOFILE = 5000
EOF
cat >/etc/systemd/system/mysqld3308.service <<EOF
[Unit]
Description=MySQL Server
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/3308/my.cnf
LimitNOFILE = 5000
EOF
cat >/etc/systemd/system/mysqld3309.service <<EOF
[Unit]
Description=MySQL Server
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/3309/my.cnf
LimitNOFILE =