mysql

mysql

mysql源码安装

```

####下载源码安装需要用到的软件

[root@mysql_1 ~]# yum install cmake gcc-c++ openssl-devel -y

[root@mysql_1 ~]# yum install ncurses-devel.x86_64 rpcgen.x86_64 -y

#导入libtirpc-devel-0.2.4-0.16.el7.x86_64.rpm

[root@mysql_1 ~]# yum install libtirpc-devel-0.2.4-0.16.el7.x86_64.rpm -y

##########

导入mysql-boost-5.7.44.tar.gz

[root@mysql_1 ~]# tar zxf mysql-boost-5.7.44.tar.gz

[root@mysql_1 ~]# cd mysql-5.7.44/

####源码编译安装mysql

[root@mysql_1 mysql-5.7.44]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_EXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8mb4 -DDEFAULT_COLLATION=utf8mb4_unicode_ci -DWITH_BOOST=/root/mysql-5.7.44/boost/boost_1_59_0/

[root@mysql_1 mysql-5.7.44]# make -j2 && make install #会很慢

```

部署mysql

[root@mysql-node1 ~]# mkdir /data/mysql -p

[root@mysql-node1 ~]# useradd -s /sbin/nologin -M mysql

[root@mysql-node1 ~]# chown -R mysql.mysql /data/mysql

[root@mysql-node1 ~]# cd /usr/local/mysql/support-files/

[root@mysql-node1 support-files]# cp mysql.server /etc/init.d/mysqld

[root@mysql-node1 support-files]# vim ~/.bash_profile

PATH=PATH:HOME/bin:/usr/local/mysql/bin

[root@mysql-node1 support-files]# source ~/.bash_profile

[root@mysql-node1 ~]# cat /etc/my.cnf

[mysqld]

datadir=/data/mysql

socket=/data/mysql/mysql.sock

symbolic-links=0

[root@mysql-node1 ~]# mysqld --user mysql --initialize #初始化

[root@mysql-node1 ~]# /etc/init.d/mysqld start

[root@mysql-node1 ~]# yum install chkconfig -y

[root@mysql-node1 ~]# mysql_secure_installation #重新设置密码

从一台已经源码安装好的快速安装另一台

[root@mysql-node1 ~]# yum install rsync -y #俩台都要安装

[root@mysql-node1 ~]# rsync -al -r mysql root@172.25.250.10:/usr/local

[root@mysql-node2 ~]# yum install rsync -y

[root@mysql-node2 ~]# mkdir /data/mysql -p

[root@mysql-node2 ~]# useradd -s /sbin/nologin -M mysql

[root@mysql-node2 ~]# chown -R mysql.mysql /data/mysql

cp mysql.server /etc/init.d/mysqld

[root@mysql-node1 support-files]# vim ~/.bash_profile

PATH=PATH:HOME/bin:/usr/local/mysql/bin

[root@mysql-node2 support-files]# source ~/.bash_profile

[root@mysql-node2 ~]# mysqld --user mysql --initialize

[root@mysql-node2 ~]# cd /usr/local/mysql/

[root@mysql-node2 mysql]# cd s

share/ support-files/

[root@mysql-node2 mysql]# cd s

share/ support-files/

[root@mysql-node2 mysql]# cd support-files/

[root@mysql-node2 support-files]# cp mysql.server /etc/init.d/mysql

[root@mysql-node2 support-files]# /etc/init.d/mysql start

主从复制

在node1

[root@mysql-node1 ~]# vim /etc/my.cnf

[root@mysql-node1 ~]# /etc/init.d/mysql restart

[mysqld]

datadir=/data/mysql

socket=/data/mysql/mysql.sock

symbolic-links=0

log-bin=mysql-bin

server-id=1

#进入数据库配置用户权限

[root@mysql-node1 ~]# mysql -p123

mysql> CREATE USER 'repl'@'%' IDENTIFIED BY '123'

-> ;

Query OK, 0 rows affected (0.00 sec)

mysql>

mysql> GRANT REPLICATION SLAVE ON *.* TO repl@'%';

Query OK, 0 rows affected (0.00 sec)

mysql> show master status;

+------------------+----------+--------------+------------------+-------------------+

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

+------------------+----------+--------------+------------------+-------------------+

| mysql-bin.000001 | 350 | | | |

+------------------+----------+--------------+------------------+-------------------+

1 row in set (0.00 sec)

mysql> exit

Bye

