mariadb 学习笔记
安装:
yum install mariadb-server mariadb
vim /etc/my.cnf.d/server.cnf
innodb_file_per_table = on
#设置后当创建数据库的表的时候表文件都会分离开,方便复制表,不开启创建的表都在一个文件
skip_name_resolve = on
#跳过名称反解,Mysql每次使用客户端链接时都会把ip地址反解成主机名
连接字符串参考:
"server=127.0.0.1;uid=root;pwd=12346;databaser=test"
创建数据库:
create databasers jumpserver character set uft8mb4 collate uft8mb4_general_ci;
character set 指定字符集, uft8mb4是utf8的扩展,支持4字节,版本需要5.5.+
collate 指定字符集的校对规则,用来做字符串的比较的。例如 a 和 A 谁大?
create database jumpserver default charset 'utf8';
grant all on jumpserver.* to 'jumpuser'@'192.168.1.%' identified by 'khb123';
授权:
grant all on 表名.* to 'root'@'%' identified by '用户'; all-表示增删改查;
撤消用户权限:
revoke all on 表名.* from 用户名;
删除用户:
drop user 用户名;
增加数据语句:
insert into table_name (col_name,...) values(value1,...);
更新数据语句:
update table_name set col_name=expr1 where 加条件;
删除数据语句:
delete from table_name where 条件;
为更新做独占锁: for update
select * from user for update; *:表级别
commit; 提交后释放锁
Pymsql
connection初始化常用参数:
host 主机
user 用户名
password 密码
databaser 数据库
port 端口
编译安装mariadb
准备环境:
[root@localhost yum.repos.d]# yum groupinstall "Development Tools" -y
[root@localhost yum.repos.d]# yum install ncurses-devel openssl-devel libevent-devel jemalloc-devel cmake #jemalloc-devel需要EPEL源
MariaDB [(none)]> HELP SHOW #查看SHOW帮助
mariadb日志
1.查询日志 query log #一般不开启,影响性能
MariaDB [(none)]> show global variables like '%log%';
general_log | OFF #是否开启
general_log_file | localhost.log # 路径,可以修改 默认主机名.log
log_output | FILE #日志输出 TABLE/FILE/NONE
2.慢查询日志 slow query log #一般全局启用
MariaDB [(none)]> show global variables like '%long%'; #查看当前时常
long_query_time | 10.000000
MariaDB [(none)]> show global variables like '%log%';
slow_query_log | OFF #是否开启
log_slow_queries | OFF #是否开启 一般用上面选项控制开关
slow_query_log_file | localhost-slow.log
log_slow_filter | admin,filesort,filesort_on_disk,full_join,full_scan,query_cache,query_cache_miss,tmp_table,tmp_table_on_disk #只记录定义
log_slow_rate_limit | 1 #速率控制,每秒一个
log_slow_verbosity | #记录级别
set global long_query_time= 20 #重启失效,永久写到配置文件中,定义参考时长
3.错误日志 error log #记录mysqld启动和关闭过程中输出的事件信息,运行中产生的错误信息,event scheduler运行一个event时产生的日志信息
主从复制架构中的从服务器上启动从服务器线程时产生的信息;
log_error | /var/log/mariadb/mariadb.log #默认安装位置
log_warnings | 1 #是否记录警告信息到错误日志中 1-记录
4.二进制日志 binary log #记录导致数据改变的语句
MariaDB [(none)]> SHOW BINARY LOGS #查看二进制日志
SHOW MASTER STATUS #当前使用的二进制日志
二进制日志记录格式:基于语句记录:statement
基于行记录:row
混合模式:mixed #让系统自行判定
二进制日志文件的构成: 两类
日志文件:mysql-bin.文件名后缀 #二进制格式
索引文件:mysql-bin.index #文本格式
服务器变量:
sql_log_bin | ON #是否开启
log_bin | OFF #通常为ON
binlog_format | STATEMENT #记录格式
max_binlog_size | 1073741824 #滚动日志大小设置 默认为1G
max_binlog_cache_size | 18446744073709547520 #
max_binlog_stmt_cache_size | 18446744073709547520 #
expire_logs_days | 0 #二进制日志过期时长,0-不启动功能
sync_binlog | 0 #0-不启用二进日志同步功能 1-启用
mysqlbinlog :客户端命令工具
5.中继日志 reley log
复制架构中,从服务器用于保存从主服务器的二进制日志中读取到的事件
6.事务日志 transaction log
事务型存储引擎自行管理和使用
分组进行,一般两个循环进行同步
MariaDB [(none)]> show global variables like '%innodb_log%';
+---------------------------+---------+
| Variable_name | Value |
+---------------------------+---------+
| innodb_log_block_size | 512 |
| innodb_log_buffer_size | 8388608 |
| innodb_log_file_size | 5242880 |
| innodb_log_files_in_group | 2 |
| innodb_log_group_home_dir | ./ | ./为数据目录
+---------------------------+---------+
数据库备份
逻辑备份:与存储引擎无关
备份进需要考虑的因素:
持锁时长
备份过程的时长
备份时产生的负载
恢复过程的时长
备份哪些东西
数据
二进制日志,InnoDB的事务日志
代码(存储过程,存储函数,触发器,事件调度器
服务器的配置文件
备份工具
mysqldump :逻辑备份工具,适用所有存储引擎
cp ,tar 等复制归档工具:物理备份工具,适用所有存储引擎,冷备
lvm2的快照:施加一个全局锁,在一个快照,然后释放锁,借助于文件系统管理是工具进行备份
mysqlhotcopy:冷备工具,仅适于myisam存储引擎
方案:mysqldump+复制binlog
mysqldump完全备份
复制指定时间范围的event,增量备份
xtrabackup:支持对Innodb做执备(物理备份) ,完全备份,增量备份
mysqldump [options] [db_name [tbl_name ...]]
-A --all-databases
-B --databases
shell> mysqldump [options] db_name [tbl_name ...] #不会创建表名,恢复时要手动创建表
shell> mysqldump [options] --databases db_name ...
shell> mysqldump [options] --all-databases
--master-date=2 #记录为注释的change master to 语句
--flush-logs #锁定表完成后,执行flush logs命令
[root@localhost src]# yum install xtrabackup
数据库:在执行还原操作时,建议把二进制日志临时关掉。
mysql复制:
主从复制:
从节点:
I/O Thread 从master请求二进制日志事件,并保存于中继日志中
SQL Thread 从中继日志中读取日志事件,在本地完成重放
主节点:
dump Thread 为每个slave的I/O Thread启动一个dump线程,用于向其发送binary log events
特点:
异步复制的
主从配置过程:
主节点:
启动二进制日志
[mysqld]
log_bin=mysql-bin
为当前主节点设置一个全局唯一的ID号
[mysqld]
server_id=#
创建有复制权限的用户账号 --replication slave --replication client
grant replication slave,replication client on *.* to 'xxxx'@'xxxx' identified by 'xxxx';
从节点:
启动中继日志
[mysqld]
relay_log=relay-log
relay_log_index=relay-log.index
为当前主节点设置一个全局唯一的ID号
[mysqld]
server_id=#
使用有复制权限的用户连接至主服务器,并启动复制线程
change master to MASTER_HOST='xxxx',MASTER_USER='xxx',MASTER_PASSWORD='xxxx',
MASTER_LOG_FILE='master-bin.xxxx',MASTER_LOG_POS=#;
MariaDB [(none)]> START SLAVE
注意:如果主节点已经运行一段时间,且有大量数据时,如何配置配置并启动slave节点
1.通过备份恢复数据到从节服务器
2.复制超始位置为备份时的二进制日志文件及其POS
主节点:
[root@localhost ~]# cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
log_bin=master-bin
server_id=1
innodb_file_per_table=ON
skip_name_resolve=ON
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
MariaDB [(none)]> show global variables like '%log%';
log_bin | ON
MariaDB [(none)]> show master logs;
+-------------------+-----------+
| Log_name | File_size |
+-------------------+-----------+
| master-bin.000001 | 245 |
+-------------------+-----------+
MariaDB [(none)]> show global variables like '%server%';
+----------------------+-------------------+
| Variable_name | Value |
+----------------------+-------------------+
| character_set_server | latin1 |
| collation_server | latin1_swedish_ci |
| server_id | 1 |
+----------------------+-------------------+
MariaDB [(none)]> grant replication slave,replication client on *.* to 'repluser'@'172.16.%.%' identified by 'replpass';
MariaDB [(none)]> flush privileges;
从节点:
[root@localhost ~]# cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
relay_log=relay-log
relay_log_index=relay-log.index
server_id=8
innodb_file_per_table=ON
skip_name_resolve=ON
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
root@localhost ~]# systemctl start mariadb.service
MariaDB [(none)]> help change master to
Name: 'CHANGE MASTER TO'
Description:
Syntax:
CHANGE MASTER TO option [, option] ...
option:
MASTER_BIND = 'interface_name'
| MASTER_HOST = 'host_name'
| MASTER_USER = 'user_name'
| MASTER_PASSWORD = 'password'
| MASTER_PORT = port_num
| MASTER_CONNECT_RETRY = interval
| MASTER_HEARTBEAT_PERIOD = interval
| MASTER_LOG_FILE = 'master_log_name' ------------------
| MASTER_LOG_POS = master_log_pos ------------------
| RELAY_LOG_FILE = 'relay_log_name'
| RELAY_LOG_POS = relay_log_pos
| MASTER_SSL = {0|1}
| MASTER_SSL_CA = 'ca_file_name'
| MASTER_SSL_CAPATH = 'ca_directory_name'
| MASTER_SSL_CERT = 'cert_file_name'
| MASTER_SSL_KEY = 'key_file_name'
| MASTER_SSL_CIPHER = 'cipher_list'
| MASTER_SSL_VERIFY_SERVER_CERT = {0|1}
| IGNORE_SERVER_IDS = (server_id_list)
示例:
The following example changes the master server the slave uses and
establishes the master binary log coordinates from which the slave
begins reading. This is used when you want to set up the slave to
replicate the master:
CHANGE MASTER TO
MASTER_HOST='master2.mycompany.com',
MASTER_USER='replication',
MASTER_PASSWORD='bigs3cret',
MASTER_PORT=3306,
MASTER_LOG_FILE='master2-bin.001',
MASTER_LOG_POS=4,
MASTER_CONNECT_RETRY=10;
MariaDB [(none)]> change master to MASTER_HOST='172.16.16.129',MASTER_USER='repluser',MASTER_PASSWORD='replpass',
MASTER_LOG_FILE='master-bin.000001',MASTER_LOG_POS=245;
MariaDB [(none)]> show slave status\G #查看从节点状态值
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 172.16.16.129
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000001
Read_Master_Log_Pos: 245
Relay_Log_File: relay-log.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: master-bin.000001
Slave_IO_Running: No
Slave_SQL_Running: No
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 245
Relay_Log_Space: 245
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 0
MariaDB [(none)]> START SLAVE #启动复制线程
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.16.16.129
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000001
Read_Master_Log_Pos: 496
Relay_Log_File: relay-log.000002
Relay_Log_Pos: 781
Relay_Master_Log_File: master-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 496
Relay_Log_Space: 1069
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
复制架构中要注意的问题:
1.限制从服务器上为只读
在从服务器上设置read_only=ON ,此限制对super权限的用户均不效
或阴止所有用户
mysql> fulsh tables with read lock;
2.如何保证主从复制的事务安全
在master节点启用参数
sync_binlog=ON
对于Innodb引擎:
innodb_flush_logs_at_trx_commit=ON #及时在事务日志缓存中的数据刷写到事务日志中
innodb_support_xa=ON #支持分布式事务
在slave节点:
skip_slave_start=ON #跳过,手动来启动
在主节点:
sync_master_info=ON #会增加开销,视情况而定
在从节点:
sync_relay_log #会增加开销,视情况而定
sync_relay_log_info #会增加开销,视情况而定