mysql集群的主从复制
点击上方“IT那活儿”,关注后了解更多内容,不管IT什么活儿,干就完了!!!
1. 解压及创建目录
[root@node1 local] # mv /root/mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz /usr/local/
[root@hadoop1 local]# tar -zxvf mysql-5 .7 .28-linux-glibc2 .12-x86_64 .tar .gz
[root@hadoop1 local]# mv mysql-5 .7 .28-linux-glibc2 .12-x86_64 mysql
[root@hadoop1 local] # cd mysql
[root@hadoop1 mysql] # mkdir arch tmp relay_log
2. 创建用户及用户组
[root@hadoop1 local] # groupadd -g 101 dba
[root@hadoop1 mysql]# cat / etc/ group | tail -n 3
postfix :x :89:
ntp :x :38:
dba :x :101:
/usr/ local/mysql/
[root@hadoop1 local] # useradd -g dba -d /usr/local/mysql/ mysql
[root@hadoop1 local] # cp /etc/skel/.bash_profile /usr/local/mysql/
[root@hadoop1 local] # cp /etc/skel/.bash_logout /usr/local/mysql/
[root@hadoop1 local] # cp /etc/skel/.bashrc /usr/local/mysql/
3. 配置mysql用户环境变量(注意使用root用户)
[mysql@hadoop1 ~]$ vi /usr/ local/mysql/.bash_profile
#mysql
export MYSQL=/usr/ local/mysql
export PATH= $PATH: $MYSQL/bin
4. 创建mysql的配置文件my.cnf(如果出现覆盖overwrite,输入y)
上传my. cnf
[root@hadoop1 ~]# mv my. cnf /etc/
[root@hadoop1 ~] # chown mysql:dba /etc/my.cnf
[root@hadoop1 ~] # chmod 640 /etc/my.cnf
[root@hadoop1 ~] # chown -R mysql:dba /usr/local/mysql/
[root@hadoop1 ~] # chmod -R 755 /usr/local/mysql/
6. 配置开机自动启动
[root@hadoop1 ~] # cp /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysql
[root@hadoop1 ~] # chmod +x /etc/rc.d/init.d/mysql
[root@hadoop1 ~] # chkconfig --add mysql
[root@hadoop1 ~] # chkconfig mysql on
7. 安装libaio包
[root@hadoop1 ~] # yum -y install libaio
8. 初始化mysql
[root@hadoop1 ~] # su - mysql
# data是mysql数据库存放数据的目录
[mysql@hadoop1 ~]$ mkdir data
#初始化mysql
[mysql@hadoop1 mysql]$ bin/mysqld --defaults-file= /etc/my.cnf --user=mysql --basedir= /usr/local /mysql --datadir=/usr /local/mysql /data --initialize
9. 查看临时密码有没有生成
[mysql@hadoop1 data]$ cat /usr/local/mysql/data/ hostname.err | grep password
10. 启动mysql服务
[mysql@hadoop1 data]$ /usr/local/mysql/bin/mysqld_safe --defaults-file= /etc/my.cnf &
[mysql@hadoop1 data]$ service mysql status
#也可以去看一下mysql的进程在不在
[mysql@hadoop1 data]$ ps -elf | grep mysql (会发现mysql进程正在启动)
11. 修改临时密码
需要进入mysql
mysql -uroot -p
mysql> alter user root@localhost identified by '123456';
mysql> grant all privileges on *.* to 'root'@ '%' identified by '123456';
mysql> flush privileges;
12. 验证连接工具能够连接
[mysql@hadoop1 mysql]$ ps -elf | grep mysql
[mysql@hadoop1 mysql]$ kill - 9 [PID]
检查你的密码对不对;
检查你的mysql有没有启动,需要重新启动,而且需要再添加开机自动启动。
[mysql@hadoop1 ~]$ service mysql status
SUCCESS! MySQL running ( 7661)
如果是success没有问题。
[mysql@hadoop1 ~]$ ps -elf | grep mysql
看一下有没有这个进程。
mysqldump -uroot -p123456 test user > user.sql
[mysql@hadoop1 mysql]$ mysql -uroot -p123456 -s - e "show databases;" | grep -v Database
mysq l: [Warning] Using a password on the command line interface can be insecure.
information_schema
mysql
performance_schema
sys
test
test2
[mysql@hadoop1 mysql]$ mysql -uroot -p123456 -s - e "use test;show tables;" | grep -v Tables_in
mysq l: [Warning] Using a password on the command line interface can be insecure.
user
开启binlog日志需要在配置文件中添加:
查看binlog日志文件的命令:
[mysql@hadoop1 arch]$ mysqlbinlog -- no-defaults --base64-output=decode-rows -vvv mysql-bin .000003
[mysql@hadoop2 relay_log]$ exit
logout
[root@hadoop2 ~] # service mysql restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
创建用户:
mysql> create user 'reproduce'@ 'localhost' identified by '123456';
Query OK, 0 rows affected (0.02 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
验证:
远程访问:
加权限(在主库使用root用户):
mysql> grant replication slave on *.* to 'reproduce'@ '%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
验证:
change master to
master_host= '192.168.174.10',
master_port= 3306,
master_user= 'reproduce',
master_password= '123456',
master_log_file= 'mysql-bin.000002',
master_log_pos= 1680;
IO_thread
SQL_thread
mysql> start slave;
Query OK, 0 rows affected (0.02 sec)
mysql> show slave status\G
Slave_IO_Runnin g: Yes
Slave_SQL_Runnin g: Yes
[mysql@hadoop2 relay_log]$ mysqlbinlog -- no-defaults --base64-output=decode-rows -vvv relay- log. 000002