# ​Mysql双主配置及安装部署

> 作者/来源: UCloud 运营管理员
> 发布时间: 2023-01-11T05:19:00.000Z
> 分类: CDN
> 标签: AI, 数据库, CDN
> 原文链接: http://117.50.162.249:3000/yun/articles/1648

---

# ​Mysql双主配置及安装部署

> 来源: https://www.ucloud.cn/yun/129271.html
> 作者: IT那活儿
> 发布日期: 发布于2023-01-11 13:19

​Mysql双主配置及安装部署

**点击上方“IT那活儿****”公众号，关注后了解更多内容，不管IT什么活儿，干就完了！！！**

![](https://ucloud-blog.cn-bj.ufileos.com/articles/129271/images/129271_000.png)

![](https://ucloud-blog.cn-bj.ufileos.com/articles/129271/images/129271_001.png)

由于在工作中客户要求安装Mysql并进行双主配置，Mysql的双主配置在生产环境中也是常用到的，下面我来介绍一下Mysql的安装与双主的配置，有不对的地方，欢迎大家指出！！

一

**部署安装**

**1.1 获取安装介质**

https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.23-linux-glibc2.12-x86\_64.tar.xz

**1.2 创建用户**

```
groupaddmysqluseradd-r -g mysql -s /bin/false mysql
```

**1.3 解压安装包**

```
cd/usr/localchown-R mysql:mysql  mysql-8.0.23-linux-glibc2.12-x86_64.tar.xztar-xvf mysql-8.0.23-linux-glibc2.12-x86_64.tar.xzchown-R mysql:mysql mysql-8.0.23-linux-glibc2.12-x86_64ln-s mysql-8.0.23-linux-glibc2.12-x86_64 mysqlchown-R mysql:mysql mysql
```

**1.4 创建数据文件目录**

```
mkdir-p /mysqldata/datamkdir-p /mysqldata/logmkdir-p /mysqldata/syschown-R mysql:mysql  /mysqldata/chmod-R 750 /mysqldata/
```

**1.5 配置环境变量**(写到/etc/profile)

```
exportPATH=/usr/local/mysql/bin:$PATHsource/etc/profile
```

**1.6 编辑参数文件**

- 主库

```
vi/etc/my.cnf[mysql]prompt=[u@h][d]>\_socket=/mysqldata/sys/mysql.sock[mysqld]user=mysqldatadir=/mysqldata/data/socket=/mysqldata/sys/mysql.sockcharacter_set_server=utf8mb4transaction_isolation=read-committedexplicit_defaults_for_timestamp=1max_allowed_packet=16777216event_scheduler=1server_id=212211lower_case_table_names=1interactive_timeout=1800wait_timeout=1800skip_name_resolve=1max_connections=2000max_connect_errors=100000table_open_cache=4096table_definition_cache=4096table_open_cache_instances=64read_buffer_size=6Mread_rnd_buffer_size=16Msort_buffer_size=16Mtmp_table_size=64Mjoin_buffer_size=64Mthread_cache_size=64log_error=/mysqldata/log/alert.loglog_bin=/mysqldata/log/binlogmaster_info_repository=tablerelay_log_info_repository=tablesync_binlog=1gtid_mode=onenforce_gtid_consistency=1log_slave_updates=1binlog_format=rowbinlog_rows_query_log_events=1relay_log=/mysqldata/log/relay.logrelay_log_recovery=1#slave_rows_search_algorithms=index_scan,hash_scaninnodb_buffer_pool_size=16Ginnodb_buffer_pool_instances=4innodb_buffer_pool_load_at_startup=1innodb_buffer_pool_dump_at_shutdown=1innodb_lru_scan_depth=2048innodb_flush_method=o_directinnodb_open_files=4096innodb_log_file_size=1024000000
```

- 从库

  从库的参数基本上跟主库的参数一致，只需要改变一个server\_id，这个配置双主的时候不能一样。

**1.7 初始化数据库**

```
cd/usr/local/mysqlbin/mysqld--initialize --lower-case-table-names=1 -user=mysql
```

**1.8 启动数据库**

```
cd/usr/local/mysqlbin/mysqld_safe--user=mysql &
```

**1.9 进入mysql修改密码**

```
mysql-uroot -p（初始密码在error.log里）VKgfkfqUA0,7alteruser root@localhost identified by My1qaz@WSX;flushprivileges;
```

**1.10 添加服务到系统**

```
cd/usr/local/mysqlcpsupport-files/mysql.server /etc/init.d/mysqlchmod+x /etc/init.d/mysqlchkconfig--add mysqlchkconfig--list mysql
```

**1.11 测试**

```
servicemysql start
```

二

**双主配置**

**2.1** 192.168.245.138->192.168.245.139方向

1）创建复制用户（主库：192.168.245.138）

```
createuser repl@% identified with mysql_native_password by "2w3e@W#E";grantfile on *.* to repl@%;grantreplication slave on *.* to repl@%;flushprivileges;
```

2）开启复制进程（从库：192.168.245.139）

```
changemaster tomaster_host=192.168.245.138,master_port=3306,master_user=repl,master_password=2w3e@W#E,master_auto_position=1;startslave;
```

3）检查slave状态

```
showslave statusG
```

**2.2** 192.168.245.139->192.168.245.138方向

1）创建复制用户（主库：192.168.245.139）

```
createuser sysadmin@% identified with mysql_native_password by"1q2w!Q@W";grantall on *.* to sysadmin@%;flushprivileges;
```

2）开启复制进程（从库：192.168.245.138）

```
changemaster tomaster_host=192.168.245.139,master_port=3306,master_user=repl,master_password=2w3e@W#E,master_auto_position=1;startslave;
```

3）检查slave状态

```
showslave statusG
```

三

**测试**

**3.1** 192.168.245.138->192.168.245.139方向

```
192.168.245.138：CREATEDATABASE `gohealth-plat` CHARACTER SET utf8mb4 COLLATEutf8mb4_general_ci;usegohealth-plat;createtable product(product_idint(10) not NULL,product_namevarchar(100) not NULL,product_tyepvarchar(32) not NULL,sale_priceint(10) default 0,input_priceint(10) default 0,regist_timedate,primarykey (product_id));192.168.245.139：showdatabases;usegohealth-plat;showtables;
```

**3.2** 192.168.245.139->192.168.245.138方向

```
192.168.245.139：usegohealth-plat;insertinto product values(555,sdfsd,sdfd,54,215,null);select* from product;192.168.245.138：select* from product;
```

MySQL软件安装以及双主配置还是非常简单的,如果在启动复制过程中报错，可以使用showslave status进行查看是什么原因导致的，好了，今天的分享就到此结束了！！

![](https://ucloud-blog.cn-bj.ufileos.com/articles/129271/images/129271_002.png)

## 本文作者：关 莹（上海新炬王翦团队）

## 本文来源：“IT那活儿”公众号

![](https://ucloud-blog.cn-bj.ufileos.com/articles/129271/images/129271_003.png)