首页 > 数据库 >mysql笔记

mysql笔记

时间:2022-10-01 08:56:38浏览次数:57  
标签:slave 笔记 Master MASTER mysql mysqld root

mysql安装

yum安装

  • mysql安装命令

    yum -y install mysql-server
    
  • mysql配置文件

    /etc/my.cnf
    /etc/my.cnf.d/mysql-server.cnf
    /etc/my.cnf.d/client.cnf 
    
  • 配置文件格式

    [mysqld]
    [mysqld_safe]
    [mysqld_multi]
    [mysql]
    [mysqladmin]
    [mysqldump]
    [server]
    [client]
    
  • mysqlserver配置文件

    vim /etc/my.cnf.d/mysql-server.cnf
    [mysqld]
    datadir=/var/lib/mysql   	           #安装路径
    socket=/var/lib/mysql/mysql.sock 	   #本地连接mysql的socket路径
    log-error=/var/log/mysql/mysqld.log	   #错误日志
    pid-file=/run/mysqld/mysqld.pid		   #mysql进程
    
  • mysql服务启动文件

    /usr/lib/systemd/system/mysqld.service
    
  • 开机启动命令

    systemctl enable --now mysqld.ervice
    
  • 初始化脚本提高安全性,root密码默认为空

    /usr/bin/mysql_secure_installation
    [root@Rocky_100 ~]#mysql_secure_installation
    
    Securing the MySQL server deployment.
    
    Connecting to MySQL using a blank password.
    
    VALIDATE PASSWORD COMPONENT can be used to test passwords
    and improve security. It checks the strength of password
    and allows the users to set only those passwords which are
    secure enough. Would you like to setup VALIDATE PASSWORD component?
    
    Press y|Y for Yes, any other key for No: y   #是否修改密码,按y回车
    
    There are three levels of password validation policy:
    
    LOW    Length >= 8
    MEDIUM Length >= 8, numeric, mixed case, and special characters
    STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file
    
    Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0  #密码复杂度选择:0|1|2
    Please set the password for root here.
    
    New password: #填写密码
    
    Re-enter new password: #填写确认密码
    
    Estimated strength of the password: 50 
    Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
    By default, a MySQL installation has an anonymous user,
    allowing anyone to log into MySQL without having to have
    a user account created for them. This is intended only for
    testing, and to make the installation go a bit smoother.
    You should remove them before moving into a production
    environment.
    
    Remove anonymous users? (Press y|Y for Yes, any other key for No) : y #移除匿名用户,按y
    Success.
    
    
    Normally, root should only be allowed to connect from
    'localhost'. This ensures that someone cannot guess at
    the root password from the network.
    
    Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y #禁用root远程访问,按y
    Success.
    
    By default, MySQL comes with a database named 'test' that
    anyone can access. This is also intended only for testing,
    and should be removed before moving into a production
    environment.
    
    
    Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y #移除test数据库,按y
     - Dropping test database...
    Success.
    
     - Removing privileges on test database...
    Success.
    
    Reloading the privilege tables will ensure that all changes
    made so far will take effect immediately.
    
    Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y #重新加载表,按y
    Success.
    
    All done! 
    
    
  • 登录mysql

    mysql -uroot -p123456
    
  • 备注

    在安装了对 MySQL 的 systemd 支持的平台上,诸如mysqld_safe和 System V 初始化脚本之类的脚本是不必要的并且不会安装。例如,mysqld_safe可以处理服务器重新启动,但 systemd 提供相同的功能,并且以与管理其他服务一致的方式而不是通过使用特定于应用程序的程序来处理。
    
    在使用 systemd 进行服务器管理的平台上不使用mysqld_safe 的一个含义是不支持在选项文件中使用[mysqld_safe]or [safe_mysqld]部分,并且可能导致意外行为。
    
    因为 systemd 具有在安装了 systemd 对 MySQL 支持的平台上管理多个 MySQL 实例的能力,所以 mysqld_multi和 mysqld_multi.server是不必要的并且没有安装。
    