[root@mysql-node1 ~]# cd /data/mysql/

[root@mysql-node1 mysql]# mysql

mysql mysql_config_editor mysqlimport mysql_ssl_rsa_setup

mysqladmin mysqld mysql_install_db mysqltest

mysqlbinlog mysqld_multi mysql_plugin mysqltest_embedded

mysqlcheck mysqld_safe mysqlpump mysql_tzinfo_to_sql

mysql_client_test mysqldump mysql_secure_installation mysql_upgrade

mysql_client_test_embedded mysqldumpslow mysqlshow mysqlxtest

mysql_config mysql_embedded mysqlslap

[root@mysql-node1 mysql]# mysqlbinlog mysql-bin.000001 -vv #查看二进制文件

配置salve

[root@mysql-node2 ~]# vim /etc/my.cnf

[root@mysql-node2 ~]# /etc/init.d/mysql restart

Shutting down MySQL. SUCCESS!

Starting MySQL. SUCCESS!

[root@mysql-node2 ~]# cat /etc/my.cnf

[mysqld]

datadir=/data/mysql

socket=/data/mysql/mysql.sock

symbolic-links=0

server-id=2

mysql> CHANGE MASTER TO CHANGE MASTER TO MASTER_HOST='172.25.250.10',MASTER_USER='repl',MASTER_PASSWORD='123',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=350

mysql> start slave;

mysql> SHOW SLAVE STATUS\G;

mysql> CREATE DATABASE ll;

Query OK, 1 row affected (0.00 sec)

mysql> CREATE TABLE ll.userlist(

-> username varchar(20) not null,

-> password varchar(50) not null

-> );

Query OK, 0 rows affected (0.01 sec)

mysql> INSERT INTO ll.userlist VALUE('liming','123');

Query OK, 1 row affected (0.01 sec)

mysql> SELECT * FROM ll.userlist;

+----------+----------+

| username | password |

+----------+----------+

| liming | 123 |

+----------+----------+

1 row in set (0.00 sec)

#node2

mysql> SELECT * FROM ll.userlist;

+----------+----------+

| username | password |

+----------+----------+

| liming | 123 |

+----------+----------+

1 row in set (0.00 sec)

#从服务器只能读

node1

nide2

### node2的只能读不能写

当有数据添加slave2

[root@mysql-node3 mysql]# cat /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
symbolic-links=0
server-id=3

#从mater节点备份数据

[root@mysql-node1 ~]# mysqldump -uroot -p123 ll > ll.s

[root@mysql-node1 ~]# scp ll.sql root@172.25.250.30:/root
root@172.25.250.30's password:
ll.sql

[root@mysql-node3 ~]# mysql -uroot -p123 -e "create database ll;"

[root@mysql-node3 ~]# mysql -uroot -p123 ll<ll.sql

[root@mysql-node3 ~]# mysql -uroot -p123 -e "select * from ll.userlist;"

配置slave2的slave功能

在master查询日志pos

[root@mysql-node1 ~]# mysql -uroot -p123 -e "SHOW MASTER STATUS;"

mysql: [Warning] Using a password on the command line interface can be insecure.

+------------------+----------+--------------+------------------+-------------------+

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

+------------------+----------+--------------+------------------+-------------------+

| mysql-bin.000003 | 793 | | | |

+------------------+----------+--------------+------------------+-------------------+

在node3中

mysql> CHANGE MASTER TO MASTER_HOST='172.25.250.10',MASTER_USER='repl',MASTER_PASSWORD='123',MASTER_LOG_FILE='mysql-bin.000003',MASTER_LOG_POS=793;

mysql> start slave;

mysql> SHOW SLAVE STATUS\G;

测试

[root@mysql-node1 ~]# mysql -uroot -p123 -e "INSERT INTO ll.userlist VALUES('user1','123');"

延迟复制

#在slave端

mysql> STOP SLAVE SQL_THREAD;

Query OK, 0 rows affected (0.00 sec)

mysql> CHANGE MASTER TO MASTER_DELAY=60;

Query OK, 0 rows affected (0.00 sec)

mysql> START SLAVE SQL_THREAD;

Query OK, 0 rows affected (0.01 sec)

mysql> SHOW SLAVE STATUS\G;

在master里面插入数据

[root@mysql-node1 ~]# mysql -uroot -p123 -e "INSERT INTO ll.userlist VALUES('user2','123');"

在设置的slave延迟时间到了才能查询到

