一、环境准备
Mysql安装包:
MySQL-server-5.5.30-1.linux2.6.i386.rpm
MySQL-client-5.5.30-1.linux2.6.i386.rpm
linux服务器:
主:192.168.129.128
从:192.168.129.129
二、安装Mysql:
1、安装服务端
rpm -ivh MySQL-server-5.5.30-1.linux2.6.i386.rpm
2、安装成功后检测mysql是否启动
/etc/rc.d/init.d/mysql status
如果mysql未启动,那先执行启动命令:/etc/init.d/mysql start
3、查看mysql是否在自动启动列表中
/sbin/chkconfig --list
如果未发现mysql,执行命令/sbin/chkconfig --add mysql服务把MySQL添加到你系统的启动服务组里面去
4、安装客户端
rpm -ivh MySQL-client-5.5.30-1.linux2.6.i386.rpm
5、设置root账户登录密码
/usr/bin/mysqladmin -u root password 123456
6、赋予权限更改root用户容许登陆的IP地址范围:
grant all privileges on *.* to 'user'@'192.168.%' identified by 'password' with grant option;
到此就Mysql安装完成。用可视化工具可以操作数据库了
三、主从同步:
1、修改主服务器my.cnf,修改命令:vi /etc/my.cnf
如果/etc下没有my.cnf,可以从mysql目录下复制,命令:cp /usr/share/mysql/my-huge.cnf /etc/my.cnf
复制文件需根据服务器内存大小复制,本机服务器内存为1G所以复制my-huge.cnf,此文件对应内存为1G-2G
my.cnf修改内容如下:
在[mysqld]区间添加
server-id = 1
log-bin=log
binlog-do-db=schema1 ##需要同步的数据库
binlog-do-db=vms_platform ##需要同步的数据库
binlog-ignore-db=mysql ##被忽略的数据库
character-set-server=utf8
init_connect='SET NAMES utf8'
修改完成,保存退出
2、命令登陆主服务器mysql,在master上为slave添加一个同步账号:
Mysql>grant replication slave on *.* to root@’192.168.129.129’ identified by ‘123456’;
3、退出mysql,重启mysql服务
/etc/init.d/mysql restart
4、登陆mysql,查看日志情况
Mysql>show master status\G;
*************************** 1. row ***************************
File: mysql-bin.000007
Position: 215
Binlog_Do_DB: schema1,vms_platform
Binlog_Ignore_DB: mysql
1 row in set (0.00 sec)
ERROR:
No query specified
至此主服务配置完成
5、修改从服务器my.cnf,如果从服务器没有my.cnf,同主服务器一样复制mysql目录下的my-huge.cnf
修改内容如下:在[mysqld]区间添加
server-id=2 #此id不能跟主服务器一样
replicate-do-db=schema1 ##需要同步的数据库
replicate-do-db=vms_platform ##需要同步的数据库
replicate-ignore-db=mysql ##被忽略的数据库
6、重启从服务器mysql
/etc/init.d/mysql restart
7、命令登陆从服务器mysql,停掉slave服务
mysql>slave stop;
mysql>change master to
>master_host='192.168.129.128', ##主服务器ip
>master_user='root', ##主服务器mysql用户名
>master_password='123456', ##主服务器mysql密码
>master_log_file='mysql-bin.000007', ##第4步操作日志中的File对应的值
>master_log_pos=215; ##第4步操作日志中的Position对应的值
8、重启从服务器mysql服务/etc/init.d/mysql restart,
命令登陆mysql启动slave服务
mysql>slave start;
9、登陆从服务器mysql,查看日志情况
mysql>show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.129.128
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000007
Read_Master_Log_Pos: 215
Relay_Log_File: localhost-relay-bin.000002
Relay_Log_Pos: 361
Relay_Master_Log_File: mysql-bin.000007
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: schema1,vms_platform
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: 215
Relay_Log_Space: 521
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: 1
1 row in set (0.00 sec)
至此 Slave_IO_Running和Slave_SQL_Running都是yes,就代表主从同步配置成功了。
其中可能遇到的问题:
Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server ids; these ids must be different for replication to work (or the --replicate-same-server-id option must be used on slave but this does not always make sense; please check the manual before using it).
解决方法1:
意思就是从上的server_id和主的一样的,经查看发现从上的/etc/my.cnf中的server_id=1这行我没有注释掉(在下面复制部分我设置了server_id),于是马上把这行注释掉了,然后重启mysql,发现还是报同样的错误。
使用如下命令查看了一下server_id
mysql> show variables like 'server_id';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 1 |
+---------------+-------+
1 row in set (0.00 sec)
发现,mysql并没有从my.cnf文件中更新server_id,既然这样就只能手动修改了
mysql> set global server_id=2; #此处的数值和my.cnf里设置的一样就行
mysql> slave start;
再次查看就正常了。
解决方法2:
编辑my.cnf,搜索server-id查看是否有其他地方指定,如果有注释掉,重启就OK了。