通用二进制格式安装 MySQL

  • mysql二进制包下载路径

    https://downloads.mysql.com/archives/community/
    https://mirrors.aliyun.com/mysql/MySQL-8.0/mysql-8.0.27-linux-glibc2.12-x86_64.tar
    
  • 安装相关依赖包

    yum -y install libaio numactl-libs
    
  • 卸载mariadb

    yum remove mariadb-config mariadb-common mariadb-libs
    
  • 新建用户和组

    groupadd mysql
    useradd -r -g mysql -s /bin/false mysql
    
  • 准备程序文件

    wget https://mirrors.aliyun.com/mysql/MySQL-8.0/mysql-8.0.27-linux-glibc2.12-x86_64.tar
    tar xf mysql-8.0.27-linux-glibc2.12-x86_64.tar -C /usr/local
    tar -xf  mysql-8.0.27-linux-glibc2.12-x86_64.tar.xz
    ln -s mysql-8.0.27-linux-glibc2.12-x86_64/ mysql
    chown -R root.root /usr/local/mysql/
    
  • 准备环境变量

    echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
    . /etc/profile.d/mysql.sh
    
  • 准备配置文件

    cp /etc/my.cnf{,.bak}
    vim /etc/my.cnf
    [mysqld]
    datadir=/data/mysql
    skip_name_resolve=1
    socket=/data/mysql/mysql.sock
    log-error=/data/mysql/mysql.log
    pid-file=/data/mysql/mysql.pid
    [client]
    socket=/data/mysql/mysql.sock
    
  • 初始化数据库文件并提取root密码

    #/data/mysql 会自动生成,但是/data/必须事先存在
    mkdir -pv /data/mysql
    
    • 方式1:初始化数据库,生成root随机密码

      mysqld --initialize --user=mysql --datadir=/data/mysql
      
    • 方式2: 初始化数据库,生成 root 空密码

      mysqld --initialize-insecure --user=mysql --datadir=/data/mysql
      
  • 准备服务脚本和启动

    cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
    chkconfig --add mysqld
    service mysqld start
    
  • 修改口令

    #修改前面生成的随机密码为指定密码
    mysqladmin -uroot -p'LufavlMka6,!' password 123456
    #修改前面生成的空密码为指定密码
    mysqladmin -uroot password 123456
    
  • 登录mysql

    mysql -uroot -p123456
    
  • mysql二进制安装自动化脚本

    #!/bin/bash
    #
    #********************************************************************
    #Author:                zhangxuelong
    #QQ:                    1024320609
    #Date:                  2022-09-22
    #FileName:             install.sh
    #URL:                  	https://www.cnblogs.com/zxl1024320609/
    #Description:          For personal learning
    #Copyright (C):        2022 All rights reserved
    #********************************************************************
    #!/bin/bash
    
    function color () {
        RES_COL=60
        MOVE_TO_COL="echo -en \\033[${RES_COL}G"
        SETCOLOR_SUCCESS="echo -en \\033[1;32m"
        SETCOLOR_FAILURE="echo -en \\033[1;31m"
        SETCOLOR_WARNING="echo -en \\033[1;33m"
        SETCOLOR_NORMAL="echo -en \E[0m"
        echo -n "$1" && $MOVE_TO_COL
        echo -n "["
        if [ $2 = "success" -o $2 = "0" ] ;then
            ${SETCOLOR_SUCCESS}
            echo -n $"  OK  "    
        elif [ $2 = "failure" -o $2 = "1"  ] ;then 
            ${SETCOLOR_FAILURE}
            echo -n $"FAILED"
        else
            ${SETCOLOR_WARNING}
            echo -n $"WARNING"
        fi
        ${SETCOLOR_NORMAL}
        echo -n "]"
        echo 
    }
    
    #安装相关依赖包
    function install_dependent_package() {
    yum -y install libaio numactl-libs 
    }
    #卸载mariadb
    function remove_mariadb() {
    yum remove mariadb-config mariadb-common mariadb-libs
    }
    #新建用户和组
    function create_user() {
    groupadd -g 306 mysql 
    useradd -u 360 -r -g mysql -s /bin/false mysql
    }
    #准备程序文件
    function prepare_mysql_package() {
    cd 
    #线上下载
    #wget https://mirrors.aliyun.com/mysql/MySQL-8.0/mysql-8.0.27-linux-glibc2.12-x86_64.tar
    #线下拷贝mysql-8.0.27-linux-glibc2.12-x86_64.tar
    tar xf  /root/mysql-8.0.27-linux-glibc2.12-x86_64.tar -C /usr/local
    cd 		/usr/local
    tar xf  mysql-8.0.27-linux-glibc2.12-x86_64.tar.xz
    
    ln -s   /usr/local/{mysql-8.0.27-linux-glibc2.12-x86_64/,mysql}
    chown -R root.root /usr/local/mysql/
    }
    #准备环境变量
    function env_variable() {
    echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
    . /etc/profile.d/mysql.sh
    }
    #准备配置文件
    function prepare_file() {
    cp /etc/my.cnf{,.bak}
    cat > /etc/my.cnf <<EOF
    [mysqld]
    datadir=/data/mysql
    skip_name_resolve=1
    socket=/data/mysql/mysql.sock
    log-error=/data/mysql/mysql.log
    pid-file=/data/mysql/mysql.pid
    [client]
    socket=/data/mysql/mysql.sock
    EOF
    }
    #初始化数据库文件并提取root密码,初始化root密码为空
    function initialize_database() {
    mkdir -pv /data/mysql
    mysqld --initialize-insecure --user=mysql --datadir=/data/mysql
    }
    #准备服务脚本和启动
    function start_service() {
    cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
    chkconfig --add mysqld
    service mysqld start
    }
    #修改口令
    function update_password() {
    mysqladmin -uroot password 123456
    }
    
    
    init_install() {
    
    function_arr=("install_dependent_package" "remove_mariadb" "create_user" "prepare_mysql_package" "env_variable" "prepare_file" "initialize_database" "start_service" "update_password")
    
    for func in ${function_arr[@]} ;do 
     $func
     [ $? -eq 0 ]&&color "$func" 0 ||color "$func" 1 
     sleep 2
    
    done
    }
    init_install
    
    