慢查询日志

定义:慢查询,顾名思义,执行很慢的查询 当执行SQL超过long_query_time参数设定的时间阈值(默认10s)时,就被认为是慢查询,这个 SQL语句就是需要优化的 慢查询被记录在慢查询日志里 慢查询日志默认是不开启的 如果需要优化SQL语句,就可以开启这个功能,它可以让你很容易地知道哪些语句是需要优化的。

开启慢查询日志

mysql> SET GLOBAL slow_query_log=ON;

mysql> SET long_query_time=4;

mysql> SHOW VARIABLES like "long%";

mysql> SHOW VARIABLES like "slow%";

测试

mysql的并行复制

默认情况下slave中使用的是sql单线程回放 在master中时多用户读写,如果使用sql单线程回放那么会造成组从延迟严重 开启MySQL的多线程回放可以解决上述问题
在slaves中设定

[root@mysql-node2 ~]# vim /etc/my.cnf

[mysqld]

datadir=/data/mysql

socket=/data/mysql/mysql.sock

symbolic-links=0

server-id=2

gtid_mode=ON

enforce-gtid-consistency=ON

slave-parallel-type=LOGICAL_CLOCK #基于组提交,

slave-parallel-workers=16 #开启线程数量

master_info_repository=TABLE #master信息在表中记录,默认记录 /data/mysql/master.inf

relay_log_info_repository=TABLE #回放日志信息在表中记录,默认记录在/data/mysql/relay-log.info

relay_log_recovery=ON #日志回放恢复功能开启

[root@mysql-node2 ~]# /etc/init.d/mysql restart

此时sql线程转化为协调线程,16个worker负责处理sql协调线程发送过来的处理请求

半同步模式

原理

1.用户线程写入完成后master中的dump会把日志推送到slave端

2.slave中的io线程接收后保存到relaylog中继日志

3.保存完成后slave向master端返回ack

4.在未接受到slave的ack时master端时不做提交的,一直处于等待当收到ack后提交到存储引擎

5.在5.6版本中用到的时after_commit模式,after_commit模式时先提交在等待ack返回后输出ok

理解

MySQL 的半同步模式是一种在主从复制中提高数据一致性的机制。

在传统的异步复制中,主库在执行事务提交后,不会等待从库确认收到事务日志就会返回给客户端事务提交成功的响应。这可能导致在主库故障时,某些已经提交的事务还未同步到从库,从而造成数据丢失。

而半同步复制模式下,主库在提交事务时,会等待至少一个从库确认已经接收到事务的二进制日志,然后主库才会向客户端返回事务提交成功的响应。

半同步模式在一定程度上保证了数据的一致性,但也会带来一些性能开销,因为主库需要等待从库的确认。同时,如果所有从库都长时间没有响应,主库可能会切换回异步复制模式以避免阻塞。

gtid模式

#在master端和slave端开启gtid模式

[root@mysql-node1 ~]# cat /etc/my.cnf

[mysqld]

datadir=/data/mysql

socket=/data/mysql/mysql.sock

symbolic-links=0

log-bin=mysql-bin

server-id=1

gtid_mode=ON

enforce-gtid-consistency=ON

symbolic-links=0

[root@mysql-node1 ~]# /etc/init.d/mysql restart

[root@mysql-node2 ~]# cat /etc/my.cnf

[mysqld]

datadir=/data/mysql

socket=/data/mysql/mysql.sock

symbolic-links=0

server-id=2

log-bin=mysql-bin

gtid_mode=ON

enforce-gtid-consistency=ON

[root@mysql-node2 ~]# /etc/init.d/mysql restart

[root@mysql-node3 ~]# cat /etc/my.cnf

[mysqld]

datadir=/data/mysql

socket=/data/mysql/mysql.sock

symbolic-links=0

server-id=3

log-bin=mysql-bin

gtid_mode=ON

enforce-gtid-consistency=ON

[root@mysql-node3 ~]# /etc/init.d/mysql restart
#停止slave端

[root@mysql-node3 ~]# mysql -p123 #node2和node3

mysql> stop slave;
#开启slave端的gtid

mysql> CHANGE MASTER TO MASTER_HOST='172.25.250.10',MASTER_USER='repl',MASTER_PASSWORD='123',MASTER_AUTO_POSITION=1;

启用半同步模式

在master端启用

[root@mysql-node1 ~]# cat /etc/my.cnf

[mysqld]

