mysql master-slave mycat 安装简明教程

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
云数据库 RDS MySQL Serverless,价值2615元额度,1个月
简介:


一、数据库基本安装


1.down mysql-5.6.37-linux-glibc2.12-x86_64.tar.gz


2.install

# yum install -y perl perl-Data-Dumper libaio

# mv mysql-5.6.37-linux-glibc2.12-x86_64.tar.gz /usr/local/

# cd /usr/local/

# tar zxvf mysql-5.6.37-linux-glibc2.12-x86_64.tar.gz 

# mv mysql-5.6.37-linux-glibc2.12-x86_64 mysql-5.6.37

# rm -rf mysql-5.6.37-linux-glibc2.12-x86_64.tar.gz 

# groupadd mysql

# useradd -r -g mysql mysql

# chown mysql.mysql -R mysql-5.6.37/

# mkdir /home/mysql

# chown mysql.mysql /home/mysql/



3.configure conf

# cd /usr/local/mysql-5.6.37/

# cp support-files/my-default.cnf /etc/my.cnf

cp: overwrite ‘/etc/my.cnf’? y


4.boot configure

# cp support-files/mysql.server /etc/init.d/mysql

# chmod +x /etc/init.d/mysql 

# chkconfig --add mysql

# vi /etc/init.d/mysql

### 配置补全变量 ###

basedir=/usr/local/mysql-5.6.37

datadir=/usr/local/mysql-5.6.37/data


5.env setting


# vim /etc/profile

export MYSQL_HOME=/usr/local/mysql-5.6.37

export PATH=$PATH:$MYSQL_HOME/bin


6. configure file

# vi /etc/my.cnf


[mysqld]

character_set_server = utf8

sql_mode = NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES


# GENERAL

datadir = /usr/local/mysql-5.6.37/data

socket = /usr/local/mysql-5.6.37/mysql.sock

pid_file = /usr/local/mysql-5.6.37/mysql.pid

user = mysql

port = 3306

bind_address = 0.0.0.0  


# INNODB

default-storage-engine = InnoDB

innodb_file_per_table = 1

innodb_buffer_pool_size = 800MB

innodb_log_file_size = 256MB

innodb_file_per_table = 1

innodb_flush_method = O_DIRECT

innodb_flush_log_at_trx_commit = 2  

sync_binlog = 20


# MyISAM

myisam_recover=default 

key_buffer_size = 200MB


# LOGGING

log_error = /usr/local/mysql-5.6.37/mysql-error.log

slow_query_log = 1

long_query_time = 0.5

slow_query_log_file = /usr/local/mysql-5.6.37/mysql-slow.log


# BINLOG

log_bin = mysql-bin  

binlog_format = mixed

expire_logs_days = 30


# MASTER

# server-id=1


# SLAVE

# server_id = 2

# relay_log = mysql-relay-bin

# log_slave_updates = 1

# read_only = 1


# OTHER

skip_name_resolve

max_connect_errors = 5000

tmp_table_size = 32M

max_heap_table_size = 32M

query_cache_type = 0

query_cache_size = 0

max_connections = 5000

thread_cache_size = 64

open_files_limit = 65535

max_allowed_packet = 64M 


[client]

default_character_set=utf8

socket = /usr/local/mysql-5.6.37/mysql.sock

port = 3306


7.init db

# su - mysql

Last login: Tue Sep  5 14:26:36 CST 2017 on pts/0

-bash-4.2$ cd /usr/local/mysql-5.6.37

-bash-4.2$ scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql-5.6.37 --datadir=/usr/local/mysql-5.6.37/data

-bash-4.2$ exit

logout


9.启动mysql

# service mysql start


8.登录mysql及改密码与配置远程访问

#登录mysql,密码为空

# mysql -u root -p     

#允许root用户远程访问

mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'your_password' WITH GRANT OPTION;     

#刷新权限

mysql>FLUSH PRIVILEGES;     

mysql>exit


二、配置master-slave

1.参数区别

server_id、server_uuid均不同

master

[mysqld]

log-bin=mysql-bin

server-id=1


slave (/etc/my.cnf)

[mysqld]

log_bin = mysql-bin

server_id = 2

relay_log = mysql-relay-bin

log_slave_updates = 1

read_only = 1



1).master


mysql> show variables like 'server%';

+----------------+--------------------------------------+

| Variable_name  | Value                                |

+----------------+--------------------------------------+

| server_id      | 1                                    |

| server_id_bits | 32                                   |

| server_uuid    | 4875d4d5-9211-11e7-90ac-000c29e56ccc |

+----------------+--------------------------------------+

3 rows in set (0.04 sec)


mysql> 

mysql> 


2).salve


mysql> show variables like 'server%';

+----------------+--------------------------------------+

| Variable_name  | Value                                |

+----------------+--------------------------------------+

| server_id      | 2                                    |

| server_id_bits | 32                                   |

| server_uuid    | eb40bb6a-920d-11e7-9096-000c29e0d6fa |