源码安装mysql

  • 安装相关依赖包

    yum -y install wget  cmake gcc gcc-c++ ncurses boost-devel  ncurses-devel  libaio-devel  openssl openssl-devel libtirpc-devel pcre pcre-devel zlib-devel libcurl-devel libxml2-devel perl-Data-Dumper
    
    yum install gcc-toolset-10-gcc gcc-toolset-10-gcc-c++ gcc-toolset-10-binutils
    
    
    yum -y install gcc gcc-c++ cmake bison bison-devel zlib-devel libcurl-devel libarchive-devel boost-devel ncurses-devel gnutls-devel libxml2-devel openssl-devel libevent-devel libaio-devel perl-Data-Dumper
    
  • 做准备用户和数据目录

    useradd -r -s /sbin/nologin -d /data/mysql mysql
    
  • 准备数据库目录

    mkdir /data/mysql
    chown mysql.mysql /data/mysql
    
  • 下载并解压缩源码包

    tar xvf mysql-5.6.51.tar.gz -C /usr/local/src
    #mariadb-10.2.18.tar.gz
    
  • 源码编译安装 MySQL

    cd /usr/local/src/mysql-8.0.28/
    cmake . \
    -DCMAKE_INSTALL_PREFIX=/apps/mysql \
    -DMYSQL_DATADIR=/data/mysql/ \
    -DSYSCONFDIR=/etc/ \
    -DMYSQL_USER=mysql \
    -DWITH_INNOBASE_STORAGE_ENGINE=1 \
    -DWITH_ARCHIVE_STORAGE_ENGINE=1 \
    -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
    -DWITH_PARTITION_STORAGE_ENGINE=1 \
    -DWITHOUT_MROONGA_STORAGE_ENGINE=1 \
    -DWITH_DEBUG=0 \
    -DWITH_READLINE=1 \
    -DWITH_SSL=system \
    -DWITH_ZLIB=system \
    -DWITH_LIBWRAP=0 \
    -DENABLED_LOCAL_INFILE=1 \
    -DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock \
    -DDEFAULT_CHARSET=utf8 \
    -DDEFAULT_COLLATION=utf8_general_ci	\
    -DFORCE_INSOURCE_BUILD=1
    
    make -j 8 && make install
    
    
    cmake . \
    -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
    -DINSTALL_DATADIR=/data/mysql -DMYSQL_USER=mysql \
    -DMYSQL_UNIX_ADDR=/tmp/mysqld.sock -DDEFAULT_CHARSET=utf8 \
    -DDEFAULT_COLLATION=utf8_general_ci \
    -DWITH_INNOBASE_STORAGE_ENGINE=1 \
    -DWITH_EMBEDDED_SERVER=1 \
    -DFORCE_INSOURCE_BUILD=1 \
    -DWITH_MYISAM_STORAGE_ENGINE=1 \
    -DENABLED_LOCAL_INFILE=1 \
    -DEXTRA_CHARSETS=all \
    -DWITH_BOOST=/usr/local/boost
    

    提示:如果出错,执行rm -f CMakeCache.txt

  • 准备环境变量

    echo 'PATH=/apps/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
    . /etc/profile.d/mysql.sh
    
  • 生成数据库文件

    cd /apps/mysql/
    scripts/mysql_install_db --datadir=/data/mysql/ --user=mysql
    
  • 准备配置文件

    cp -b /apps/mysql/support-files/my-default.cnf /etc/my.cnf
    #针对旧版本或mariadb-10.2.18.tar.gz
    cp /apps/mysql/support-files/my-huge.cnf /etc/my.cnf
    
  • 准备启动脚本,并启动服务

    cp /apps/mysql/support-files/mysql.server /etc/init.d/mysqld
    chkconfig --add mysqld
    service mysqld start
    
  • 安全初始化

    ysql_secure_installation
    

