linux下安装mysql(5.7)主从、主主配置

1. 卸载旧版本(如果有的话)

1. 检查是否安装有MySQL Server:  rpm -qa | grep mysql
2. 卸载:rpm -e mysql_libs   //普通删除模式
3. rpm -e --nodeps mysql_libs    // 强力删除模式,如果使用上面命令删除时,提示有依赖的其它文件,则用该命令可以对其进行强力删除

2.安装mysql

1. 安装编译代码需要的包:yum -y install make gcc-c++ cmake bison-devel  ncurses-devel
2. 下载mysql(8.0) :wget https://cdn.mysql.com/archives/mysql-5.7/mysql-boost-5.7.28.tar.gz
放在/usr/local/mysql目录下
cd mysql-boost-5.7.28.tar.gz   //解压
cd mysql-5.7.28/ 进入解压目录
3.编译安装:
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DSYSCONFDIR=/etc -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock -DMYSQL_TCP_PORT=3306 -DENABLED_LOCAL_INFILE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/usr/local/mysql/mysql-5.7.28/boost/

cmake -DCMAKE_INSTALL_PREFIX=/data/mysql/mysql1/ -DMYSQL_DATADIR=/data/mysql/mysql1/data -DSYSCONFDIR=/etc/mysql1 -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DMYSQL_UNIX_ADDR=/data/mysql/mysql1/mysql.sock -DMYSQL_TCP_PORT=3306 -DENABLED_LOCAL_INFILE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/data/mysql/mysql1/mysql-5.7.28/boost/


报错:
Cannot find appropriate system libraries for WITH_SSL=system.
Make sure you have specified a supported SSL version. 
Valid options are : 
system (use the OS openssl library), 
yes (synonym for system), 
</path/to/custom/openssl/installation>

CMake Error at cmake/ssl.cmake:66 (MESSAGE):
Please install the appropriate openssl developer package.

Call Stack (most recent call first):
cmake/ssl.cmake:260 (FATAL_SSL_NOT_FOUND_ERROR)
CMakeLists.txt:573 (MYSQL_CHECK_SSL) 

缺少两个依赖包:
yum install –y openssl openssl-devel ncurses ncurses-devel
 
安装依赖包报错:
Transaction Check Error:
file /usr/lib64/libcrypto.so from install of openssl-devel-1.0.1e-57.el6.x86_64 conflicts with file from package openssl-1.0.1g-1.x86_64
file /usr/lib64/libssl.so from install of openssl-devel-1.0.1e-57.el6.x86_64 conflicts with file from package openssl-1.0.1g-1.x86_64

Error Summary

解决办法把不需要的版本删掉就好了

例:rpm -e openssl-1.0.1g-1.x86_64
编译并安装:
make && make install

3. 配置mysql

1.演示的环境如下:
名称 | ip
---|---
主库 | 192.168.0.15
从库 | 192.168.0.16

1.创建mysql用户
    useradd mysql
2. mysql目录改为mysql读写权限
    chown -R mysql:mysql /var/lib/mysql
    chown -R mysql:mysql /usr/local/mysql
3. 设置启动脚本
    cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
4. 初始化变量
    cd /usr/local/mysql/
    ./bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --explicit_defaults_for_timestamp=on
5.开启mysql服务
    service mysqld start (stop) (restart)
6.若想设置开机启动:(任选一个)
    chkconfig --add mysqld
    chkconfig mysqld on
    service mysqld start
7.建立一个软连接,方便使用mysql命令
    ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql
8.用初始密码登陆后会提示 重置root本地账户的密码
    # 初始密码在  cat /root/.mysql_secret
    alter user 'root'@'localhost' identified by 'root';
    重置权限表
    flush privileges;
9.设置远程访问权限
    GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root';
    //授权
    grant all privileges on *.* to 'root'@'%' with grant option;
    重置权限表
    flush privileges;

4. 数据库主从配置

1.配置主库
    . 修改my.cnf文件,在[mysqld]加入下面的内容:
        # 服务的唯一编号
        server-id = 1
        # 开启mysql binlog功能
        log-bin = mysql-bin
        # binlog记录内容的方式,记录被操作的每一行
        binlog_format = ROW
        # 减少记录日志的内容,只记录受影响的列
        binlog_row_image = minimal
        # 指定需要复制的数据库名为test
        binlog-do-db = test
    · 修改好配置文件,重启mysql服务
        service mysqld restart
    · 创建从库同步数据的账号
        grant replication slave on *.* to 'clone'@'192.168.0.16' identified by 'clone';
        flush privileges;
        注意:上面这两个命令是在mysql的终端执行的。
    · 查看主库的状态:
        mysql的终端执行:
        show master status\G;
        返回信息:
            *************************** 1. row ***************************
            File: mysql-bin.000002
            Position: 600
            Binlog_Do_DB: test
            Binlog_Ignore_DB: mysql,information_schema
            Executed_Gtid_Set: 
            1 row in set (0.00 sec)

            ERROR: 
            No query specified