+----------------+--------------------------------------+

3 rows in set (0.04 sec)


mysql> 



2.master建立复制用户


mysql> show master status;

+------------------+----------+--------------+------------------+-------------------+

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

+------------------+----------+--------------+------------------+-------------------+

| mysql-bin.000003 |      120 |              |                  |                   |

+------------------+----------+--------------+------------------+-------------------+

1 row in set (0.00 sec)


mysql> grant replication slave,replication client on *.* to  'replic_user'@'172.16.3.%' identified by 'repl123456';

Query OK, 0 rows affected (0.28 sec)


mysql> flush privileges;

Query OK, 0 rows affected (0.29 sec)


mysql> 



3.slave设置复制起始位置



mysql> CHANGE MASTER TO MASTER_HOST='172.16.3.226',

    -> MASTER_PORT=3306,

    -> MASTER_USER='replic_user',

    -> MASTER_PASSWORD='repl123456',

    -> MASTER_LOG_FILE='mysql-bin.000003',

    -> MASTER_LOG_POS=120;

Query OK, 0 rows affected, 2 warnings (0.28 sec)


mysql> start slave;

Query OK, 0 rows affected (0.31 sec)


mysql> show slave status\G;

*************************** 1. row ***************************

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 172.16.3.226

                  Master_User: replic_user

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000003

          Read_Master_Log_Pos: 433

               Relay_Log_File: mysql-relay-bin.000002

                Relay_Log_Pos: 596

        Relay_Master_Log_File: mysql-bin.000003

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

              Replicate_Do_DB: 

          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: 433

              Relay_Log_Space: 769

              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: 

  Replicate_Ignore_Server_Ids: 

             Master_Server_Id: 1

                  Master_UUID: 4875d4d5-9211-11e7-90ac-000c29e56ccc

             Master_Info_File: /usr/local/mysql-5.6.37/data/master.info

                    SQL_Delay: 0

          SQL_Remaining_Delay: NULL

      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it

           Master_Retry_Count: 86400

                  Master_Bind: 

      Last_IO_Error_Timestamp: 

     Last_SQL_Error_Timestamp: 

               Master_SSL_Crl: 

           Master_SSL_Crlpath: 

           Retrieved_Gtid_Set: 

            Executed_Gtid_Set: 

                Auto_Position: 0

1 row in set (0.00 sec)


ERROR: 

No query specified


master建立测试库


mysql> create database db1;

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'172.16.3.%' IDENTIFIED BY '123456' WITH GRANT OPTION;

Query OK, 0 rows affected (0.29 sec)


mysql> flush privileges;

Query OK, 0 rows affected (0.01 sec)


mysql> 

mysql> 



三、安装mycat


1.安装Java


提示:mycat正常应该独立一台机器

共3台主机

master(172.16.3.226),

slave(172.16.3.228),

mycat(172.16.3.229)


jdk-8u144-linux-x64.tar.gz 

http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

# cd /opt/

# tar zxvf jdk-8u144-linux-x64.tar.gz 

# rm -rf jdk-8u144-linux-x64.tar.gz


# vi /etc/profile


### normal add stand alone ###

export JAVA_HOME=/opt/jdk1.8.0_144

export JRE_HOME=$JAVA_HOME/jre

export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

export PATH=$PATH:$JAVA_HOME/bin



[root@masterdb ~]# java -version

java version "1.8.0_144"

Java(TM) SE Runtime Environment (build 1.8.0_144-b01)

Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)

[root@masterdb ~]#


2.安装mycat

# cd /opt

Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz

官网地址:http://dl.mycat.io/1.6-RELEASE/

# tar -xvf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz

# rm -rf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz 

# groupadd mycat

# useradd -g mycat mycat

# chown -R mycat:mycat mycat/


3.mycat 登录流程


firewall--> user (logical user)--> schemas(logical db) --> 

dataNode(physical db/logical host) --> dataHost(logical host)

-->writeHost or readHost(physical host)



# cd /opt/mycat/conf/


# vi server.xml 

.......

.......

.......

        </system>


        <!-- 全局SQL防火墙设置 -->

        <!-- 

        <firewall> 

           <whitehost>

              <host host="172.16.3.229" user="mycat"/>

           </whitehost>

       <blacklist check="false">

       </blacklist>

        </firewall>

        -->


        <user name="mycat">

                <property name="password">mycat123456</property>

                <property name="schemas">TESTDB</property>

        </user>


</mycat:server>

        

[root@mycat conf]# more schema.xml 

<?xml version="1.0"?>

<!DOCTYPE mycat:schema SYSTEM "schema.dtd">

<mycat:schema xmlns:mycat="http://io.mycat/">


        <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1">

        </schema>

        <dataNode name="dn1" dataHost="localhost1" database="db1" />

        <dataHost name="localhost1" maxCon="1000" minCon="10" balance="1"

                          writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">

                <heartbeat>select user()</heartbeat>

                <!-- can have multi write hosts -->

                <writeHost host="hostM1" url="172.16.3.226:3306" user="root"

                                   password="123456">

                        <!-- can have multi read hosts -->

                        <readHost host="hostS1" url="172.16.3.228:3306" user="root" password="123456" />

                </writeHost>

        </dataHost>

