mysql配置主从数据库

mysql配置主从数据库

MySQL的主从同步是一个很成熟的架构,优点为:

①在从服务器可以执行查询工作(即我们常说的读功能),降低主服务器压力;

②在从主服务器进行备份,避免备份期间影响主服务器服务;

③当主服务器出现问题时,可以切换到从服务器。

所以我在项目部署和实施中经常会采用这种方案.

#我们在配置之前两个服务器的数据库不相同的话我们可以先迁移数据库是两边的数据先相同!然后在配置主从

+迁移数据库

mysqldump-h127.0.0.1-uroot-ppass myweb|mysql–host=***.***.***.*** -u数据库用户名 -p数据库密码 -C serweb

#将数据库转移到新服务器。此例为将本地数据库myweb复制到远程数据库名为serweb中,其中远程数据库必须有名为serweb的数据库

#在迁移过程中可能会报以下错误

mysqldump: Got error: 1045: Access denied for user ‘csfdp’@’%’ (using password: YES)

这时我们只要给用户权限就好

grant all privileges on . to ‘root’@’%’ identified by ‘root’ with grant option;

使两边的数据相同后我们就可以配置主从同步了。

  • 数据库目录及其它

my.cnf配置文件 /etc/my.cnf

mysql数据库位置 datadir=/usr/local/nginx/mysql

主数据库:192.168.253.130

从数据库:192.168.253.130

一、设置主库

1、修改主库my.cnf,主要是设置个不一样的id和logbin(为了方便识别id我们可以设置为域名最后几位数字)

[root@localhost etc]#vi /etc/my.cnf

记住这部分一定要配置在[mysqld]后面,否则无法找到从节点,各个配置项的含义可自己查阅文档

log-bin=mysql-bin

binlog_format=mixed

server-id = 130

2、显示主库信息

mysql> show master status;

±-----------------±---------±-------------±-----------------+

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |

±-----------------±---------±-------------±-----------------+

| mysql-bin.000045 | 2575 | | |

±-----------------±---------±-------------±-----------------+

看到这个信息说明mysql的二进制文件是从000045开始 磁盘的偏移量在2575

二、设置从库

1、在220节点上修改从库my.cnf(位置一样)

log-bin=mysql-bin(开启二进制日志!根据个人习惯可开启或不开启,本人建议还是开启,以防数据丢失时可以恢复)

relay-log=mysql-relay(从库的relay日志)

binlog_format=mixed()

server-id = 131

三、授权

#我们配置完主库和从库以后!我们要给两个服务器简历链接!所以我们要授权,出库给从库授权一个user从库使用这个user建立起链接

1、grant replication slave on . to ‘username’@’%’ identified by ‘your_password’;

三、这是回到从库设置

1、mysql> change master to

                master_host='192.168.253.130',

                master_user='username',

                master_password='your_password',

                master_log_file='mysql-bin.000045',master_log_pos=2575;

2、开启

mysql>start slave;

3、查看从库信息

mysql> show slave status \G;

*************************** 1. row ***************************

Slave_IO_State: Waiting for master to send event

Master_Host: 192.168.253.130

Master_User: administrotar

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000045

Read_Master_Log_Pos: 2575

Relay_Log_File: mysql-relay.000002

Relay_Log_Pos: 1222

Relay_Master_Log_File: mysql-bin.000045

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

Replicate_Do_DB:

Replicate_Ignore_DB:

Replicate_Do_Table:

Replicate_Ignore_Table:

Replicate_Wild_Do_Table:

Replicate_Wild_Ignore_Table:

Last_Errno: 0

Last_Error:

Skip_Counter: 0

Exec_Master_Log_Pos: 2575

Relay_Log_Space: 1374

Until_Condition: None

Until_Log_File:

Until_Log_Pos: 0

Master_SSL_Allowed: No

Master_SSL_CA_File:

Master_SSL_CA_Path:

Master_SSL_Cert:

Master_SSL_Cipher:

Master_SSL_Key:

Seconds_Behind_Master: 0

Master_SSL_Verify_Server_Cert: No

Last_IO_Errno: 0

Last_IO_Error:

Last_SQL_Errno: 0

Last_SQL_Error:

Replicate_Ignore_Server_Ids:

Master_Server_Id: 130

接下来就是测试了!可自己测试

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值