linux 7.2 下先安装当前的包 mysql-5.1.18的安装
先卸载mariadb数据库
#rpm -qa|grep mariadb
#rpm -e --nodeps mariadb-libs ,server,client
bison安装包,libaio安装包,gcc-c安装包,ncurses-devel安装包 c* gc*
boost这个包。
yum -y install bison* libaio* gcc* ncurses* c* boost*
创建用户以及目录
shell> groupadd mysql
shell> useradd -r -g mysql -s /bin/false mysql --此乃mysql官网说明
groupadd -g 701 mysql
useradd -M -g mysql -u 1101 -s /sbin/nologin mysql #建立mysql账号,-u是用户号,-M不建立家目录,-s自动以shell为/sbin/nologin账号不能登录
mysql软件目录: /data/mysql/installdir
mysql数据目录:/data/mysql/datadir/3306/data
mysql日志目录:/data/mysql/logdir/3306/{bin_log,general_log,error_log,query_log}
mkdir -p /data/mysql/installdir
mkdir -p /data/mysql/datadir/3306/data
mkdir -p /data/mysql/logdir/3306/{bin_log,general_log,error_log,query_log}
mkdir -p /data/mysql/logdir/3306/relay_log ----若要主主的时候会用到
mkdir -p /data/mysql/tmpdir
mkdir -p /data/mysql/src
cd /data
chown -R mysql:mysql mysql/
安装cmake安装包
#cd mysql
#tar -zxvf cmake-3.11.1.tar.gz
#cd cmake-3.11.1
#./configure
#gmake && gmake install
#echo $? 可以查看是否有错误
安装mysql安装包
#cp mysql-5.7.18.tar.gz /data/mysql/src
#tar -zxvf mysql-5.7.18.tar.gz
#cd mysql-5.7.18
#cmake . -DCMAKE_INSTALL_PREFIX=/data/mysql/installdir
-DDEFAULT_CHARSET=utf8
-DDEFAULT_COLLATION=utf8_general_ci
-DENABLED_LOCAL_INFILE=ON
-DWITH_INNOBASE_STORAGE_ENGINE=1
-DWITH_FEDERATED_STORAGE_ENGINE=1
-DWITH_BLACKHOLE_STORAGE_ENGINE=1
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1
-DWITH_PARTITION_STORAGE_ENGINE=1
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1
-DWITH_READLINE=ON
-DDOWNLOAD_BOOST=1
-DWITH_BOOST=/usr/local/boost
-DSYSCONFDIR=/data/mysql/datadir/3306/data
-DMYSQL_UNIX_ADDR=/data/mysql/datadir/3306/data/mysql.sock
-DCOMPILATION_COMMENT=‘MySQL Master’\ ## [有时候增加后,会出现The source directory “/mysql/mysql-5.1.18/master” does not exist 的错误,去掉以后就好了 ]
需要安装: 一下安装是为了支持readline 库
yum install readline-devel -y
如果出现boost的错误,就需要安装以下的包
boost这个包
#cd /usr/local/boost
wget http://www.sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz
make && make install
编辑mysql,的my.cnf
vi /etc/my.cnf
[client]
port=3306
socket=/data/mysql/datadir/3306/data/mysql.sock
[mysqld]
port=3306
user=mysql
socket=/data/mysql/datadir/3306/data/mysql.sock
pid-file=/data/mysql/datadir/3306/data/mysql.pid
basedir=/data/mysql/installdir
datadir=/data/mysql/datadir/3306/data
tmpdir=/data/mysql/tmpdir
log_error=/data/mysql/logdir/3306/error_log/mysql3.err
server-id = 2
log_bin = /data/mysql/logdir/3306/bin_log/binlog
general_log_file=/data/mysql/logdir/3306/general_log
general_log = 1
slow_query_log = ON
long_query_time = 2
slow_query_log_file = /data/mysql/logdir/3306/query_log
log_queries_not_using_indexes = ON
binlog-ignore-db=mysql
binlog_cache_size=512M
binlog_format=mixed
expire_logs_days=0
slave_skip_errors=1062
relay_log= /data/mysql/logdir
log_slave_updates=1
auto_increment_increment=2
auto_increment_offset=2
###########################################################################################################
[client]
port=3306
socket=/data/mysql/datadir/3306/data/mysql.sock
[mysqld]
port=3306
user=mysql
socket=/data/mysql/datadir/3306/data/mysql.sock
pid-file=/data/mysql/datadir/3306/data/mysql.pid
basedir=/data/mysql/installdir
datadir=/data/mysql/datadir/3306/data
tmpdir=/data/mysql/tmpdir
log_error=/data/mysql/logdir/3306/error_log/mysql3.err
server-id = 1
log_bin = /data/mysql/logdir/3306/bin_log/binlog
general_log_file=/data/mysql/logdir/3306/general_log
general_log = 1
slow_query_log = ON
long_query_time = 2
slow_query_log_file = /data/mysql/logdir/3306/query_log
log_queries_not_using_indexes = ON
binlog-ignore-db=mysql
binlog_cache_size=512M
binlog_format=mixed
expire_logs_days=0
slave_skip_errors=1062
relay_log= /data/mysql/logdir
log_slave_updates=1
auto_increment_increment=2
auto_increment_offset=1
explicit_defaults_for_timestamp=true
#Master config#
binlog-ignore-db=mysql
binlog_cache_size=1M
binlog_format=mixed
expire_logs_days=0
slave_skip_errors=1062
relay_log=/data/mysql/logdir/3306/relay_log
log_slave_updates=1
#GTID USING#
###########################################################################################################
初始化数据库
cd /data/mysql/installdir/bin
./mysqld --initialize --user=mysql --basedir=/data/mysql/installdir --datadir=/data/mysql/datadir/3306/data
启动数据库
./mysqld_safe --defaults-file=/etc/my.cnf &
或者
cp /data/mysql/installdir/support-files/mysql.server /etc/init.d/mysqld
/etc/init.d/mysqld start
/etc/init.d/mysqld stop
建立全局mysql命令环境
echo ‘export PATH=/data/mysql/installdir/bin:$PATH’ >>/etc/profile
source /etc/profile
配置mysql密码
/etc/init.d/mysqld stop
mysqld_safe --skip-grant-tables &
mysql
mysql>update mysql.user set authentication_string=password(‘cyh123@’); where user=‘root’;
select host,user,authentication_string from mysql.user;
mysql>flush privileges;
/etc/init.d/mysqld restart
mysql -u -p
mysql>set password=password(‘cyh123@’); --由于前面更改不完全,需要重新进行一次密码更改。然后才能运行其它的mysql命令
主主配置
Master A 参数文件配置,增加如下行
########################################缩减后的结果#################
binlog-ignore-db=mysql
binlog_cache_size=1M
binlog_format=mixed
expire_logs_days=0
slave_skip_errors=1062
relay_log=/data/mysql/logdir/3306/relay_log
log_slave_updates=1
########################################如上述结果#################
#vim /etc/my.cnf
[mysqld]
[mysqld]
server_id=1
binlog-ignore-db=mysql
log-bin=日志路径
binlog_cache_size=1M
binlog_format=mixed
expire_logs_days=0
slave_skip_errors=1062
relay_log=中继日志的位置或名称,默认是在数据目录下
log_slave_updates=1
auto_increment_increment=2 # 自增因子(每次加2)
auto_increment_offset=1 # 自增偏移(从1开始),单数
Master A 数据库配置
#/etc/init.d/mysqld restart
#mysql -uroot -p
mysql>stop slave;
mysql>grant replication slave,replication client on . to ‘rep1’@‘10.60.10.71’ identified by ‘repcyh123@’; ###让对方来读取我的日志
mysql>flush privileges;
mysql>show master status\G;
获取当前的binlog日志名称,以及position位置点
然后在Master B 数据库上面配置
#mysql -uroot -p
mysql>stop slave;
mysql>change master to
master_host=‘10.60.11.2’,
master_user=‘repl’,
master_password='repcyh123@ ',
master_log_file=‘binlog.000022’,
master_log_pos=1177,
master_connect_retry=30,
master_port=3306;
mysql> start slave;
Master B 数据库的配置
/etc/init.d/mysqld restart
#mysql -uroot -p
mysql>grant replication slave,replication client on . to ‘rep1’@‘10.60.11.2’ identified by ‘repcyh123@’; ###让对方来读取我的日志
mysql>flush privileges;
mysql>show master status\G;
Master A 数据库的配置
#mysql -uroot -p
mysql>stop slave;
mysql>change master to
master_host=‘10.60.11.4’,
master_user=‘repl’,
master_password='repcyh123@ ',
master_log_file=‘binlog.000022’,
master_log_pos=1177,
master_connect_retry=30,
master_port=3306;
mysql> start slave;
Master B 参数文件配置,增加如下行
server_id=2
binlog-ignore-db=mysql
log-bin=路径
binlog_cache_size=1M
binlog_format=mixed
expire_logs_days=0
slave_skip_errors=1062
relay_log=路径
log_slave_updates=1
#ID自增从2开始,双数
auto_increment_increment=2
auto_increment_offset=2
如果停止stop slave 的过程中遇到
> stop slave;
ERROR 1192 (HY000): Can’t execute the given command because you have active locked tables or an active transaction的错误
unlock tables
然后再重新stop slave
mysql> show slave status\G;
Master A 跟 Master B 运行如下
#mysql -uroot -p
mysql>start slave;
mysql>show slave status\G;
注意:
Slave_IO_Running:Yes
Slave_SQL_Running:Yes
################my.cnf的配置文件如下#################
[client]
port=3306
socket=/data/mysql/datadir/3306/data/mysql.sock
[mysqld]
port=3306
user=mysql
socket=/data/mysql/datadir/3306/data/mysql.sock
pid-file=/data/mysql/datadir/3306/data/mysql.pid
basedir=/data/mysql/installdir
datadir=/data/mysql/datadir/3306/data
tmpdir=/data/mysql/tmpdir
log_error=/data/mysql/logdir/3306/error_log/mysql3.err
server-id = 2
log_bin = /data/mysql/logdir/3306/bin_log/binlog
general_log_file=/data/mysql/logdir/3306/general_log
general_log = 1
slow_query_log = ON
long_query_time = 2
slow_query_log_file = /data/mysql/logdir/3306/query_log
log_queries_not_using_indexes = ON
binlog-ignore-db=mysql
binlog_cache_size=512M
binlog_format=mixed
expire_logs_days=0
slave_skip_errors=1062
#relay_log= /data/mysql/logdir
log_slave_updates=1
auto_increment_increment=2
auto_increment_offset=2
explicit_defaults_for_timestamp=true
#Master config
#binlog-ignore-db=mysql
#binlog_cache_size=1M
#binlog_format=mixed
#expire_logs_days=0
#slave_skip_errors=1062
relay_log=/data/mysql/logdir/3306/relay_log
#log_slave_updates=1
skip-host-cache
skip-name-resolve=1
#relay-log-index=slave-relay-bin.index
#relay-log=slave-relay-bin
##############MYSQL GTID的开启###############
1,确保error log没有错误,且验证是否可以开启gtid|每台服务器都需要进行执行
set @@global.enforce_gtid_consistency=warn;
2,在每一台server上执行
set @@global.enforce_gtid_consistency=on;
3, 在每一台server上执行
set @@global.gtid_mode=off_permissive;
4,每一台server 执行,执行顺序没有先后之分,要保证下一步操作之前,上面的操作都已在所有server上执行过
set @@global.gtid_mode=on_permissive;
5,保证每一台ongoing_anonymous_transaction_count状态值为零
show status like ‘ongoing_anonymous_transaction_count’;
6,确保没有任何操作事务在进行
mysql> SET @@global.read_only = ON;
7,设置gtid_mode=on在my.cnf文件中
gtid_mode=on
enforce_gtid_consistency=on
8, 执行如下操作如下
set @@global.gtid_mode=on;
9,stop slave
10, 重启每台服务器
11,执行如下语句(主主的都要进行执行)
stop slave;
change master to master_auto_position=1 ;
start slave;
12 ,如果新建立的情况下,则此步需要另外加参数,如同主主配置。
###########如何关闭GTID MODE#################
stop slave;
change master to master_auto_position=0,master_log_file=‘mysql-bin.000383’,master_log_pos= 245710922 ;
binlog日志选择没有的日志号
pos 选择一个很大的位置号
start slave;
以下每条语句都需要在每个服务器上执行完成后,然后执行下一条
set @@global.gtid_mode=on_permissive;
set @@global.gtid_mode=off_permissive;
select @@global.gtid_owned;
set @@global.gtid_mode=off;
然后在my.cnf里面
gtid_mode=off
################################################
以上开启GTID MODE 的模式可以省略一些步骤,不过最好都执行。