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

一定要操作主库

你可能感兴趣的:(在Linux上安装mysql主-从)