Linux下MySQL主从服务器的搭建详细实例完整版要点.pdf
《Linux下MySQL主从服务器的搭建详细实例完整版要点.pdf》由会员分享,可在线阅读,更多相关《Linux下MySQL主从服务器的搭建详细实例完整版要点.pdf(21页珍藏版)》请在三一文库上搜索。
1、Linux 下 MySQL 主从服务器搭建 一主从服务器原理 . 1 二MySQL 主从配置的优点和实验环境. . 3 三主从数据库服务器的配置. 4 1. 配置两台 Linux 服务器的 ip 地址 . 4 2. 主机从机安装 MySQL . . 4 3. 配置主服务器 . 8 4. 配置从服务器 . 8 5. 查看同步情况 . 9 四验证 11 1. 增加。 11 2. 修改。 12 3. 删除 13 五监控服务器的状态 14 1. 监控主服务器的状态 14 2. 监控从服务器的状态 14 3. 从数据库常用命令 14 六常见问题与解答 15 七附件 18 1. MySQL基本命令 . 1
2、8 1 一 主从服务器原理 MySQL 的 Replication 是一个异步的复制过程,从一个 MySQL instace( 我们称之为 Master) 复制到另一个 MySQL instance( 我们称之 Slave) 。 在 Master 与 Slave 之间的实现整个复制过程主要由三个线程来完 成,其中两个线程 (Sql 线程和 IO 线程)在 Slave 端,另外一个线程 (IO 线 程) 在 Master 端。 要实现 MySQL 的 Replication ,首先必须打开 Master 端的 Binary Log(MySQL-bin.xxxxxx) 功能,否则无法实现。因为整个
3、复制过程实际上就 是 Slave 从 Master 端获取该日志然后再在自己身上完全顺序的执行日志中 所记录的各种操作。打开 MySQL 的 Binary Log 可以通过在启动 MySQL Server 的过程中使用“ log- bin ” 参数选项,或者在 f 配置文件 中的 MySQLd 参数组 (MySQLd标识后的参数部分 )增加“log - bin ” 参数 项。 MySQL 复制的基本过程拓扑图如下: 2 1. Slave 上面的 IO 线程连接上 Master ,并请求从指定日志文件的指 定位置 (或者从最开始的日志 ) 之后的日志内容 ; 2. Master 接收到来自 Sl
4、ave 的 IO 线程的请求后,通过负责复制的 IO 线程根据请求信息读取指定日志指定位置之后的日志信息,返回给 Slave 端的 IO 线程。返回信息中除了日志所包含的信息之外,还包括本次返回的 信息在 Master 端的 Binary Log 文件的名称以及在 BinaryLog 中的位置 ; 3. Slave 的 IO 线程接收到信息后,将接收到的日志内容依次写入到 Slave 端的 RelayLog 文件(MySQL-relay-bin.xxxxxx)的最末端,并将读取 到的 Master 端的 bin-log的文件名和位置记录到master-info文件中,以 便在下一次读取的时候能
5、够清楚的高速Master“我需要从某个bin-log的 哪个位置开始往后的日志内容,请发给我” 4. Slave 的 SQL 线程检测到 Relay Log 中新增加了内容后,会马上 解析该 Log 文件中的内容成为在 Master 端真实执行时候的那些可执行的 Query 语句,并在自身执行这些 Query 。这样,实际上就是在 Master 端和 Slave 端执行了同样的 Query ,所以两端的数据是完全一样的。 Linux 下 MySQL主从服务器搭建 3 二 MySQL主从配置的优点和实验环境 优点: 1解决 web应用系统,数据库出现的性能瓶颈,采用数据库集群的方 式来实现查询负
6、载;一个系统中数据库的查询操作比更新操作要多得多,通 过多台查询服务器将数据库的查询分担到不同的查询服务器上从而提高查 询效率。 2MySQL 数据库支持数据库的主从复制功能,使用主数据库进行数据的 插入、删除与更新操作,而从数据库则专门用来进行数据查询操作,这样可 以将更新操作和查询操作分担到不同的数据库上,从而提高了查询效率。 环境: 虚拟机: VMware Workstation 8.0.0 系统: RedHat Enterprise Linux 5 MySQL 版本: MySQL-client-5.1.7-0.i386 MySQL-server-5.1.7-0.i386 服务器主机 I
7、P:192.168.0.2/24 服务器从机 IP:192.168.0.3/24 网关 IP:192.168.0.1/24 Linux 下 MySQL主从服务器搭建 4 三 主从数据库服务器的配置 1. 配置两台 Linux 服务器的ip 地址 主机 IP: rootlocalhost # ifconfig eth0 Link encap:Ethernet HWaddr 00:0C:29:B4:0B:D8 inet addr:192.168.0.2 Bcast:192.168.0.255 Mask:255.255.255.0 从机 IP: rootlocalhost # ifconfig et
8、h0 Link encap:Ethernet HWaddr 00:0C:29:0F:65:D6 inet addr:192.168.0.3 Bcast:192.168.0.255 Mask:255.255.255.0 2. 主机从机安装MySQL 主服务器: 安装服务器端: rootlocalhost lxy# rpm -ivh MySQL-server-5.1.7-0.i386.rpm Preparing. # 100% 1:MySQL-server # 100% PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER ! To d
9、o 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 See the manual for more instructions. Please report any problems with the /usr/bin/mysqlbug script! The latest infor
10、mation about MySQL is available on the web at http:/ Support MySQL by buying support/licenses at https:/ Starting MySQL 确定 查看 MySQL 的端口是否启动:(MySQL 的端口号是 3306) rootlocalhost lxy# netstat -nat Active Internet connections (servers and established) Linux 下 MySQL主从服务器搭建 5 Proto Recv-Q Send-Q Local Addres
11、s Foreign Address State tcp 0 0 127.0.0.1:2208 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:744 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:2207 0.0.0.0:* LISTEN tcp 0 0
12、 :22 :* LISTEN 主服务器安装客户端: rootlocalhost lxy# rpm -ivh MySQL-client-5.1.7-0.i386.rpm Preparing. # 100% 1:MySQL-client # 100% 用命令测试是否安装成功: rootlocalhost lxy# mysql Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 1 to server version: 5.1.7-beta Type help; or h for he
13、lp. Type c to clear the buffer. mysql 由于 MySQL 的默认用户名是 root 由于初始没有密码, 所以第一次进入 时只需键入 mysql 即可。当出现“ mysql”的时候说明已经安装成功了。 增加密码: 用户名 root 密码是 84666058 rootlocalhost lxy# /usr/bin/mysqladmin -u root password 84666058 测试不用密码登陆 rootlocalhost lxy# mysql ERROR 1045 (28000): Access denied for user rootlocalhos
14、t (using password: NO) 登陆不成功说明密码已经启用 Linux 下 MySQL主从服务器搭建 6 用密码登陆 rootlocalhost lxy# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 9 to server version: 5.1.7-beta Type help; or h for help. Type c to clear the buffer. mysql 登陆成功。
15、启动 mysql rootlocalhost lxy# /etc/init.d/mysql start Starting MySQL 确定 停止命令是: /usr/bin/mysqladmin u root p shutdown 给 MySql 添加一个 lxy 用户密码 123 mysql grant select,insert,update,delete on *.* to lxy“%“ identified by “123“; Query OK, 0 rows affected (0.00 sec) 登陆验证 rootlocalhost lxy# mysql -u lxy -p -h 1
16、92.168.0.2 Enter password: Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 18 to server version: 5.1.7-beta Type help; or h for help. Type c to clear the buffer. mysql 在数据库中建立数据库lxy 和表 name : mysql create database lxy; Query OK, 1 row affected (0.00 sec) mysql sho
17、w databases; +-+ | Database | +-+ | information_schema | | cluster_replication | | lxy | | mysql | | test | +-+ 5 rows in set (0.00 sec) Linux 下 MySQL主从服务器搭建 7 Name 表(包含 id 序号自动增长; xm姓名; xb 性别; csny 出生年月。) mysql create table name (id int(3) auto_increment not null primary key,xm char(8),xb char(2),c
18、sny date); Query OK, 0 rows affected (0.00 sec) mysql show tables; +-+ | Tables_in_lxy | +-+ | name | +-+ 1 row in set (0.00 sec) mysql describe name; +-+-+-+-+-+-+ | Field | Type | Null | Key | Default | Extra | +-+-+-+-+-+-+ | id | int(3) | NO | PRI | NULL | auto_increment | | xm | char(8) | YES |
19、 | NULL | | | xb | char(2) | YES | | NULL | | | csny | date | YES | | NULL | | +-+-+-+-+-+-+ 4 rows in set (0.00 sec) 在表里增加几条记录 mysql insert into name values(,张三,男,1988-10-10); Query OK, 1 row affected, 2 warnings (0.00 sec) mysql insert into name values(,斯,男,1988-10-10); Query OK, 1 row affected, 2
20、 warnings (0.00 sec) mysql insert into name values(,陆宇,男,1988-10-10); Query OK, 1 row affected, 2 warnings (0.00 sec) 检查 mysql select*from name; +-+-+-+-+ | id | xm | xb | csny | +-+-+-+-+ | 1 | 张三 | 男 | 1988-10-10 | | 2 | 斯 | 男 | 1988-10-10 | | 3 | 陆宇 | 男 | 1988-10-10 | +-+-+-+-+ 3 rows in set (0.0
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Linux MySQL 主从 服务器 搭建 详细 实例 完整版 要点
链接地址:https://www.31doc.com/p-5197420.html