在Linux上安装mysql主-从

[root@localhost ~]# cd /
[root@localhost /]# cd /usr/local/src
[root@localhost src]# mkdir mysql
把文件 Percona-Server安装文件拖拽进虚拟机
[root@localhost src]# ls
mysql Percona-Server-5.6.24-72.2-r8d0f85b-el6-x86_64-bundle.tar
[root@localhost src]# mv Percona-Server-5.6.24-72.2-r8d0f85b-el6-x86_64-bundle.tar mysql/
[root@localhost src]# ls
mysql
[root@localhost src]# cd mysql/
[root@localhost mysql]# ls
Percona-Server-5.6.24-72.2-r8d0f85b-el6-x86_64-bundle.tar
解压
[root@localhost mysql]# tar -xvf Percona-Server-5.6.24-72.2-r8d0f85b-el6-x86_64-bundle.tar

安装顺序
1.Percona-Server-56-debuginfo-5.6.24-rel72.2.el6.x86_64.rpm
2.Percona-Server-shared-56-5.6.24-rel72.2.el6.x86_64.rpm
3.Percona-Server-client-56-5.6.24-rel72.2.el6.x86_64.rpm
4.安装服务端 Percona-Server-server-56-5.6.24-rel72.2.el6.x86_64.rpm
安装命令 rpm -ivh

rpm安装软件的基本命令
-ivh 打印安装过程的同时,把安装项以原有的路径下进行安装

安装文件名称
[root@localhost mysql]# rpm -ivh Percona-Server-56-debuginfo-5.6.24-rel72.2.el6.x86_64.rpm
[root@localhost mysql]# rpm -ivh Percona-Server-shared-56-5.6.24-rel72.2.el6.x86_64.rpm
[root@localhost mysql]# rpm -ivh Percona-Server-client-56-5.6.24-rel72.2.el6.x86_64.rpm
[root@localhost mysql]# rpm -ivh Percona-Server-server-56-5.6.24-rel72.2.el6.x86_64.rpm

启动mysal服务项
启动命令:service mysql start
重启命令:service mysql restart
关闭命令:service mysql stop

[root@localhost mysql]# service mysql start

Starting MySQL (Percona Server)..... SUCCESS! 

[root@localhost mysql]# ls
jtdb.sql
验证一下安装是否成功

[root@localhost mysql]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.24-72.2 Percona Server (GPL), Release 72.2, Revision 8d0f85b

Copyright (c) 2009-2015 Percona LLC and/or its affiliates
Copyright (c) 2000, 2015, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.06 sec)
mysql> exit
Bye

设置用户名密码,不要管警报
[root@localhost mysql]# mysqladmin -u root password root
尝试这样登录
[root@localhost mysql]# mysql -uroot -proot
导入数据库.sql
mysql> source jtdb.sql;
验证下

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| jtdb2              |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.07 sec)
mysql> use jtdb2;
Database changed
mysql> show tables;
+---------------------+
| Tables_in_jtdb2     |
+---------------------+
| index_book          |
| section             |
| student_book        |
| student_section     |
| tb_book             |
| tb_cart             |
| tb_content          |
| tb_content_category |
| tb_item             |
| tb_item_cat         |
| tb_item_desc        |
| tb_item_param       |
| tb_item_param_item  |
| tb_order            |
| tb_order_item       |
| tb_order_shipping   |
| tb_user             |
| user                |
+---------------------+
18 rows in set (0.02 sec)
mysql> grant all on *.* to 'root'@'%' identified by 'root';
Query OK, 0 rows affected (0.21 sec)
mysql> exit;'
[root@localhost mysql]# ifconfig
eth3      Link encap:Ethernet  HWaddr 00:0C:29:E1:BB:99  
          inet addr:192.168.161.132  Bcast:192.168.161.255  Mask:255.255.255.0

关闭防火墙
[root@localhost mysql]# service iptables stop
iptables: Setting chains to policy ACCEPT: filter [ OK ]
iptables: Flushing firewall rules: [ OK ]
iptables: Unloading modules: [ OK ]

[root@localhost mysql]# ifconfig
eth3      Link encap:Ethernet  HWaddr 00:0C:29:E1:BB:99  
          inet addr:192.168.161.132  Bcast:192.168.161.255  Mask:255.255.255.0