</mycat:schema>

[root@mycat conf]#  


# vi /etc/profile

export MYCAT_HOME=/opt/mycat

export PATH=$MYCAT_HOME/bin:$PATH:$JAVA_HOME/bin



4.安装MySQL软件


# cd /opt

# tar zxvf mysql-5.6.37-linux-glibc2.12-x86_64.tar.gz

# mv mysql-5.6.37-linux-glibc2.12-x86_64 mysql-5.6.37

# vi /etc/profile


export JAVA_HOME=/opt/jdk1.8.0_144

export JRE_HOME=$JAVA_HOME/jre

export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

export MYCAT_HOME=/opt/mycat

export MYSQL_HOME=/opt/mysql-5.6.37

export PATH=$MYCAT_HOME/bin:$PATH:$JAVA_HOME/bin:$MYSQL_HOME/bin



5.启动mycat

[root@masterdb ~]#  mycat start

Starting Mycat-server...

[root@masterdb ~]# 

        

6.测试使用


[root@mycat conf]# mysql -u mycat -h 172.16.3.229 -P 8066 -pmycat123456 -D TESTDB

Warning: Using a password on the command line interface can be insecure.

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A


Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 3

Server version: 5.6.29-mycat-1.6-RELEASE-20161028204710 MyCat Server (OpenCloundDB)


Copyright (c) 2000, 2017, 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> select * from t1;

Empty set (0.28 sec)


mysql> insert into t1 values(1,'Peter'),(2,'Chris');

Query OK, 2 rows affected (0.06 sec)

Records: 2  Duplicates: 0  Warnings: 0


mysql> select * from t1;

+------+-------+

| id   | name  |

+------+-------+

|    1 | Peter |

|    2 | Chris |

+------+-------+

2 rows in set (0.01 sec)


mysql> 



本文转自 pgmia 51CTO博客,原文链接:http://blog.51cto.com/heyiyi/1962913

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
22小时前
|
关系型数据库 MySQL 数据库
mysqlTools 一分钟部署安装本mysql多个版本,解锁繁琐部署过程
mysqlTools 一分钟部署安装本mysql多个版本,解锁繁琐部署过程
15 2
|
2天前
|
关系型数据库 MySQL Linux
本地虚拟机centos7通过docker安装主从mysql5.7.21
本地虚拟机centos7通过docker安装主从mysql5.7.21
10 0
|
3天前
|
关系型数据库 MySQL 数据库
龙蜥操作系统上安装MySQL:步骤详解与常见问题解决
龙蜥操作系统上安装MySQL:步骤详解与常见问题解决
|
3天前
|
关系型数据库 MySQL Linux
在Centos7中:通过Docker安装MySQL5.7(保姆级)
在Centos7中:通过Docker安装MySQL5.7(保姆级)
|
3天前
|
编解码 安全 关系型数据库
祝福CSDN的小伙伴2024年快乐!Windows7安装MySQL
祝福CSDN的小伙伴2024年快乐!Windows7安装MySQL
|
4天前
|
SQL 关系型数据库 MySQL
MySQL最新版8.1.0安装配置教程
本文章,是对自己在安装最新版MySQL最新版8.1.0安装配置的一个详细过程吧,中间也有很多不清楚的地方,但是经过自己的多次对,工具环境以及软件环境的配置,现在对于这个的安装也算得心应手吧,不仅仅是一种总结,更是对自己经验的一种积累.
|
5天前
|
存储 安全 关系型数据库
MySQL中使用percona-xtrabackup工具 三种备份及恢复 (超详细教程)
MySQL中使用percona-xtrabackup工具 三种备份及恢复 (超详细教程)
|
5天前
|
关系型数据库 MySQL Linux
Linux CentOs7 安装Mysql(5.7和8.0版本)密码修改 超详细教程
Linux CentOs7 安装Mysql(5.7和8.0版本)密码修改 超详细教程
|
6天前
|
安全 关系型数据库 MySQL
MySQL安装教程
MySQL安装教程
29 0
|
6天前
|
SQL 关系型数据库 MySQL
【简单无脑】自动化脚本一键安装虚拟机下的MySQL服务
该文章提供了在虚拟机上安装MySQL服务的简化方法,特别是针对新手。作者提供了一个自动化脚本`install_mysql.sh`,使得安装过程更简单。用户需要下载`install.rpm`资源,将其放在指定目录下,然后创建并编辑脚本文件,将提供的代码粘贴进去,通过`chmod u+x`授权,最后运行脚本`./install_mysql.sh [rpm文件路径]`来安装MySQL。文章还附有相关图片说明。
16 1
【简单无脑】自动化脚本一键安装虚拟机下的MySQL服务