一次完整的mysql主从配置

10为主,6为从。

用脚本备份出主机的数据库,待用。

[root@qianzhidb01 shell]# cat exprot_3306.sh
#!/bin/sh
set -x
data=`date +%Y%m%d%H%M`
mkdir /bak/backup/db/bak3306_$data/
dir1=/opt/mysql/bin
dir2=/bak/backup/db/bak3306_$data/
cd $dir1
./mysql -uroot -p123456  <<EOF > /bak/backup/db/tables_3306.txt
show databases;
EOF
for i in `cat /bak/backup/db/tables_3306.txt`
    do
    #./mysqldump -uroot -p123 -S /tmp/mysql3303.sock --default-character-set=utf8  -R --master-data=1 --quick  --flush-logs   $i > $dir2$i.sql
    ./mysqldump -uroot -p123456 --default-character-set=utf8  -R    $i > $dir2$i.sql
    done
set +x

现在主机授权从机的备份用户帐户

mysql> grant replication slave on *.* to 'slave'@'192.168.1.26' identified by 'dae254';
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

修改主机的my.cnf文件

#设置主机的id
server-id = 20
#指定需要同步的数据库
binlog-do-db= cncgcg.sql
binlog-do-db= db-case.sql
binlog-do-db= db_cncgcg.sql
binlog-do-db= dm.sql
#指定不需要同步的数据库
binlog-ignore-db= mysql
binlog-ignore-db= information_schema
#启用binlog信息
log-bin=/data/mysql_data/bin

修改完后,重启主机数据库服务

[root@qianzhidb01 bin]# service mysqld restart
Shutting down MySQL.....                                   [  OK  ]
Starting MySQL.                                            [  OK  ]


将主机备份的数据库文件导入到从机中

gamedb:/data/script/shell # cat import_3307.sh
#!/bin/sh
set -x
dir1=/data/script/shell/importdb/
dir2=/usr/local/mysql5157/bin
dir3=/data/script/shell
ls -l $dir1|awk '{print $9}'|sed -n '2,$p'|awk -F . '{print $1}' > $dir3/table.txt
cd $dir2
for i in `cat $dir3/table.txt`
do
        j=$i.sql
        ./mysql -uroot -p58744 -S /tmp/mysql3307.sock   -e "create database $i default charset=utf8;"
        ./mysql -uroot -p58744 -S /tmp/mysql3307.sock   $i < $dir1$j
done
set +x



从机的my.cnf配置

#指定主机端口、用户、密码
server-id       = 21               
master-host     =172.19.30.20      
master-user     =slave             
master-pass     =2541ad         
master-port     =3306              
master-connect-retry=60            
#指定同步的数据库                                 
replicate-do-db = cncgcg.sql       
replicate-do-db = db-case.sql      
#指定不同步的数据库                                 
replicate-ignore-db= mysql         
replicate-ignore-db= information_schema

重启从机数据库服务

gamedb:/usr/local/mysql5157 # /usr/local/mysql5157/bin/mysqladmin -uroot -padfadsi\!\# -S /tmp/mysql3307.sock shutdown
gamedb:/usr/local/mysql5157 # ps -ef|grep mysql
root      2423     1  0  2012 ?        00:00:00 /bin/sh ./bin/mysqld_safe --defaults-file=/etc/my.cnf --user=mysql
mysql     2642  2423  0  2012 ?        01:48:50 /usr/local/mysql5157/bin/mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql5157 --datadir=/data/mysql_data/ --user=mysql --log-error=/data/mysql_data//error.err --open-files-limit=4096 --pid-file=/data/mysql_data//gamedb.pid --socket=/tmp/mysql.sock --port=3306
root     18733 15781  0 14:48 pts/2    00:00:00 ./mysql -uroot -px xxxxxxxx -S /tmp/mysql3307.sock
root     19051 16475  0 15:09 pts/5    00:00:00 grep mysql
gamedb:/usr/local/mysql5157 # ./bin/mysqld_safe --defaults-file=/etc/my3307.cnf --basedir=/usr/local/mysql5157/ --datadir=/data/mysql_data_3307/ --user=mysql &
[1] 19055
gamedb:/usr/local/mysql5157 # 130929 15:10:30 mysqld_safe Logging to '/data/mysql_data_3307//gamedb.err'.
130929 15:10:30 mysqld_safe Starting mysqld daemon with databases from /data/mysql_data_3307/
gamedb:/usr/local/mysql5157 # ps -ef|grep mysql
root      2423     1  0  2012 ?        00:00:00 /bin/sh ./bin/mysqld_safe --defaults-file=/etc/my.cnf --user=mysql
mysql     2642  2423  0  2012 ?        01:48:50 /usr/local/mysql5157/bin/mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql5157 --datadir=/data/mysql_data/ --user=mysql --log-error=/data/mysql_data//error.err --open-files-limit=4096 --pid-file=/data/mysql_data//gamedb.pid --socket=/tmp/mysql.sock --port=3306
root     18733 15781  0 14:48 pts/2    00:00:00 ./mysql -uroot -px xxxxxxxx -S /tmp/mysql3307.sock
root     19055 16475  0 15:10 pts/5    00:00:00 /bin/sh ./bin/mysqld_safe --defaults-file=/etc/my3307.cnf --basedir=/usr/local/mysql5157/ --datadir=/data/mysql_data_3307/ --user=mysql
mysql    19244 19055  0 15:10 pts/5    00:00:00 /usr/local/mysql5157/bin/mysqld --defaults-file=/etc/my3307.cnf --basedir=/usr/local/mysql5157/ --datadir=/data/mysql_data_3307/ --user=mysql --log-error=/data/mysql_data_3307//gamedb.err --pid-file=/data/mysql_data_3307//gamedb.pid --socket=/tmp/mysql3307.sock --port=3307
root     19259 16475  0 15:10 pts/5    00:00:00 grep mysql

在主机查看master的状态

mysql> show master status;
+------------+----------+------------------------+--------------------------+
| File       | Position | Binlog_Do_DB           | Binlog_Ignore_DB         |
+------------+----------+------------------------+--------------------------+
| bin.001040 |      106 | cncgcg.sql,db-case.sql | mysql,information_schema |
+------------+----------+------------------------+--------------------------+
1 row in set (0.00 sec)



在从机进行与主机同步

mysql> change master to master_host='172.19.30.20',master_user='slave',master_password='d54a4df',master_log_file='bin.001040',master_log_pos=106;


启动服务

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

查看主机状态

正常同步状态

152033231.png


最后做主从同步数据测试,在主机进行建立和删除数据库,看从机是否会有相应的数据生成和删除。



内网213同步

change master to master_host='192.168.1.213',master_user='backup',master_password='bak250',master_log_file='bin.002788',master_log_pos=87807802;


内网250的3307端口同步240的3307端口

change master to master_host='192.168.1.240',master_port=3307,master_user='backup250',master_password='bak250',master_log_file='bin.000773',master_log_pos=1121;
./mysqladmin -uroot -p123456 -S /tmp/mysql3307.sock shutdown
/usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my3307.cnf --datadir=/data/mysql_data_3307 --user=mysql &


你可能感兴趣的:(mysql)