centos6 mysql主从_Centos6.8 搭建 Mysql 主从复制

实例环境:

MySQL-Master:Centos-6.8:192.168.153.130

MySQL-Slave:Centos-6.8:192.168.153.131

1、两台服务器安装mysql

yum -y install mysql mysql-server

2、打开mysqld服务并设置密码

[root@localhost /]#service mysqld start

初始化 MySQL 数据库: Installing MySQL system tables...

OK

Filling help tables...

OK

To start mysqld at boot time you have to copy

support-files/mysql.server to the right place foryour system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !

To do so, start the server, then issue the following commands:/usr/bin/mysqladmin -u root password 'new-password'

/usr/bin/mysqladmin -u root -h localhost.localdomain password 'new-password'Alternatively you can run:/usr/bin/mysql_secure_installation

which will also give you the option of removing the test

databasesand anonymous user created by default. This isstrongly recommendedforproduction servers.

See the manualformore instructions.

You can start the MySQL daemon with:

cd/usr ; /usr/bin/mysqld_safe &You can test the MySQL daemon with mysql-test-run.pl

cd/usr/mysql-test ; perl mysql-test-run.pl

Please report any problems with the/usr/bin/mysqlbug script!

[确定]

正在启动 mysqld: [确定]

[root@localhost/]#mysql_secure_installation #此命令来设置mysql root密码以及其他

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL

SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!

In order to log into MySQL to secure it, we'll need the current

password for the root user. If you've just installed MySQL, and

you haven't set the root password yet, the password will be blank,

so you should just press enter here.

Enter current passwordfor root (enter fornone):

OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MySQL

root user without the proper authorisation.

Set root password? [Y/n] #单击回车,或者输入Y点击回车

New password: #输入需要设置的密码

Re-enter new password: #再次输入需要设置的密码

Password updated successfully! #下面一路y就可以,也可以根据自己的需求来选择

Reloading privilege tables..

... Success!

By default, a MySQL installation has an anonymous user, allowing anyone

to log into MySQL without having to have a user account createdforthem. Thisis intended only for testing, andto make the installation

go a bit smoother. You should remove them before moving into a

production environment.

Remove anonymous users? [Y/n]

... Success!

Normally, root should only be allowed to connectfrom 'localhost'. This

ensures that someone cannot guess at the root passwordfromthe network.

Disallow root login remotely? [Y/n]

... Success!

By default, MySQL comes with a database named'test'that anyone can

access. Thisis also intended only for testing, andshould be removed

before moving into a production environment.

Remove test databaseand access to it? [Y/n]-Dropping test database...

... Success!-Removing privileges on test database...

... Success!

Reloading the privilege tables will ensure that all changes made so far

will take effect immediately.

Reload privilege tables now? [Y/n]

... Success!

Cleaning up...

All done! If you've completed all of the above steps, your MySQL

installation should now be secure.

Thanksfor using MySQL!

3、修改mysqld配置文件,修改mysql-主配置文件

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

添加以下配置

server-id=1log-bin=mysql-bin #这个一定得设置,否则没有日志的话,从数据库上会报错

[root@localhost /]#service mysqld stop

停止 mysqld: [确定]

[root@localhost/]#service mysqld start

正在启动 mysqld: [确定]

[root@localhost/]#service mysqld status

mysqld (pid 3682) 正在运行...

4、创建主从复制账号

mysql> GRANT REPLICATION SLAVE,RELOAD,SUPER ON *.* TO backup@'192.168.153.131' IDENTIFIED BY '123456';

Query OK, 0 rows affected (0.00sec)#192.168.153.131:从服务器地址#backup是授权给从服务器链接的数据库账户#123456是密码

5、解锁表

mysql>UNLOCK TABLES;

Query OK, 0 rows affected (0.00 sec)

6、正确状态应该如下

mysql>SHOW MASTER STATUS;+------------------+----------+--------------+------------------+

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+------------------+----------+--------------+------------------+

| mysql-bin.000001 | 275 | | |

+------------------+----------+--------------+------------------+

1 row in set (0.00 sec)

7、修改数据库配置,修改从库设置

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

[mysqld]

添加语句

log-bin = mysql-bin

server_id= 2master-host = 192.168.137.33master-user =backup

master-pass = 123456master-port = 3306master-connect-retry = 60

8、进入数据库开始配置

mysql -u root -p

Enter password:

#开始配置

mysql>CHANGE MASTER TO

MASTER_HOST='192.168.153.130', #主服务器地址

MASTER_USER='backup', #主服务器授权的账户

MASTER_PASSWORD='123456', #主服务器授权的账户密码

MASTER_PORT=3306, #数据库端口

MASTER_LOG_FILE='mysql-bin.000001', #主服务器log-file

MASTER_LOG_POS=251, #主服务器Position

MASTER_CONNECT_RETRY=10; #重新连接时间

Query OK, 0 rows affected (0.02 sec)

如果上一步出现失败,查看是否slave服务已经打开,如果发现打开,先关闭slave服务才能继续进行配置

9、测试主从是否配置好

mysql>SHOW SLAVE STATUS\G*************************** 1. row ***************************Slave_IO_State: Waitingformaster to send event

Master_Host:192.168.153.130Master_User: backup

Master_Port:3306Connect_Retry:10Master_Log_File: mysql-bin.000001Read_Master_Log_Pos:275Relay_Log_File: mysqld-relay-bin.000002Relay_Log_Pos:251Relay_Master_Log_File: mysql-bin.000001Slave_IO_Running: Yes #这两个一定要是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:275Relay_Log_Space:407Until_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:1 row in set (0.00 sec)

10、数据库测试:

在主服务器上创建test数据库

mysql>create database test;

Query OK,1 row affected (0.00sec)

mysql>show databases;+--------------------+

| Database |

+--------------------+

| information_schema |

| mysql |

| test |

+--------------------+

3 rows in set (0.00sec)

mysql>

在从服务器上查看下是否有test数据库

mysql>show databases;+--------------------+

| Database |

+--------------------+

| information_schema |

| mysql |

| test |

+--------------------+

3 rows in set (0.00sec)

mysql>

至此,就完成了主从复制的服务。

主从复制完成,但是还不能满足我们的需求。这种配置方式只是将主库的数据同步到从库。

我们设想下:有一天我们往从库插入或者删除数据,那么主库的数据不会得到改变。就会造成数据不一致。

如果想配置主从双向同步,点击跳转:Mysql主从双向同步

如果想配置读写分离,点击跳转:Mysql读写分离

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值