[root@localhost mysql]# ps -ef |grep mysql
root      2966     1  0 Oct10 pts/2    00:00:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --pid-file=/var/lib/mysql/localhost.localdomain.pid
mysql     3132  2966  0 Oct10 pts/2    00:00:09 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/log/mysqld.log --pid-file=/var/lib/mysql/localhost.localdomain.pid --socket=/var/lib/mysql/mysql.sock
root      3515  2720  0 00:23 pts/2    00:00:00 grep mysql

同样的方法配置mysql-slave

[root@localhost Desktop]# ifconfig
eth3      Link encap:Ethernet  HWaddr 00:0C:29:63:21:9F  
          inet addr:192.168.161.133  Bcast:192.168.161.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe63:219f/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:19 errors:0 dropped:0 overruns:0 frame:0
          TX packets:22 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:2325 (2.2 KiB)  TX bytes:2126 (2.0 KiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
[root@localhost ~]#  ps -ef |grep mysql
root      2782     1  0 00:10 ?        00:00:03 /usr/bin/gnome-terminal -x /bin/sh -c cd '/usr/local/src/mysql' && exec $SHELL
root      2809     1  0 00:10 pts/0    00:00:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --pid-file=/var/lib/mysql/localhost.localdomain.pid
mysql     2975  2809  0 00:10 pts/0    00:00:06 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/log/mysqld.log --pid-file=/var/lib/mysql/localhost.localdomain.pid --socket=/var/lib/mysql/mysql.sock
root      3176  3148  0 00:24 pts/1    00:00:00 grep mysql

主库master
[root@localhost mysql]# vim /etc/my.cnf

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
server-id=1
log-bin=mysql-bin

[root@localhost mysql]# cd /var/lib/mysql/

[root@localhost mysql]# ls
auto.cnf  ib_logfile0  jtdb2                      mysql       performance_schema   RPM_UPGRADE_MARKER-LAST
ibdata1   ib_logfile1  localhost.localdomain.pid  mysql.sock  RPM_UPGRADE_HISTORY  test

重启服务
[root@localhost mysql]# service mysql restart
Shutting down MySQL (Percona Server).... SUCCESS!
Starting MySQL (Percona Server)..... SUCCESS!

[root@localhost mysql]# ls
auto.cnf     ib_logfile1                mysql             mysql.sock           RPM_UPGRADE_MARKER-LAST
ibdata1      jtdb2                      mysql-bin.000001  performance_schema   test
ib_logfile0  localhost.localdomain.pid  mysql-bin.index   RPM_UPGRADE_HISTORY

发现二进制文件mysql-bin.000001表示成功

[root@localhost ~]# vim /etc/my.cnf

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
server-id=2
log-bin=mysql-bin

[root@localhost ~]# service mysql restart
Shutting down MySQL (Percona Server).... SUCCESS!
Starting MySQL (Percona Server)..... SUCCESS!

[root@localhost ~]# cd /var/lib/mysql
[root@localhost mysql]# ls
auto.cnf     ib_logfile1                mysql             mysql.sock           RPM_UPGRADE_MARKER-LAST
ibdata1      jtdb2                      mysql-bin.000001  performance_schema   test
ib_logfile0  localhost.localdomain.pid  mysql-bin.index   RPM_UPGRADE_HISTORY
检查


这样表示成功
SHOW MASTER STATUS;
-- 实现主从挂载 主库IP、端口、用户名、密码、二进制日志文件
CHANGE MASTER TO MASTER_HOST="192.168.161.132",
MASTER_PORT=3306,
MASTER_USER="root",
MASTER_PASSWORD="root",
MASTER_LOG_FILE="mysql-bin.000001",
MASTER_LOG_POS=120;
-- 启动从服务
START SLAVE;
-- 检查主从状态
SHOW SLAVE STATUS;

-- 如果挂载有误,先关闭主从服务
STOP SLAVE;

不要把ip写错,不然会有如下问题

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).

一定要操作主库
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,968评论 6 482
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,601评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 153,220评论 0 344
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,416评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,425评论 5 374
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,144评论 1 285
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,432评论 3 401
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,088评论 0 261
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,586评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,028评论 2 325
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,137评论 1 334
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,783评论 4 324
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,343评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,333评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,559评论 1 262
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,595评论 2 355
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,901评论 2 345

推荐阅读更多精彩内容