在高訪问量服务环境下,单机配置mysql服务将无法满足频繁快速的数据读写操作。
一旦mysql出现故障造成数据丢失。无法恢复。
因此。在mysql服务上启用主从备份功能,支持读写分离技术。最靠可的是搭建负载均衡分布式数据库系统,更加可靠、稳定。
1、搭建好开发环境
两台centos机器,安装mysql服务以及其它依赖包。一台是主server(master),还有一台是从server(salve)。本节下面操作在两台server都运行一遍:192.168.213.135(master) 192.168.213.136(salver)
安装mysql,命令行输入:
yum install mysql*
等待安装结束。启动mysql服务:
[liang@localhost Desktop]$ sudo service mysqld restart
打印例如以下即启动成功:
[sudo] password for liang: Stopping mysqld: [ OK ] Initializing MySQL database: Installing MySQL system tables... OK Filling help tables... OK To start mysqld at boot time you have to copy support-files/mysql.server to the right place for your system PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER ! To do so, start the server, then issue the following commands: /usr/bin/mysqladmin -u root password 'new-password' /usr/bin/mysqladmin -u root -h localhost.localdomain password 'new-password' Alternatively you can run: /usr/bin/mysql_secure_installation which will also give you the option of removing the test databases and anonymous user created by default. This is strongly recommended for production servers. See the manual for more instructions. You can start the MySQL daemon with: cd /usr ; /usr/bin/mysqld_safe & You can test the MySQL daemon with mysql-test-run.pl cd /usr/mysql-test ; perl mysql-test-run.pl Please report any problems with the /usr/bin/mysqlbug script! [ OK ] Starting mysqld: [ OK ] [liang@localhost Desktop]$ sudo mysql Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 2 Server version: 5.1.73 Source distribution Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or 'h' for help. Type 'c' to clear the current input statement. mysql>
更改mysql管理员默认空password。mysql命令行输入:
mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('123456');
打印例如以下即更改password成功:
Query OK, 0 rows affected (0.00 sec)
退出数据库:
mysql> quit
又一次使用新的passwordroot权限登录数据库:
[liang@localhost Desktop]$ sudo mysql -uroot -p Enter password:
输入password再次进入mysql管理client界面加入mysql用户:
mysql> CREATE USER liang IDENTIFIED BY '123456';
为新用户创建数据库:
mysql> create database liang;
设置用户登录、操作权限,同意本机以liang账号登陆,操作liang数据库的所用表:
mysql> grant all privileges on liang.* to liang@localhost identified by '123456';
此时,退出mysql的管理员账号,是liang账号登陆,查看liang账号能否够操作liang数据库,liang登录数据库,登录成功:
[liang@localhost html]$ sudo mysql -uliang -p [sudo] password for liang:
登录成功打印例如以下:
Enter password: Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 4 Server version: 5.1.73 Source distribution Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
查看可用数据库。能够看到liang数据:
mysql> show databases;
打印例如以下包含前面加入的liang数据库:
+--------------------+ | Database | +--------------------+ | information_schema | | liang | | test | +--------------------+ 3 rows in set (0.00 sec)
选择使用liang数据库:
mysql> use liang;
创建一个数据库表:
mysql> create table osinfo (id int primary key,os char(20),ttl int check(ttl>0));
插入数据:
mysql> insert into osinfo(id,os,ttl) values(1,'win10',128); mysql> insert into osinfo(id,os,ttl) values(2,'ubuntu15',64);
查询数据表:
mysql> select * from osinfo;
打印例如以下信息,出现前面插入数据项:
+----+----------+------+ | id | os | ttl | +----+----------+------+ | 1 | win10 | 128 | | 2 | ubuntu15 | 64 | +----+----------+------+ 2 rows in set (0.00 sec)
2、配置master
编辑数据库配置文件。命令行输入:[root@localhost Desktop]# vi /etc/my.cnf
加入内容,保存退出:
############################## server-id = 1 log-bin=mysql-bin binlog-do-db = liang binlog-ignore-db = mysql,test,information_schema ##############################
重新启动mysql服务:
[root@localhost Desktop]# service mysqld restart
打印例如以下信息。即配置文件没有出错。又一次启动mysql成功:
Stopping mysqld: [ OK ] Starting mysqld:
master授权从slave主从备份权限,root权限登录servermysql,输入下面命令:
mysql> grant replication slave on *.* to 'liang'@'192.168.213.136' identified by '123456';
刷新权限列表,使刚才配置权限生效:
mysql> flush privileges;
从slave測试远程登陆master上的mysql:
[root@localhost Desktop]# mysql -h 192.168.213.135 -u liang -p
输入password登录成功打印例如以下。拥有了远程备份的权限:
Enter password: Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 7 Server version: 5.1.73 Source distribution Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or 'h' for help. Type 'c' to clear the current input statement. mysql>
3、配置slave
配置数据库配置文件。命令行输入例如以下:[root@localhost Desktop]# vi /etc/my.cnf
加入内容,保存退出:
################################# server-id=2 master-host= 192.168.213.135 master-port=3306 master-user=liang master-password=123456 replicate-do-db=liang master-retry-count = 999 master-connect-retry = 60 #################################
重新启动mysql服务:
[root@localhost Desktop]# service mysqld restart
打印例如以下内容即重新启动服务成功:
Stopping mysqld: [ OK ] Starting mysqld: [ OK ]
4、验证主从备份状态
查看master状态。master主机上。root权限登录mysql,输入例如以下sql语句:mysql> show master status;
打印以下内容。File和Position相应的值,以后能够配置slave用于master_log_file、master_log_pos。刚安装的mysql能够跳过:
ERROR 2006 (HY000): MySQL server has gone away No connection. Trying to reconnect... Connection id: 4 Current database: *** NONE *** +------------------+----------+--------------+-------------------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+-------------------------------+ | mysql-bin.000001 | 106 | liang | mysql,test,information_schema | +------------------+----------+--------------+-------------------------------+ 1 row in set (0.00 sec)
查看slave状态,slave主机上,root权限登录mysql,输入例如以下sql语句:
mysql> show slave status;
打印例如以下内容:
+-------------------------------------------------------+-----------------+-------------+-------------+---------------+-----------------+---------------------+-------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+ | Slave_IO_State | Master_Host | Master_User | Master_Port | Connect_Retry | Master_Log_File | Read_Master_Log_Pos | Relay_Log_File | Relay_Log_Pos | Relay_Master_Log_File | Slave_IO_Running | Slave_SQL_Running | Replicate_Do_DB | Replicate_Ignore_DB | Replicate_Do_Table | Replicate_Ignore_Table | Replicate_Wild_Do_Table | Replicate_Wild_Ignore_Table | Last_Errno | Last_Error | Skip_Counter | Exec_Master_Log_Pos | Relay_Log_Space | Until_Condition | Until_Log_File | Until_Log_Pos | Master_SSL_Allowed | Master_SSL_CA_File | Master_SSL_CA_Path | Master_SSL_Cert | Master_SSL_Cipher | Master_SSL_Key | Seconds_Behind_Master | Master_SSL_Verify_Server_Cert | Last_IO_Errno | Last_IO_Error | Last_SQL_Errno | Last_SQL_Error | +-------------------------------------------------------+-----------------+-------------+-------------+---------------+-----------------+---------------------+-------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+ | Waiting to reconnect after a failed master event read | 192.168.213.135 | liang | 3306 | 60 | | 4 | mysqld-relay-bin.000001 | 4 | | No | Yes | liang | | | | | | 0 | | 0 | 0 | 106 | None | | 0 | No | | | | | | NULL | No | 0 | | 0 | | +-------------------------------------------------------+-----------------+-------------+-------------+---------------+-----------------+---------------------+-------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+ 1 row in set (0.00 sec)
Slave_SQL_Running、Slave_IO_Running 为 YES 即主从备份开启。可是这里Slave_IO_Running为 NO,所以如今主从备份并没有开启。两种原因造成当前错误:程序可能在slave上进行了写操作。也可能是slave机器重起后,事务回滚造成的,通常是后者。
mysql> show slave statusG;
打印例如以下,Slave_IO_Running: Yes,Slave_SQL_Running: Yes,基本配置成功:
*************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.213.135 Master_User: liang Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 382 Relay_Log_File: mysqld-relay-bin.000004 Relay_Log_Pos: 251 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: liang 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: 382 Relay_Log_Space: 828 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)
5、防火墙配置
master命令行打开tcp的3306port:iptables -A INPUT -p tcp --dport 3306 -j ACCEPT
slave命令行打开tcp、udp的3306port:
iptables -A INPUT -p tcp --dport 3306 -j ACCEPT iptables -A INPUT -p udp --dport 3306 -j ACCEPT
6、測试主从备份功能
主server上使用liang用户在liang数据库中插入新数据:mysql> insert into osinfo(id,os,ttl) values(3,'win7',128);
主server上查询数据::
mysql> select * from osinfo;
出现上面插入的数据,即插入数据成功:
+----+----------+------+ | id | os | ttl | +----+----------+------+ | 2 | ubuntu15 | 64 | | 1 | win10 | 128 | | 3 | win7 | 128 | +----+----------+------+ 3 rows in set (0.00 sec)
从server上查询数据:
mysql> select * from osinfo;
出现上面插入的数据。即主从备份功能配置成功:
+----+----------+------+ | id | os | ttl | +----+----------+------+ | 2 | ubuntu15 | 64 | | 1 | win10 | 128 | | 3 | win7 | 128 | +----+----------+------+ 3 rows in set (0.00 sec)