datadir=/data/mysql

socket=/data/mysql/mysql.sock

symbolic-links=0

log-bin=mysql-bin

server-id=1

gtid_mode=ON

enforce-gtid-consistency=ON

symbolic-links=0

rpl_semi_sync_master_enabled=1

[root@mysql-node1 ~]# mysql -p123

mysql> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';

###安装半同步插件

mysql> SELECT PLUGIN_NAME, PLUGIN_STATUS FROM INFORMATION_SCHEMA.PLUGINS WHERE PLUGIN_NAME LIKE '%semi%';

###查看插件情况

mysql> SET GLOBAL rpl_semi_sync_master_enabled = 1;

#打开半同步功能

mysql> SHOW VARIABLES LIKE 'rpl_semi_sync%';

##查看半同步功能状态

mysql> SHOW STATUS LIKE 'Rpl_semi_sync%';

在slave端开启半同步功能

[root@mysql-node2 ~]# cat /etc/my.cnf

[mysqld]

datadir=/data/mysql

socket=/data/mysql/mysql.sock

symbolic-links=0

server-id=2

log-bin=mysql-bin

gtid_mode=ON

enforce-gtid-consistency=ON

rpl_semi_sync_master_enabled=1 #开启半同步功能

mysql> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';

mysql> SET GLOBAL rpl_semi_sync_slave_enabled =1;

mysql> STOP SLAVE IO_THREAD; ###重启io线程,半同步才能生效

mysql> START SLAVE IO_THREAD;

mysql> SHOW VARIABLES LIKE 'rpl_semi_sync%';

mysql> SHOW STATUS LIKE 'Rpl_semi_sync%';

测试

在master端写入数据

mysql> insert into ll.userlist values ('user4','123');

mysql> SHOW STATUS LIKE 'Rpl_semi_sync%';

模拟故障

#在slave端

[root@mysql-node2 ~]# mysql -p123

mysql> STOP SLAVE IO_THREAD;

[root@mysql-node3 ~]# mysql -p123

mysql> STOP SLAVE IO_THREAD;

master里面

mysql组复制

#在mysql-node1中

[root@mysql-node1 ~]# rm -fr /data/mysql/

[root@mysql-node1 ~]# vim /etc/my.cnf

mysqld]

datadir=/data/mysql

socket=/data/mysql/mysql.sock

symbolic-links=0

server-id=1

disabled_storage_engines="MyISAM,BLACKHOLE,FEDERATED,ARCHIVE,MEMORY"

gtid_mode=ON

enforce_gtid_consistency=ON

master_info_repository=TABLE

relay_log_info_repository=TABLE

binlog_checksum=NONE

log_slave_updates=ON

log_bin=binlog

binlog_format=ROW

plugin_load_add='group_replication.so'

transaction_write_set_extraction=XXHASH64

group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"

group_replication_start_on_boot=off

group_replication_local_address="172.25.250.10:33061"

group_replication_group_seeds="172.25.250.10:33061,172.25.250.20:33061,172.25.250.30:33061"

group_replication_ip_whitelist="172.25.250.0/24,127.0.0.1/8"

group_replication_bootstrap_group=off

group_replication_single_primary_mode=OFF

group_replication_enforce_update_everywhere_checks=ON

group_replication_allow_local_disjoint_gtids_join=1

[root@mysql-node1 ~]# mysqld --user=mysql --initialize

[root@mysql-node1 ~]# /etc/init.d/mysqld start

[root@mysql-node10 ~]# mysql -uroot -p初始化后生成的密码 -e "alter user root@localhost identified by '123';"

#配置sql

[root@mysql-node1 ~]# mysql -p123

mysql> SET SQL_LOG_BIN=0;

mysql> CREATE USER rpl_user@'%' IDENTIFIED BY '123';

mysql> GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%';

mysql> FLUSH PRIVILEGES;

mysql> SET SQL_LOG_BIN=1;

mysql> CHANGE MASTER TO MASTER_USER='rpl_user', MASTER_PASSWORD='123' FOR CHANNEL 'group_replication_recovery';

mysql> SET GLOBAL group_replication_bootstrap_group=ON;

mysql> START GROUP_REPLICATION;

mysql> SET GLOBAL group_replication_bootstrap_group=OFF;

mysql> SELECT * FROM performance_schema.replication_group_members;

#在复制配置文件到myql-node20和mysql-node30

