mysql-8.0.22 部署配置

mysql-8.0.22 部署配置

版本

mysql-8.0.22-el7-x86_64.tar.gz

安装方式

二进制安装

安装

  • 复制 mysql-8.0.22-el7-x86_64.tar.gz 文件到目标服务器,解压到 /opt/yidun/mysql-8.0.22-el7-x86_64

  • 创建符号链接 /opt/yidun/mysql --> /opt/yidun/mysql-8.0.22-el7-x86_64

  • 安装服务 /etc/init.d/mysqld

  • 编辑配置文件 /etc/my.cnf

#
# documentation https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html
#
[mysqld]
read-only                       = {% if mysql_replication_role == 'master' %}0{% else %}1{% endif %}

lower_case_table_names          = 1
user                            = {{ mysql_user }}
server_id                       = {{ mysql_server_id }}
port                            = {{ mysql_port }}
basedir                        = {{ mysql_dir }}
datadir                        = {{ mysql_data_dir }}
socket                         = {{ mysql_data_dir }}/mysql.sock
log-error                      = {{ mysql_data_dir }}/mysqld.log
pid-file                       = {{ mysql_data_dir }}/mysqld.pid
default_authentication_plugin  = mysql_native_password
skip_name_resolve              = ON
enforce_gtid_consistency       = ON
gtid_mode                      = ON
log-bin                        = mysql-bin
binlog_format                  = ROW
expire-logs-days               = {{ mysql_expire_logs_days }}
max_connections                = {{ mysql_max_connections }}

# character set
character-set-client-handshake = FALSE
character-set-server           = utf8mb4
collation-server               = utf8mb4_unicode_ci
init_connect                   = 'SET NAMES utf8mb4'
sql_mode                       = '{{ mysql_sql_mode }}'

# Remove leading # and set to the amount of RAM for the most important data cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
innodb_buffer_pool_size = {{ mysql_innodb_buffer_pool_size }}

# Remove leading # to set options mainly useful for reporting servers. The server defaults are faster for transactions and fast SELECTs. Adjust sizes as needed, experiment to find the optimal values.
join_buffer_size = {{ mysql_join_buffer_size }}
sort_buffer_size = {{ mysql_sort_buffer_size }}
read_rnd_buffer_size = {{ mysql_read_rnd_buffer_size }}

相关参数定义如下

# The Apache mysql version to be downloaded and installed
mysql_version: 8.0.22
mysql_download_url: "https://cdn.mysql.com/archives/mysql-8.0/mysql-{{ mysql_version }}-el7-x86_64.tar.gz"

# The mysql user and group to create files/dirs with and for running the mysql service
mysql_user: mysql
mysql_group: mysql

mysql_port: 3331
mysql_root_dir: /opt/yidun
mysql_download_dir: /tmp/ansible-tmp
mysql_binary: "mysql-{{ mysql_version }}-el7-x86_64.tar.gz"
mysql_dir: "{{ mysql_root_dir }}/mysql"

mysql_hosts: "{{groups['mysql']}}"
mysql_data_dir: /var/lib/mysql
mysql_root_password: "your-root-password"
mysql_sql_mode: 'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'

mysql_replication_user: repl
mysql_replication_password: repl

# binlog keep 7 days by default
mysql_expire_logs_days: 7

mysql_max_connections: 4096
mysql_innodb_buffer_pool_size: 4096M
mysql_join_buffer_size: 128M
mysql_sort_buffer_size: 2M
mysql_read_rnd_buffer_size: 2M

  • 执行初始化
mysqld --defaults-file=/etc/my.cnf --initialize-insecure --user=mysql

  • 启动 mysqld 服务 service mysqld start

  • 因为是 initialize-insecure 方式初始化的,root 没有密码,执行脚本设置 root 密码

mysql -u root -S {{ mysql_data_dir }}/mysql.sock -NBe 'flush privileges; ALTER USER "root"@"localhost" IDENTIFIED WITH mysql_native_password BY "{{ mysql_root_password }}'

配置 replication

  • 在主节点和从节点分别创建 repl 帐号
CREATE USER IF NOT EXISTS 'repl'@'192.168.122.88' IDENTIFIED BY 'repl';
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.122.88';

CREATE USER IF NOT EXISTS 'repl'@'192.168.122.195' IDENTIFIED BY 'repl';
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.122.195';

  • 相互配置为主从节点
# 从节点
CHANGE MASTER TO
    MASTER_HOST='192.168.122.88' ,
    MASTER_USER='repl' ,
    MASTER_PASSWORD='repl' ,
    MASTER_AUTO_POSITION=1;

# 主节点
CHANGE MASTER TO
    MASTER_HOST='192.168.122.195' ,
    MASTER_USER='repl' ,
    MASTER_PASSWORD='repl' ,
    MASTER_AUTO_POSITION=1;

  • 查看复制状态
mysql> show replica status \G                                                                                                                                                                                                                 
*************************** 1\. row ***************************                                                                                                                                                                                
             Replica_IO_State: Waiting for master to send event                                                                                                                                                                               
                  Source_Host: 192.168.122.88                                                                                                                                                                                                 
                  Source_User: repl                                                                                                                                                                                                           
                  Source_Port: 3306                                                                                                                                                                                                           
                Connect_Retry: 60                                                                                                                                                                                                             
              Source_Log_File: mysql-bin.000002                                                                                                                                                                                               
          Read_Source_Log_Pos: 2950                                                                                                                                                                                                           
               Relay_Log_File: localhost-relay-bin.000002                                                                                                                                                                                     
                Relay_Log_Pos: 2661                                                                                                                                                                                                           
        Relay_Source_Log_File: mysql-bin.000002                                                                                                                                                                                               
           Replica_IO_Running: Yes                                                                                                                                                                                                            
          Replica_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_Source_Log_Pos: 2950
              Relay_Log_Space: 2874
              Until_Condition: None
               Until_Log_File:   
                Until_Log_Pos: 0 
           Source_SSL_Allowed: No
           Source_SSL_CA_File:   
           Source_SSL_CA_Path:   
              Source_SSL_Cert:   
            Source_SSL_Cipher:   
               Source_SSL_Key:   
        Seconds_Behind_Source: 0 
Source_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:  
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Source_Server_Id: 1
                  Source_UUID: e23f2357-7be9-11eb-b42f-5254001f9701
             Source_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
    Replica_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Source_Retry_Count: 86400
                  Source_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Source_SSL_Crl: 
           Source_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Source_TLS_Version: 
       Source_public_key_path: 
        Get_Source_public_key: 0
            Network_Namespace: 

主备切换操作

如果主节点宕机,需要修改从节点 /etc/my.cnf read-only 配置为 0 (注意 read-only 参数对于 root 帐号不起作用,尽量不要用 root 帐号去连数据库), 打开可写,然后重启 service mysqld restart,应用写流量切换到从节点

主节点重新起来后,修改 read-only 为 1, 重启 service mysqld restart

数据库连接需要用一个域名或者vip来管理,确保切换过程中要么 100% 切换要么不切换,避免部分应用切换到从库,部分还连着主库,导致双写的情况发生

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

推荐阅读更多精彩内容