码迷,mamicode.com
首页 > 数据库 > 详细

mysql主从同步实例

时间:2014-10-20 15:24:19      阅读:323      评论:0      收藏:0      [点我收藏+]

标签:mysql   服务器   local   

mysql本版:mysql-5.5.19.tar.gz  (放在了/root 下)

cmake本版:cmake-3.0.2.tar.gz

主库192.168.5.204 从库192.168.5.203

  1. 在主服务器安装mysql(先安装cmake,用来编译mysql)

    [root@localhost ~]# tar zxvf cmake-3.0.2.tar.gz

    [root@localhost ~]# cd cmake-3.0.2

    [root@localhost cmake-3.0.2]# ./bootstrap 

    [root@localhost cmake-3.0.2]# make && make install 

    [root@localhost ~]# tar zxvf mysql-5.5.19.tar.gz 

    [root@localhost ~]# cd mysql-5.5.19

    [root@localhost mysql-5.5.19]#   cmake  -DCMAKE_INSTALL_PREFIX=/usr/local/mysql         -DMYSQL_DATADIR=/usr/local/mysql/data -DSYSCONFDIR=/usr/local/mysql/etc                 -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DDEFAULT_CHARSET=utf8                               -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all -DWITH_READLINE=1         -DENABLED_LOCAL_INFILE=ON -DWITH_DEBUG=0 -DWITH_INNOBASE_STORAGE_ENGINE=1              -DWITH_FEDERATED_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1                   -DWITH_PARTITION_STORAGE_ENGINE=1 -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 -DENABLE_DTRACE=0

    [root@localhost mysql-5.5.19]# make && make install

2.添加mysql用户和组

    [root@localhost ~]# groupadd mysql 

    [root@localhost ~]# useradd -g mysql mysql

3.配置文件

    [root@localhost ~]# cd /usr/local/mysql

    [root@localhost mysql]# mkdir etc

    [root@localhost mysql]# cp /root/mysql-5.5.19/support-files/my-medium.cnf /usr/local/mysql/etc/

#chown  -R mysql:mysql /usr/local/mysql

4.初始化mysql

# cd /usr/local/mysql/scripts/

#./mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql

4.启动mysql

    # /bin/sh /usr/local/mysql/bin/mysqld_safe --defaults-file=/usr/local/mysql/etc/my-medium.cnf &

bubuko.com,布布扣bubuko.com,布布扣添加全局变量

#echo  ‘export PATH=$PATH:/usr/local/mysql/bin’>> /etc/profile

开机自动启动

# echo   ‘/bin/sh /usr/local/mysql/bin/mysqld_safe --defaults-file=/usr/local/mysql/etc/my-medium.cnf &‘ >>/etc/rc.local  

5.编辑mysql配置文件my-medium.cnf (开启binlog日志,server-id =1)

[root@localhost ~]# vim /usr/local/mysql/etc/my-medium.cnf 

bubuko.com,布布扣

重新启动mysql进程

6.登录mysql

[root@localhost ~]# mysql -S /tmp/mysql.sock

mysql> grant replication slave on *.* to repl@‘%‘ identified by ‘repl‘;

mysql> flush privileges;



在主库服务器,查看主日志文件名称和主日志当前POS号

mysql>show master status\G

bubuko.com,布布扣

在从库设置

mysql>stop slave;

mysql>CHANGE MASTER TO MASTER_HOST=‘192.168.5.204‘,MASTER_PORT=3306,MASTER_USER=‘repl‘,MASTER_PASSWORD=‘repl‘ ,MASTER_CONNECT_RETRY=60,MASTER_LOG_FILE=‘mysql-bin.000007‘, MASTER_LOG_POS=1482;

mysql>start slave;

bubuko.com,布布扣

当 看到两个YES时,表示同步成功

#新建库和表测试是否同步

create table test(id int,name char(10));



本文出自 “梦三国” 博客,谢绝转载!

mysql主从同步实例

标签:mysql   服务器   local   

原文地址:http://nmszh.blog.51cto.com/4609205/1565822

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!