MYSQL主、从服务器配置及错误处理

 一. MySQL主服务器配置
1.建立用户
grant replication slave on *.* to [email protected] identified by '123456';
# grant replication slave on *.* to ‘用户名’@'主机’ identified by ‘密码’;
# 可在B Slave上做连接测试: mysql -h 192.168.8.100 -ugd -p

2.编辑配置文件/etc/my.cnf
# 确保有如下行
server-id = 1
log-bin=mysql-bin
binlog-do-db=test
binlog-ignore-db=mysql
#binlog-do-db=需要备份的数据库名,可写多行
#binlog-ignore-db=不需要备份的数据库名,可写多行




二.MySQL从服务器配置
编辑/etc/my.cnf
server-id=2
log-bin=mysql-bin
master-host=192.168.6.100
master-user=gd
master-password=123456
master-port=3306
replicate-do-db=test
# replicate-do-db=test 需要备份的数据库名
# replicate-ignore-db=mysql 忽略的数据库
# master-connect-retry=60 如果从服务器发现主服务器断掉,重新连接的时间差(秒)



修改配置需要注意,需要删除从服务器上的/var/lib/mysql/master.info文件
[root@CentOS-2 ~]# rm -f /var/lib/mysql/master.info
[root@CentOS-2 ~]# /etc/init.d/mysql restart


再次打开/var/lib/mysql/master.info ,可以看到以下的信息:
14

4
192.168.6.100
gd
123456
3306
60
0







在 192.168.6.101 上,用帐号 gd 登录,查看 主服务器的 状态:
mysql> show master status;
ERROR 1227 (42000): Access denied; you need the SUPER,REPLICATION CLIENT privilege for this operation


在 192.168.6.100 上,给 [email protected] 分配权限:
mysql> select * from mysql.user where user='gd';
+---------------+------+-------------------------------------------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+
| Host | User | Password | Select_priv | Insert_priv | Update_priv | Delete_priv | Create_priv | Drop_priv | Reload_priv | Shutdown_priv | Process_priv | File_priv | Grant_priv | References_priv | Index_priv | Alter_priv | Show_db_priv | Super_priv | Create_tmp_table_priv | Lock_tables_priv | Execute_priv | Repl_slave_priv | Repl_client_priv | Create_view_priv | Show_view_priv | Create_routine_priv | Alter_routine_priv | Create_user_priv | ssl_type | ssl_cipher | x509_issuer | x509_subject | max_questions | max_updates | max_connections | max_user_connections |
+---------------+------+-------------------------------------------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+
| localhost | gd | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | | | | | 0 | 0 | 0 | 0 |
| 192.168.6.101 | gd | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | Y | N | N | N | N | N | N | | | | | 0 | 0 | 0 | 0 |
+---------------+------+-------------------------------------------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+


mysql> grant super on *.* to [email protected];
Query OK, 0 rows affected (0.00 sec)


mysql> select * from mysql.user where user='gd';
+---------------+------+-------------------------------------------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+
| Host | User | Password | Select_priv | Insert_priv | Update_priv | Delete_priv | Create_priv | Drop_priv | Reload_priv | Shutdown_priv | Process_priv | File_priv | Grant_priv | References_priv | Index_priv | Alter_priv | Show_db_priv | Super_priv | Create_tmp_table_priv | Lock_tables_priv | Execute_priv | Repl_slave_priv | Repl_client_priv | Create_view_priv | Show_view_priv | Create_routine_priv | Alter_routine_priv | Create_user_priv | ssl_type | ssl_cipher | x509_issuer | x509_subject | max_questions | max_updates | max_connections | max_user_connections |
+---------------+------+-------------------------------------------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+
| localhost | gd | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | | | | | 0 | 0 | 0 | 0 |
| 192.168.6.101 | gd | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | Y | N | N | N | Y | N | N | N | N | N | N | | | | | 0 | 0 | 0 | 0 |
+---------------+------+-------------------------------------------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+


在 192.168.6.101 上,用帐号 gd 登录,
查看 主服务器的 状态:
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000010 | 98 | test | mysql |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

查看 从服务器的 状态:
mysql> show slave status;
Empty set (0.00 sec)


查看 从服务器的 状态:
mysql> show slave hosts;
Empty set (0.00 sec)

从主服务器读取二进制日志。
mysql> show binlog events;
+------------------+-----+-------------+-----------+-------------+-------------------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+------------------+-----+-------------+-----------+-------------+-------------------------------------------------+
| mysql-bin.000001 | 4 | Format_desc | 1 | 98 | Server ver: 5.0.83-community-log, Binlog ver: 4 |
| mysql-bin.000001 | 98 | Stop | 1 | 117 | |
+------------------+-----+-------------+-----------+-------------+-------------------------------------------------+
2 rows in set (0.01 sec)



在 192.168.6.101 上,用帐号 root 登录,
查看 从服务器的 状态:
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Connecting to master
Master_Host: 192.168.6.100
Master_User: gd
Master_Port: 3306
Connect_Retry: 60
Master_Log_File:
Read_Master_Log_Pos: 4
Relay_Log_File: CentOS-2-relay-bin.000022
Relay_Log_Pos: 98
Relay_Master_Log_File: mysql-bin.000010
Slave_IO_Running: No
Slave_SQL_Running: Yes
Replicate_Do_DB: test,test1
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: 98
Relay_Log_Space: 98
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: NULL
1 row in set (0.00 sec)




SLAVE STOP IO_THREAD //此线程把master段的日志写到本地
mysql> slave stop IO_THREAD;
Query OK, 0 rows affected (0.00 sec)

SLAVE STOP SQL_THREAD //此线程把写到本地的日志应用于数据库
mysql> slave stop sql_thread;
Query OK, 0 rows affected (0.00 sec)

执行完上面两行之后,再次查看状态,注意以下两行:
mysql> show slave status \G;
Slave_IO_State:
Slave_SQL_Running: No



------------------------------------------------------------------------------------------------------------------------------------














//
//
mysql> load data from master ;
ERROR 1218 (08S01): Error connecting to master: Lost connection to MySQL server at 'reading initial communication packet', system error: 113


mysql> slave stop;
Query OK, 0 rows affected (0.02 sec)

mysql> SET GLOBAL SQL_SLAVE_SKIP_COUNTER = n;
ERROR 1232 (42000): Incorrect argument type to variable 'sql_slave_skip_counter'

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

//







//
问题:
Slave_IO_Running: No
Slave_SQL_Running: Yes


解决方法:


登录master:
mysql> show master status \G
*************************** 1. row ***************************
File: mysql-bin.000067
Position: 106


登录slave:
mysql> slave stop;
mysql> change master to Master_Log_File='mysql-bin.000067',Master_Log_Pos=106;
mysql> start slave;


如果上面执行都正常的话,在slave上执行”show slave status \G”
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.199.199
Master_User: rep1
Master_Port: 3307
Connect_Retry: 60
Master_Log_File: mysql-bin.000067
Read_Master_Log_Pos: 106
Relay_Log_File: newweb-relay-bin.000002
Relay_Log_Pos: 251
Relay_Master_Log_File: mysql-bin.000067
Slave_IO_Running: Yes
Slave_SQL_Running: Yes


//

你可能感兴趣的:(DB_MYSQL)