实现mysql数据库,主从同步功能
内网安装两台mysql一主一从
一、安装两台mysql5.7版本
1.下载mysql
cd /usr/local/
wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.30-linux-glibc2.12-x86_64.tar
(如果没有wget命令,需要先安装wget命令。wget安装命令:yum -y install wget)
将下载好的mysql.tar包进行解压
tar -xf mysql-5.7.41-linux-glibc2.12-x86_64.tar.gz
2.把解压后的mysql移动到/usr/local/目录下
mv mysql-5.7.41-linux-glibc2.12-x86_64 /usr/local/mysql
3.进入mysql中的/bin目录
cd /usr/local/mysql/bin/
4.创建一个用户和组,给其权限,将/bin目录属组属主改成mysql
groupadd mysql
useradd -r -g mysql mysql
chown -R mysql:mysql ./
5.返回/mysql目录,初始化mysql
cd ..
bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
6.设置service进行启动mysql
cp support-files/mysql.server /etc/init.d/mysql
7.设置变量并刷新
vi /etc/profile
export MYSQL_HOME=/usr/local/mysql
export PATH=$PATH:$MYSQL_HOME/bin
source /etc/profile
8.修改my.cnf文件(为了做主从方便,这里直接把文件改好,从库不用加log-bin=test)
vi /etc/my.cnf(下图)
9.启动maysql
service mysql start
10.重置mysql密码(password后面是新密码,回车后要输入的旧密码在初始化mysql那步会显示)
/usr/local/mysql/bin/mysqladmin -u root -p password '123'
11. 进入mysql
mysql -uroot -p密码
二、按照上述安装两台mysql之后,进入mysql
1.设置可以远程登入
Use mysql;
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123' WITH GRANT OPTION;
(权限:允许所有主机的root用户通过密码123进行远程登入)
FLUSH PRIVILEGES; (刷新权限)
2.退出之后用另一台mysql远程登入测试:mysql -h ip地址 -uroot -p123 (关闭防火墙)
3.打开主库创建用户,并赋予权限 (my.cnf文件中server_id=10)
mysql> create user 'repl'@'%' identified by 'repl123';
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%';
4.查看主库的master状态
show slave status \G;
5.进入从库先配置slave,先关闭slave,配置后再开启
Stop slave;
CHANGE MASTER TO MASTER_HOST='192.168.197.131', MASTER_USER='repl', MASTER_PASSWORD='repl123', MASTER_PORT=3306, MASTER_LOG_FILE='test.000001', MASTER_LOG_POS=3054;
Start slave;
6.查看结果,IO线程和sql线程都开启显示yes,配置成功
Show slave status \G;