mysql主从复制

搭建环境准备

  • 操作系统:rockyLinux8.5
  • mysql版本:8.0.26
  • 服务器IP地址:master_192.168.100.50、slave-1_192.168.100.51、slave-2_192.168.100.52

搭建步骤

mysql安装及通用配置

安装mysql数据库服务
yum install mysql-server.x86_64 -y
#禁用防火墙

systemctl disable --now firewalld.service
关闭SELINUX

sed -i 's/^SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
setenforce 0

#开机启动mysql服务
systemctl enable --now mysqld

#mysql8.0安装默认用户名root,密码为空

master配置

  1. 创建备份用户密码

    mysql
    create user repluser@'192.168.100.%' identified by '123456';
    #修改密码
    #alter user repluser@'192.168.100.%' identified by '123456';
    grant replication slave on *.* to 'repluser'@'192.168.100.%';
    
  2. 配置vim /etc/my.cnf.d/mysql-server.cnf

    #为当前节点设置一个全局惟的ID号
    server-id=50
    
  3. 重启mysql服务

    systemctl restart mysqld
    
  4. 查看binlogn日志:确定binlog最后日志文件名以及File_size的大小作为slave服务器同步开始节点MASTER_LOG_FILE='binlog.000002', MASTER_LOG_POS=454;

    #进入mysql命令行
    mysql
    查看binlogn日志
    show master logs;
    

slave-1配置

  1. 配置vim /etc/my.cnf.d/mysql-server.cnf

    #为当前节点设置一个全局惟的ID号
    server-id=51
    read_only=ON
    #log-bin  默认mysql8.0已开启,5.7一下需要启用
    
  2. 重启mysql服务

    systemctl restart mysqld
    
  3. 使用有复制权限的用户账号连接至主服务器,并

    #进入mysql命令行
    mysql
    
    #使用有复制权限的用户账号连接至主服务器
    CHANGE MASTER TO 
    MASTER_HOST='192.168.100.50',
    MASTER_USER='repluser', 
    MASTER_PASSWORD='123456', 
    MASTER_PORT=3306,
    MASTER_LOG_FILE='binlog.000002', 
    MASTER_LOG_POS=454;
    
    #启动复制线程
    start slave;
    #查看从节点同步主服务状态
    show slave status\G
    

slave-2配置

  1. master配置

    #在主服务器完全备份
    mysqldump -A -F --single-transaction --master-data=1 >/backup/fullbackup_`date +%F_%T`.sql
    #把备份sql文件复制到slave-2服务器root目录下
    scp /backup/fullbackup_2022-09-13_17\:23\:49.sql  192.168.100.52:/root/
    
    
  2. slave-2配置

    #为当前节点设置一个全局惟的ID号:vim /etc/my.cnf.d/mysql-server.cnf
    server-id=52
    read_only=ON
    #log-bin  默认mysql8.0已开启,5.7一下需要启用
    
    grep '^CHANGE MASTER' fullbackup_2022-09-13_17\:23\:49.sql
    #显示结果:CHANGE MASTER TO MASTER_LOG_FILE='binlog.000003', MASTER_LOG_POS=156;
    #添加配置vim fullbackup_2022-09-13_17\:23\:49.sql
    MASTER_HOST='192.168.100.50',
    MASTER_USER='repluser', 
    MASTER_PASSWORD='123456', 
    MASTER_PORT=3306,
    MASTER_LOG_FILE='binlog.000003', 
    MASTER_LOG_POS=156;
    
    #进入mysql命令行
    mysql
    #临时停用log_bin日志记录
    set sql_log_bin=0;
    #导入master备份sql文件
    source fullbackup_2022-09-13_17:23:49.sql
    启用log_bin日志记录
    set sql_log_bin=1;
    #启动复制线程
    start slave;
    
    

