Centos7 配置Mysql主从服务器
注意:centos7安装的是mysql5.6
(1)首先,保证有两台设备(主、从),分别在两台设备上安装Mysql数据库。安装完成之后,
使用mysql -u[数据库用户名] -p[用户密码],若能够登陆mysql则表示安装成功。
(2)编辑mysql配置文件,路径一般为/etc/mysql/mysql.conf.d/mysqld.cnf(用户在安装Mysql时指定的目录,
也有可能是在以下目录
vi /etc/my.cnf
如:/etc/my.conf等)。分别按要求在主、从mysql中的[mysqld]模块下添加如下内容:
主:
server-id=1
log-bin=master-bin
log-bin-index=master-bin.index
或者
server-id = 1
log-bin = mysql-bin
innodb_flush_log_at_trx_commit=1
sync_binlog=1
从:
server-id = 2
replicate-do-db = test
(3)shell连接主、从服务器,mysql -u[数据库用户名] -p[用户密码],分别登录到主、从节点mysql,创建同步用户repl,执行如下命令:
grant all privileges on *.* to 'repl'@'%' identified by 'repl' with grant option;
然后exit退出Mysql操作页,重新启动Mysql服务,命令如下:
systemctl restart mysql.service
然后进入主节点Mysql,查看Mysql的File列和Positon列。执行命令:
show master status;
出现结果如下
(4)登录到从服务器,mysql -u[数据库用户名] -p[用户密码]进入Mysql,执行:
stop slave;
reset slave;
change master to master_host='192.168.124.51',master_user='repl',master_password='repl',master_log_file='master-bin.000001',master_log_pos=120;
exit;
执行
systemctl restart mysql.service
重启Mysql服务。
(5)启动从服务器slave库,mysql -u[数据库用户名] -p[用户密码]进入从节点Mysql,执行如下命令:
start slave;
执行:
show slave status\G;
查看备份服务状态。如果以下两个状态为Yes则表示主从同步成功。
接下来,大家就可以尝试向主数据库中插入一条数据,看是够从数据库中会自动增加该条数据,是不是有点小激动呢?赶紧测试下吧。
PS:若出现无法同步问题?
(1)请查看上图中Master_Log_File属性、Read_Master_Log_Pos属性与主节点查询结果是否一致。
(2)可能是从数据库连接主数据库超时,去吧连接延时改大一点吧。
(3)如果上述无法解决,请查看mysql日志排查原因。
如果是uuid重复了,那么修改uuid就行:
查询命令找此auto.cnf修改uuid即可:
find -name auto.cnf
其实这个文件就在mysql的data目录中/app/mysql-5.6.25/data,这是我的文件位置
启mysql服务器,再查看mysql从节点的状态,恢复正常
在重新启动mysql
service myqld restart
如果root 密码为空的时候配置文件中下面这句:
skip-grant-tables
GRANT ALL PRIVILEGES ON . TO IDENTIFIED BY ‘123’ WITH GRANT OPTION;
执行这句时候错误:
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
mysql> GRANT ALL PRIVILEGES ON . TO IDENTIFIED BY ‘123’ WITH GRANT OPTION;
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
这个时候我们只需要
flush privileges;
一下,在添加用户就OK了,
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> GRANT ALL PRIVILEGES ON . TO IDENTIFIED BY ‘123’ WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)
这个时候我们成功搞定了,再登录就可以了。
如果报错如下信息:
Error: Cannot retrieve repository metadata (repomd.xml) for repository: InstallMedia. Please verify its path and try again
You could try using --skip-broken to work around the prolem
You could try running: rpm -Va --nofiles --nodigest
我们只要到/etc/yum.repo.s下面把packetxxxx.repo和redhat.repo两个文件删除掉,再启动就可以了,
参考链接:
https://www.jb51.net/article/27222.htm
https://www.cnblogs.com/wajika/p/6710458.html
https://www.cnblogs.com/martinzhang/p/3454358.html
http://www.cnblogs.com/linguoguo/p/4387605.html
https://blog.csdn.net/qq_22227087/article/details/80946894
http://blog.sina.com.cn/s/blog_72e339cd0100xphg.html
https://blog.csdn.net/z69183787/article/details/70183284
https://www.cnblogs.com/zgx/archive/2011/09/13/2174823.html
https://www.cnblogs.com/tianshupei88/p/5075367.html
https://www.cnblogs.com/goodhacker/p/3373213.html
https://www.cnblogs.com/Mr-zhou/p/11470303.html
https://www.jb51.net/article/169436.htm