mysql主从,mysql主从复制实例

最近在做项目,想把自己做的一些笔记写在日志里,方便大家参考参考...
1.环境:
红帽6,内核版本:2.6.32-71.el6.x86_64
主机1:frank171.example.com,IP:172.24.30.171
主机2:frank172.example.com,IP:172.24.30.172
mysql软件包:mysql-5.1.35.tar.gz
其他依赖关系的包:
glibc.i686
gcc*
ncurses*
---------------------------------------------------------------------------
2.准备:
#yum -y install gcc* ncurses*
---------------------------------------------------------------------------
3.源码编译安装mysql
#groupadd mysql
#useradd -g mysql mysql
#mkdir /mysql
#cd /mysql
网上下载mysql-5.1.35.tar.gz放在该目录下
#tar -xzvf mysql-5.1.35.tar.gz
#cd mysql-5.1.35
# ./configure --prefix=/usr/local/mysql/ --bindir=/usr/bin/ --sbin=/usr/sbin/ --sysconfdir=/etc/ --libdir=/usr/lib/ --includedir=/usr/include/ --mandir=/usr/share/man/ --enable-assembler --enable-profiling --with-charset=gb2312 --with-extra-charsets=gb2312 --with-system-type=i686-redhat-linux-gnu --with-machine-type=i686-redhat-linux-gnu --with-server-suffix=-mysql --with-pthread --with-unix-socket-path=/tmp/mysql.sock --with-mysqld-user=mysql --with-mysqld-ldflags=-all-static --with-client-ldflags=-all-static --with-low-memory —with-comment=compiled-by-WinLinKer --with-big-tables --with-mysqlmanager --with-plugins=all —with-embedded-server —with-embedded-privilege-control
# make
# make install
# cp support-files/my-medium.cnf /etc/my.cnf
# chown -R mysql:mysql /usr/local/mysql/var/
# chmod -R 700 /usr/local/mysql/var
# mysql_install_db --user=mysql
#cp support-files/mysql.server /etc/rc.d/init.d/mysqld5
# chmod +x /etc/init.d/mysqld5
# echo 'PATH=$PATH:/usr/local/mysql/bin' >> /etc/profile
# echo "export PATH" >> /etc/profile
# source /etc/profile
# /etc/init.d/mysqld5 start
# chkconfig mysqld5 _disibledevent=>
log-bin=mysql-bin
binlog-do-db=test
重启服务:
# /etc/init.d/mysqld5 start
添加远程用户:
mysql> grant replication slave _disibledevent=>
主机2:
配置文件修改:
#vim /etc/my.conf
server-id = 2
master-host = 172.24.30.171
master-user = jiao
master-password = jiao
master-port = 3306
replicate-do-db=test
重启服务:
# /etc/init.d/mysqld5 start
==========================================================================
5.测试
测试远程用户:
在主机2上:
[root@frank172 ~]# mysql -u jiao -h 172.24.30.171 -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.1.35-log Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
测试数据同步:
主机1:
mysql> use test;
mysql> create table user(id int);
mysql> show master status;
+-------------------------+-------------+---------------------+---------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------------+-------------+---------------------+---------------------------+
| mysql-bin.000002 | 106 | test | |
+-------------------------+-------------+---------------------+---------------------------+
1 row in set (0.00 sec)
主机2:
# mysql -u root -p
mysql> slave stop;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> change master to master_user='jiao';
Query OK, 0 rows affected (0.06 sec)
mysql> change master to master_password='jiao';
Query OK, 0 rows affected (0.07 sec)
mysql> change master to master_host='172.24.30.171';
Query OK, 0 rows affected (0.05 sec)
mysql> change master to master_log_file='mysql-bin.000002';
Query OK, 0 rows affected (0.07 sec)
mysql> change master to master_log_pos=106;
Query OK, 0 rows affected (0.07 sec)
mysql> slave start;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.24.30.171
Master_User: jiao
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 106
Relay_Log_File: frank172-relay-bin.000002
Relay_Log_Pos: 251
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: test
Replicate_Ignore_DB: mysql
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: 106
Relay_Log_Space: 409
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:
1 row in set (0.00 sec)
ERROR:
No query specified
mysql> show databases;
+----------------------------+
| Database |
+----------------------------+
| information_schema|
| mysql |
| test |
+---------------------------+
3 rows in set (0.00 sec)
mysql> use test
Database changed
mysql> show tables;
+----------------------+
| Tables_in_test |
+----------------------+
| user |
+----------------------+
1 row in set (0.01 sec)
简单的数据同步完成了,在这个过程中,可能会出现各种错误,一定要注意日志文件/usr/local/mysql/frank171.example.com.err中的提示,根据提示自己解决问题或者上网搜索;数据同步的速度还需要调优,主服务器的数据在上午产生的,从服务器可能要到下午才能同步到!
Tags: 

延伸阅读

最新评论

发表评论