主从不一致修复方法

实验环境

  • 操作系统:rockyLinux8.5
  • mysql版本:8.0.26
  • 服务器IP地址:master_192.168.100.50、slave_192.168.100.52

操作步骤

场景1:如果仅仅只是slave插入数据导致master表和slave表不一

  1. 分析报错原因

    #master表查看binlog文件大小
    mysql> show master logs;
    +---------------+-----------+-----------+
    | Log_name      | File_size | Encrypted |
    +---------------+-----------+-----------+
    | binlog.000001 |       653 | No        |
    | binlog.000002 |     11094 | No        |
    | binlog.000003 |      3586 | No        |
    +---------------+-----------+-----------+
    3 rows in set (0.00 sec)
    
    #slave服务器连接master服务状态
    mysql> show slave status \G;
    *************************** 1. row ***************************
                   Slave_IO_State: Waiting for source to send event
                      Master_Host: 192.168.100.50
                      Master_User: repluser
                      Master_Port: 3306
                    Connect_Retry: 60
                  Master_Log_File: binlog.000003
              Read_Master_Log_Pos: 3586
                   Relay_Log_File: rocky8-relay-bin.000004
                    Relay_Log_Pos: 321
            Relay_Master_Log_File: binlog.000003
                 Slave_IO_Running: Yes
                Slave_SQL_Running: No
                  Replicate_Do_DB: 
              Replicate_Ignore_DB: 
               Replicate_Do_Table: 
           Replicate_Ignore_Table: 
          Replicate_Wild_Do_Table: 
      Replicate_Wild_Ignore_Table: 
                       Last_Errno: 1062
                       Last_Error: Could not execute Write_rows event on table hellodb.students; Duplicate entry '28' for key 'students.PRIMARY', Error_code: 1062; handler error HA_ERR_FOUND_DUPP_KEY; the event's master log binlog.000003, end_log_pos 3253
                     Skip_Counter: 0
              Exec_Master_Log_Pos: 2980
                  Relay_Log_Space: 1137
                  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: NULL
    Master_SSL_Verify_Server_Cert: No
                    Last_IO_Errno: 0
                    Last_IO_Error: 
                   Last_SQL_Errno: 1062
                   Last_SQL_Error: Could not execute Write_rows event on table hellodb.students; Duplicate entry '28' for key 'students.PRIMARY', Error_code: 1062; handler error HA_ERR_FOUND_DUPP_KEY; the event's master log binlog.000003, end_log_pos 3253
      Replicate_Ignore_Server_Ids: 
                 Master_Server_Id: 50
                      Master_UUID: ff07b7c3-3330-11ed-a898-000c290f4ec0
                 Master_Info_File: mysql.slave_master_info
                        SQL_Delay: 0
              SQL_Remaining_Delay: NULL
          Slave_SQL_Running_State: 
               Master_Retry_Count: 86400
                      Master_Bind: 
          Last_IO_Error_Timestamp: 
         Last_SQL_Error_Timestamp: 220913 21:06:14
                   Master_SSL_Crl: 
               Master_SSL_Crlpath: 
               Retrieved_Gtid_Set: 
                Executed_Gtid_Set: 
                    Auto_Position: 0
             Replicate_Rewrite_DB: 
                     Channel_Name: 
               Master_TLS_Version: 
           Master_public_key_path: 
            Get_master_public_key: 0
                Network_Namespace: 
    1 row in set, 1 warning (0.00 sec)
    
    ERROR: 
    No query specified
    
    mysql> 
    
  2. 解决办法1:slave服务器删除student表误插入的表数据,再重启slave服务器 mysql服务即可自动同步

    #根据slave报错信息可知删除slave表插入数据
    mysql -uroot -p
    use hellodb
    delete from students where stuid=28
    \q
    #重启slave表mysql服务器
    systemctl restart mysqld
    
  3. 解决办法2:删除salve数据库,然后从master服务器重新完整备份导入slave数据库,再重新建立主从复制

    1. slave服务器

      mysql> show databases;
      +--------------------+
      | Database           |
      +--------------------+
      | hellodb            |
      | information_schema |
      | mysql              |
      | performance_schema |
      | sys                |
      +--------------------+
      5 rows in set (0.00 sec)
      
      mysql> drop database hellodb;
      Query OK, 8 rows affected (0.05 sec)
      
      mysql> show databases;
      +--------------------+
      | Database           |
      +--------------------+
      | information_schema |
      | mysql              |
      | performance_schema |
      | sys                |
      +--------------------+
      4 rows in set (0.00 sec)
      
      mysql> \q
      Bye
      
      [23:29:14 root@rocky8 ~]#grep "^CHANGE MASTER TO" /backup/fullbackup_2022-09-13_23\:19\:22.sql 
      CHANGE MASTER TO MASTER_LOG_FILE='binlog.000004', MASTER_LOG_POS=156;
      
    2. master服务器

      [23:12:59 root@rocky8 ~]#mysqldump -p -A -F --single-transaction --master-data=1 >/backup/fullbackup_`date +%F_%T`.sql
      Enter password: 
      [23:19:30 root@rocky8 ~]#scp /backup/fullbackup_2022-09-13_23\:19\:22.sql   192.168.100.52:/root/
      [email protected]'s password: 
      Permission denied, please try again.
      [email protected]'s password: 
      fullbackup_2022-09-13_23:19:22.sql                                                                                                   100% 3819KB  76.6MB/s   00:00
      
    3. slave服务器

      [23:10:03 root@rocky8 ~]#mysql  -p123456
      mysql: [Warning] Using a password on the command line interface can be insecure.
      Welcome to the MySQL monitor.  Commands end with ; or \g.
      Your MySQL connection id is 11
      Server version: 8.0.26 Source distribution
      
      Copyright (c) 2000, 2021, Oracle and/or its affiliates.
      
      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           |
      +--------------------+
      | hellodb            |
      | information_schema |
      | mysql              |
      | performance_schema |
      | sys                |
      +--------------------+
      5 rows in set (0.00 sec)
      
      mysql> drop database hellodb;
      Query OK, 8 rows affected (0.05 sec)
      
      mysql> show databases;
      +--------------------+
      | Database           |
      +--------------------+
      | information_schema |
      | mysql              |
      | performance_schema |
      | sys                |
      +--------------------+
      4 rows in set (0.00 sec)
      
      mysql> \q
      Bye
      [23:16:34 root@rocky8 ~]#systemctl stop mysqld
      [23:17:02 root@rocky8 ~]#systemctl start mysqld
      [23:22:27 root@rocky8 ~]#mysql -p123456
      mysql: [Warning] Using a password on the command line interface can be insecure.
      Welcome to the MySQL monitor.  Commands end with ; or \g.
      Your MySQL connection id is 10
      Server version: 8.0.26 Source distribution
      
      Copyright (c) 2000, 2021, Oracle and/or its affiliates.
      
      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 |
      | sys                |
      +--------------------+
      4 rows in set (0.00 sec)
      
      mysql> set sql_log_bin=0;
      Query OK, 0 rows affected (0.00 sec)
      
      mysql> source fullbackup_2022-09-13_23:19:22.sql
      
      mysql> show databases;
      +--------------------+
      | Database           |
      +--------------------+
      | hellodb            |
      | information_schema |
      | mysql              |
      | performance_schema |
      | sys                |
      +--------------------+
      5 rows in set (0.00 sec)
      
      mysql> slave stop;
      ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'slave stop' at line 1
      mysql> stop slave;
      Query OK, 0 rows affected, 1 warning (0.01 sec)
      
      mysql> CHANGE MASTER TO 
          -> MASTER_HOST='192.168.100.50',
          -> MASTER_USER='repluser', 
          -> MASTER_PASSWORD='123456', 
          -> MASTER_PORT=3306,
          -> MASTER_LOG_FILE='binlog.000004', 
          -> MASTER_LOG_POS=156;
      Query OK, 0 rows affected, 9 warnings (0.01 sec)
      
      mysql> start slave;
      Query OK, 0 rows affected, 1 warning (0.00 sec)
      
      mysql> set sql_log_bin=1;
      Query OK, 0 rows affected (0.00 sec)
      
      mysql> use  hellodb
      Database changed
      mysql> show slave status \G;
      *************************** 1. row ***************************
                     Slave_IO_State: Waiting for source to send event
                        Master_Host: 192.168.100.50
                        Master_User: repluser
                        Master_Port: 3306
                      Connect_Retry: 60
                    Master_Log_File: binlog.000004
                Read_Master_Log_Pos: 61938
                     Relay_Log_File: rocky8-relay-bin.000002
                      Relay_Log_Pos: 62103
              Relay_Master_Log_File: binlog.000004
                   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: 61938
                    Relay_Log_Space: 62313
                    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: 50
                        Master_UUID: ff07b7c3-3330-11ed-a898-000c290f4ec0
                   Master_Info_File: mysql.slave_master_info
                          SQL_Delay: 0
                SQL_Remaining_Delay: NULL
            Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates
                 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
               Replicate_Rewrite_DB: 
                       Channel_Name: 
                 Master_TLS_Version: 
             Master_public_key_path: 
              Get_master_public_key: 0
                  Network_Namespace: 
      1 row in set, 1 warning (0.01 sec)
      
      ERROR: 
      No query specified
      
      
    4. master服务器验证

      mysql> show master logs;
      +---------------+-----------+-----------+
      | Log_name      | File_size | Encrypted |
      +---------------+-----------+-----------+
      | binlog.000001 |       653 | No        |
      | binlog.000002 |     11094 | No        |
      | binlog.000003 |  30555556 | No        |
      | binlog.000004 |     61938 | No        |
      +---------------+-----------+-----------+
      4 rows in set (0.00 sec)
      
      mysql> 
      
      