[root@mysql-node2 ~]#scp /etc/my.cnf root@172.25.250.20:/etc/my.cnf

[root@mysql-node2 ~]# vim /etc/my.cnf

[mysqld]

datadir=/data/mysql

socket=/data/mysql/mysql.sock

symbolic-links=0

server-id=2

disabled_storage_engines="MyISAM,BLACKHOLE,FEDERATED,ARCHIVE,MEMORY"

gtid_mode=ON

enforce_gtid_consistency=ON

master_info_repository=TABLE

relay_log_info_repository=TABLE

binlog_checksum=NONE

log_slave_updates=ON

log_bin=binlog

binlog_format=ROW

plugin_load_add='group_replication.so'

transaction_write_set_extraction=XXHASH64

group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"

group_replication_start_on_boot=off

group_replication_local_address="172.25.250.20:33061"

group_replication_group_seeds="172.25.250.10:33061,172.25.250.20:33061,172.25.250.30:33061"

group_replication_ip_whitelist="172.25.250.0/24,127.0.0.1/8"

group_replication_bootstrap_group=off

group_replication_single_primary_mode=OFF

group_replication_enforce_update_everywhere_checks=ON

group_replication_allow_local_disjoint_gtids_join=1

[root@mysql-node1 ~]# mysqld --user=mysql --initialize

[root@mysql-node1 ~]# /etc/init.d/mysqld start

[root@mysql-node10 ~]# mysql -uroot -p初始化后生成的密码 -e "alter user root@localhost identified by '123';"

#配置sql

[root@mysql-node1 ~]# mysql -p123

mysql> SET SQL_LOG_BIN=0;

mysql> CREATE USER rpl_user@'%' IDENTIFIED BY '123';

mysql> GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%';

mysql> FLUSH PRIVILEGES;

mysql> SET SQL_LOG_BIN=1;

mysql> CHANGE MASTER TO MASTER_USER='rpl_user', MASTER_PASSWORD='123' FOR CHANNEL 'group_replication_recovery';

mysql> SET GLOBAL group_replication_bootstrap_group=ON;

mysql> START GROUP_REPLICATION;

mysql> SET GLOBAL group_replication_bootstrap_group=OFF;

mysql> SELECT * FROM performance_schema.replication_group_members;

测试

在node1上面

mysql> CREATE TABLE bwmis.userlist(

-> username VARCHAR(10) PRIMARY KEY NOT NULL,

-> password VARCHAR(50) NOT NULL-> );

mysql>INSERT INTO bwmis.userlist VALUES ('user1','123'); node1上面

mysql>INSERT INTO bwmis.userlist VALUES ('user2','123') node2上面

mysql>INSERT INTO bwmis.userlist VALUES ('user1','123') node3上面

mysql路由

是一个对应用程序透明的InnoDB Cluster连接路由服务,提供负载均衡、应用连接故障转移和客户端路 由。 利用路由器的连接路由特性,用户可以编写应用程序来连接到路由器,并令路由器使用相应的路由策略 来处理连接,使其连接到正确的MySQL数据库服务器

[root@mysql-mha ~]# rpm -ivh mysql-router-community-8.4.0-1.el7.x86_64.rpm

[root@mysql-mha ~]# vim /etc/mysqlrouter/mysqlrouter.conf

相关推荐
时差95328 分钟前
【面试题】Hive 查询:如何查找用户连续三天登录的记录
大数据·数据库·hive·sql·面试·database
让学习成为一种生活方式30 分钟前
R包下载太慢安装中止的解决策略-R语言003
java·数据库·r语言
秋意钟1 小时前
MySQL日期类型选择建议
数据库·mysql
Dxy12393102162 小时前
python下载pdf
数据库·python·pdf
ac-er88882 小时前
MySQL如何实现PHP输入安全
mysql·安全·php
桀桀桀桀桀桀2 小时前
数据库中的用户管理和权限管理
数据库·mysql
superman超哥3 小时前
04 深入 Oracle 并发世界:MVCC、锁、闩锁、事务隔离与并发性能优化的探索
数据库·oracle·性能优化·dba
用户8007165452003 小时前
HTAP数据库国产化改造技术可行性方案分析
数据库
engchina4 小时前
Neo4j 和 Python 初学者指南:如何使用可选关系匹配优化 Cypher 查询
数据库·python·neo4j
engchina4 小时前
使用 Cypher 查询语言在 Neo4j 中查找最短路径
数据库·neo4j