实验环境
master 192.168.132.121 主库
slave 192.168.132.122 从库
一 mysql的使用介绍
1.1 mysql单台服务器特点
缺点
单台服务器如果磁盘出现问题的话,数据库数据就会丢失
单台服务器的备份,如果把数据文件备份到本地的话,难以自动定期备份
单台服务器无法应对高并发的读,只有单台的话,读写都在同一台,数据压力大
优点
数据不会出现不一致的现象
维护简单
1.2 mysql单台服务器推荐方法
如果确实只有单台服务器的话,可考虑采用单台服务器 + 云快照或者云存储
1.3 mysql多台服务器实现主从复制特点
主从复制优点
多台服务器实现正常的主从复制后,多台服务器的mysql数据一致,数据丢失可能性小
高并发的读压力可以分配到多台服务器上
主从复制缺点
复制出问题或者写量比较大的时候,数据可能会不一致
主从复制维护比较麻烦
1.4 主从复制原理
mysql主从复制原理 binlog
mysql主从复制依赖于mysql二进制日志文件,主mysql服务器会生成二进制日志文件,传递给从服务器执行
mysql从库同步有两个进程,一个io进程用来同步binlog,一个sql进程用来执行relaylog(中继日志)里的数据
目前mysql主从架构还是比较常见的,数据的重要性
1.5 复制步骤
整体上来说,复制有3 个步骤:
(1)master 将改变记录到二进制日志(binary log)中(这些记录叫做二进制日志事件,binary log events);
(2)slave 将master 的binary log events 拷贝到它的中继日志(relay log);
(3)slave 重放中继日志中的事件,将改变反映它自己的数据。
1.6 复制架构
Datagaurd 主读写,备读不能写
1.从服务器作为主服务器的实时数据备份
2.主从服务器实时读写分离,从服务器实现负载均衡
3.把多个从服务器根据业务重要性进行拆分访问
1.7 MySQL 主从复制企业应用场景:
1. 从服务器作为主服务器的实时数据备份
当主服务器出现问题时,我们可以人工或设置自动切换到从服务器继续提供服务,此时从服务器的数据和宕机时的主数据库几乎是一致的。
对于人为地执行drop、delete 等语句删除数据的情况,从库的备份功能就没有用了,因为从服务器也会执行删除的语句。
2. 主从服务器实时读写分离,从服务器实现负载均衡
主从服务器架构可通过程序(Javaweb 等)或代理软件(mysql‐proxy、mycat 等)实现对用户(客户端)的请求读写分离,即让从服务器仅仅处理用户的select 查询请求,降低用户查询响应时间及读写同时在主服务器上带来的访问压力。对于更新的数据(例如update、insert、delete 语句)仍然交给主服务器处理,确保主服务器和从服务器保持实时同步。
3. 把多个从服务器根据业务重要性进行拆分访问可以把几个不同的从服务器,根据公司的业务进行拆分。
二 mysql主库的配置
2.1 先对数据初始化,生成密码
[root@master ~]# mysqld --initialize --user=mysql
[root@master ~]# grep "password" /var/log/mysqld.log
2019-07-03T15:37:29.068299Z 1 [Note] A temporary password is generated for root@localhost: 0JlLA_qy1V:=
2.2 启动MySQL
[root@master ~]# systemctl start mysqld
[root@master ~]# netstat -ntlp
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1053/sshd tcp6 0 0 :::22 :::* LISTEN 1053/sshd tcp6 0 0 :::3306 :::* LISTEN 8263/mysqld
2.3 修改配置
需要新增二进制日志binlog配置
[root@master ~]# vim /etc/my.cnf
[mysqld] bind-address=0.0.0.0 port=3306 datadir=/data/mysql socket=/data/mysql/mysql.sock user=mysql skip-name-resolve slow_query_log=on long_query_time=1 slow_query_log_file=/data/mysql/mysql-slow.log innodb-file-per-table=1 innodb_flush_log_at_trx_commit = 2 log_warnings = 1 connect_timeout = 60 net_read_timeout = 120 performance_schema_max_table_instances = 400 server-id = 1 log-bin=master-bin #开启二进制日之 log-bin-index = master-bin.index #二进制日志的索引目录 [mysqld_safe] log-error=/data/mysql/mysqld.log pid-file=/data/mysql/mysqld.pid
server-id #binlog配置需要给mysql服务器设置id用来标识mysql服务器,每个mysql服务器都有不一样的id。从1开始标识
log-bin #开启binlog,mysql主服务器都需要开启
log-bin-index #binlog存放到文件里面,index里面存放着binlog的文件名
2.4 创建数据目录
[root@master ~]# mkdir -pv /data/mysql
[root@master ~]# chown mysql:mysql /data/mysql/
2.5 重启服务,并验证
[root@master ~]# systemctl start mysqld
报错机排错,查看日志,错误日志如下
2019-07-03T15:08:32.203843Z 0 [ERROR] InnoDB: Operating system error number 13 in a file operation. 2019-07-03T15:08:32.203862Z 0 [ERROR] InnoDB: The error means mysqld does not have the access rights to the directory. 2019-07-03T15:08:32.203871Z 0 [ERROR] InnoDB: os_file_get_status() failed on './ibdata1'. Can't determine file permissions 2019-07-03T15:08:32.203886Z 0 [ERROR] InnoDB: Plugin initialization aborted with error Generic error 2019-07-03T15:08:32.808009Z 0 [ERROR] Plugin 'InnoDB' init function returned error. 2019-07-03T15:08:32.808138Z 0 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed. 2019-07-03T15:08:32.808162Z 0 [ERROR] Failed to initialize builtin plugins. 2019-07-03T15:08:32.808172Z 0 [ERROR] Aborting
显示是没有权限,但是已经开启了/data/mysql的权限
关掉selinux,在启动,成功,是selinux的问题
[root@master ~]# getenforce Enforcing [root@master ~]# setenforce 0 [root@master ~]# getenforce Permissive [root@master ~]# systemctl start mysqld
验证启动
[root@master ~]# netstat -ntlp|grep mysql tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 10165/mysqld
使用密码不能登陆
[root@master src]# mysql -uroot -h localhost -p0JlLA_qy1V:= mysql: [Warning] Using a password on the command line interface can be insecure. ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) #这个socket不在 [root@master src]# ll /data/mysql/ total 122936 -rw-r-----. 1 mysql mysql 56 Jul 3 11:37 auto.cnf -rw-r-----. 1 mysql mysql 419 Jul 3 11:37 ib_buffer_pool -rw-r-----. 1 mysql mysql 12582912 Jul 3 11:37 ibdata1 -rw-r-----. 1 mysql mysql 50331648 Jul 3 11:37 ib_logfile0 -rw-r-----. 1 mysql mysql 50331648 Jul 3 11:37 ib_logfile1 -rw-r-----. 1 mysql mysql 12582912 Jul 3 11:37 ibtmp1 -rw-r-----. 1 mysql mysql 177 Jul 3 11:37 master-bin.000001 -rw-r-----. 1 mysql mysql 154 Jul 3 11:37 master-bin.000002 -rw-r-----. 1 mysql mysql 40 Jul 3 11:37 master-bin.index drwxr-x---. 2 mysql mysql 4096 Jul 3 11:37 mysql -rw-r-----. 1 mysql mysql 350 Jul 3 11:37 mysql-slow.log srwxrwxrwx. 1 mysql mysql 0 Jul 3 11:37 mysql.sock #查看在这里 -rw-------. 1 mysql mysql 6 Jul 3 11:37 mysql.sock.lock drwxr-x---. 2 mysql mysql 8192 Jul 3 11:37 performance_schema drwxr-x---. 2 mysql mysql 8192 Jul 3 11:37 sys [root@master src]# ln -s /data/mysql/mysql.sock #做一个软连接,成功 mysql.sock mysql.sock.lock [root@master src]# ln -s /data/mysql/mysql.sock /var/lib/mysql/mysql.sock [root@master src]# mysql -uroot -h localhost -p0JlLA_qy1V:=
查看数据库,提示必须改密码
mysql> show databases; ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
修改密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456'; Query OK, 0 rows affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec)
验证成功
三 启动从库
方式相同
3.1 mysql从库新增中继日志relaylog配置
[mysqld] bind-address=0.0.0.0 port=3306 datadir=/data/mysql socket=/data/mysql/mysql.sock user=mysql skip-name-resolve slow_query_log=on long_query_time=1 slow_query_log_file=/data/mysql/mysql-slow.log innodb-file-per-table=1 innodb_flush_log_at_trx_commit = 2 log_warnings = 1 connect_timeout = 60 net_read_timeout = 120 performance_schema_max_table_instances = 400 server-id = 2 relay-log = relay-log relay-log-index = relay-log.index [mysqld_safe] log-error=/data/mysql/mysqld.log pid-file=/data/mysql/mysqld.pid
配置说明
server-id #每台mysql服务器的标识需要不一样
relay-log #从库需要开启relay-log,中继日志
relay-log-index #relaylog也放到文件里面,index里存放着relaylog的文件名
3.2 启动mysql从库
[root@slave ~]# mkdir -pv /data/mysql mkdir: created directory ‘/data’ mkdir: created directory ‘/data/mysql’ [root@slave ~]# chown mysql:mysql /data/mysql [root@slave ~]# getenforce Enforcing [root@slave ~]# setenforce 0 [root@slave ~]# getenforce Permissive [root@slave ~]# mysqld --initialize --user=mysql 2019-07-03T15:56:10.701329Z 0 [Warning] The syntax '--log_warnings/-W' is deprecated and will be removed in a future release. Please use '--log_error_verbosity' instead. 2019-07-03T15:56:10.701519Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2019-07-03T15:56:11.241974Z 0 [Warning] InnoDB: New log files created, LSN=45790 2019-07-03T15:56:11.327038Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2019-07-03T15:56:11.398579Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 141defd2-9dab-11e9-8fe3-000c2963fd11. 2019-07-03T15:56:11.401121Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2019-07-03T15:56:11.426433Z 1 [Note] A temporary password is generated for root@localhost: xJ,RO+8krdGB [root@slave ~]# systemctl start mysqld [root@slave ~]# mysql -u root -pxJ,RO+8krdGB mysql: [Warning] Using a password on the command line interface can be insecure. ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) [root@slave ~]# ln -s /data/mysql/mysql.sock /var/lib/mysql/mysql.sock [root@slave ~]# mysql -u root -pxJ,RO+8krdGB mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or g. ...... Type 'help;' or 'h' for help. Type 'c' to clear the current input statement. mysql> show databases; ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement. mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456'; Query OK, 0 rows affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec) mysql> exit
从库配置完成
两台数据库独立的,但binlog文件已经有,由于主从还没有搭建,所以还没有relaylog
3.3 二进制日志
查看日志状态
show variables like '%log_bin%';
mysql> show variables like '%log_bin%'; +---------------------------------+------------------------------+ | Variable_name | Value | +---------------------------------+------------------------------+ | log_bin | ON | #开启状态 | log_bin_basename | /data/mysql/master-bin | | log_bin_index | /data/mysql/master-bin.index | | log_bin_trust_function_creators | OFF | | log_bin_use_v1_row_events | OFF | | sql_log_bin | ON | +---------------------------------+------------------------------+
[root@master ~]# ll /data/mysql/
master-bin.index记录的日志名字
[root@master ~]# cat /data/mysql/master-bin.index