标签:slave,笔记,Master,MASTER,mysql,mysqld,root
From: https://www.cnblogs.com/zxl1024320609/p/16746733.html

相关文章

  • Express.js 笔记
    Express在myApp.js文件的前两行中,创建一个Express应用对象很简单。这个对象有几种方法,一个基础的方法是app.listen(port)。它处于运行状态时告诉服务器监听指定的......
  • 安装mysql-community-server-8.0.30-1.el7.x86_64报错解决办法
    1.错误如下:warning:/usr/local/src/mysql-community-server-8.0.30-1.el7.x86_64.rpm:HeaderV4RSA/SHA256Signature,keyID3a79bd29:NOKEYerror:Faileddepend......
  • MySQL数据库中乐观锁和悲观锁【杭州多测师】【杭州多测师_王sir】
    乐观锁和悲观锁的理解及如何实现,有哪些实现方式?悲观锁:总是假设最坏的情况,每次去拿数据的时候都认为别人会修改,所以每次在拿数据的时候都会上锁,这样别人想拿这个数据就会......
  • 9月《代码大全2中文版》读书笔记
         本月,在王老师的推荐下,我进行了对美国的史蒂夫·麦克奈尔先生编著的《代码大全2中文版》这本书的学习工作。本次学习让我受益匪浅,以下是我个人的一些所思所想......
  • 读书笔记1
    读书笔记1程序员所应该遵循的实用主义原则。我的源码让猫给吃了:出现错误时,要诚实,不要推诿或者找借口。要提供各种可能的解决方案与后果并与他人沟通,而不是提供借口。软......
  • 【笔记】并行与分布式-进程组织与交互
    非分布式下的线程分析:举一个案例:发送一个request需要2ms,I/O延迟8ms。-单线程处理:process100requestspersecond.-2线程处理:process125requestspersecond.(原因:双......
  • 读书笔记2
    程序需要遵守的实用主义原则。重复的危害:如果某个事物在代码中重复多次,就可能会在维护过程中带来问题,因为改动了一处而忘记改动另一处造成自相矛盾。这加大了维护难度。要......
  • 《程序员修炼之道:从小工到专家》读书笔记2
    除了是程序员,AndyHunt还是一位木匠和音乐家,你LD将v到h与这样的背景粕关单引擎飞机。尽管作者未曾明言,在本书的许多地方、你都将看到与这样的背景相关的叙述。我想,对于两位......
  • 九月读书笔记《程序员修炼之道——从小工到专家》
        本月,在王老师的推荐下,我进行了对美国的安德鲁·亨特和大卫·托马斯编著的《程序员修炼之道——从小工到专家》这本书的学习工作。本次学习让我受益匪浅,以下是我......
  • 《程序员修炼之道:从小工到专家》9月阅读笔记_第二章
    第二章注重实效的途径本章总结了在软件开发中的想法和途径,中心思想:不要在系统各处对知识进行重复,不要把任何一项知识点分散在多个系统组件中。1、重复的危害摘要:......