2.配置从库
    · 修改my.cnf文件,在[mysqld]加入下面的内容:
        # 服务的唯一编号
        server-id = 2
        # 开启mysql binlog功能
        log-bin = mysql-bin
        # binlog记录内容的方式,记录被操作的每一行
        binlog_format = ROW
        # 减少记录日志的内容,只记录受影响的列
        binlog_row_image = minimal
        # 指定需要复制的数据库名为test
        replicate-do-db = test
    ·修改好配置文件,重启mysql服务
        service mysqld restart
    ·执行同步命令
        mysql的终端执行:
        主库执行
        show master status; 获取Position值
        # 设置主服务器ip,同步账号密码,同步位置
        change master to master_host='192.168.0.15',master_user='clone',master_password='clone',master_port=3306,master_log_file='mysql-bin.000002',master_log_pos=4051(主库Position值);
        # 开启同步功能
        (stop)start slave;
    .查看从库的状态
        mysql的终端执行:
        show slave status\G;
        *************************** 1. row ***************************
           Slave_IO_State: Waiting for master to send event
              Master_Host: 192.168.0.15
              Master_User: clone
              Master_Port: 3306
            Connect_Retry: 60
          Master_Log_File: mysql-bin.000002
      Read_Master_Log_Pos: 4051
           Relay_Log_File: localhost-relay-bin.000002
            Relay_Log_Pos: 320
    Relay_Master_Log_File: mysql-bin.000002
         Slave_IO_Running: Yes 
        Slave_SQL_Running: Yes
          Replicate_Do_DB: test
          Slave_IO_Running和Slave_SQL_Running状态是yes说明成功了。

5.数据库主主配置

主主同步(两台都是master):
    1. 两台mysql都可以读写,互为主备,默认只是用一台(masterA)负责数据的写入,另一台(masterB)备用;
    2. masterA是masterB的主库,masterB又是masterA的主库,他们互为主从;
不足之处:
    1. masterB可能处于抑制空闲状态(可以用他当从库,负责部分查询);
    2. 主库后面提供服务的从库要等masterB先同步完了数据后才能去masterB上去同步数据,可能会造成一定程度的同步延迟;
测试数据库照样用主从,将主从改成主主。
 1.演示的环境如下:
名称ip
主库(masterA)192.168.0.15
从库(masterB)192.168.0.16
2. masterA设置(192.168.0.15):vim /etc/my.cnf
    · 修改my.cnf文件,在[mysqld]加入下面的内容:
        # 服务的唯一编号
        server-id = 1
        # 开启mysql binlog功能
        log-bin = mysql-bin
        # binlog记录内容的方式,记录被操作的每一行
        binlog_format = mixed
        # 减少记录日志的内容,只记录受影响的列
        # mysql复制主要有三种方式:基于SQL语句的复制(statement-based replication, SBR),基于行的复制(row-based replication, RBR),混合模式复制(mixed-based replication, MBR)。对应的,binlog的格式也有三种:STATEMENT,ROW,MIXED。
        binlog_row_image = minimal
        # 指定需要复制的数据库名为test
        binlog-do-db = test
        # 忽略系统库
        binlog-ignore-db = mysql,information_schema
        # 自动增长量
        auto-increment-increment=2  
        # 自动增长开始值
        auto-increment-offset=1   
        # 因为主主同步中数据的更新会有一个自动增长标识符,如果不设置,两台数据库的自增长标识符会冲突,导致同步数据失败,这里初始值为1,每次增长2。
    .masterB设置(192.168.0.16):vim /etc/my.cnf
        server-id=2
        log-bin=mysql-bin
        binlog-ignore-db = mysql,information_schema
        auto-increment-increment=2  //自动增长量
        auto-increment-offset=2    //自动增长开始值
        配置完重启MySQL服务
    · masterA配置同步 (192.168.0.15)
        #控制台登录mysql
        mysql -u root -p
        # 创建mysql账号repl,只容许指定ip访问,也可以指定ip范围192.168.0.%,其中%为通配符,表示所有;
        grant replication slave on *.* to 'clone'@'192.168.0.16' identified by 'clone';
        # 刷新
        flush privileges;
        # 锁定数据库表暂时无法写服务;
        flush tables with read lock;    
        # 查看binlog文件值与pos值
        show master status;        
    ·masterB配置同步(192.168.0.16)
        mysql -uroot -p123456          
        grant replication slave on *.* to 'clone'@'192.168.0.15' identified by 'clone';  
        flush privileges;       
        stop slave;  # 关闭同步,
        show master status;
        # 这里注意log_file与pos值都要对应对应masterA的show master status值
        change master to master_host='192.168.0.15',master_user='clone',master_password='clone',master_log_file='mysql-bin.000003',master_log_pos=154;       
        start slave;
        show slave status\G;   // Slave_IO_Running、 Slave_SQL_Running必须为Yes,这是同步数据启动的两个进程
    ·masterA(192.168.0.15)
        # 必须先解锁数据库
        # 解锁写
        unlock tables;              
        stop slave;
        # 这里注意log_file与pos值都要对应对应masterB的show master status值
        change master to master_host='192.168.0.16',master_user='clone',master_password='clone',master_log_file='mysql-bin.000004',master_log_pos=505;     
        start slave;
        show slave status\G;
# linux   mysql  

评论

企鹅群:39438021

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×