cat > mysql-configmap.yaml << EOF apiVersion: v1 kind: ConfigMap metadata: name: mysql labels: app: mysql data: master.cnf: | # Apply this config only on the master. [mysqld] log-bin slave.cnf: | # Apply this config only on slaves. [mysqld] super-read-only EOF
cat > mysql-services.yaml << EOF apiVersion: v1 kind: Service metadata: name: mysql labels: app: mysql spec: ports: - name: mysql port: 3306 clusterIP: None selector: app: mysql --- # Client service for connecting to any MySQL instance for reads. # For writes, you must instead connect to the master: mysql-0.mysql. apiVersion: v1 kind: Service metadata: name: mysql-read labels: app: mysql spec: ports: - name: mysql port: 3306 selector: app: mysql EOF
mysql -h mysql-0 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 276 Server version: 5.7.38-log MySQL Community Server (GPL)
Copyright (c) 2000, 2022, 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>
# 插入 mysql> insert into message value("hello chenby"); Query OK, 1 row affected (0.00 sec) # 查看 mysql> select * from message; +---------------+ | message | +---------------+ | hello chenby | +---------------+ 1 row in set (0.00 sec)
四、测试备库
连接mysql-1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
mysql -h mysql-1.mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 362 Server version: 5.7.38 MySQL Community Server (GPL)
Copyright (c) 2000, 2022, 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> mysql>
# 查看数据库列表 mysql> show databases; +------------------------+ | Database | +------------------------+ | information_schema | | cby | | mysql | | performance_schema | | sys | | test | | xtrabackup_backupfiles | +------------------------+ 7 rows in set (0.01 sec) # 使用cby库 mysql> use cby; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A
Database changed mysql> # 查看表列表 mysql> show tables; +---------------+ | Tables_in_cby | +---------------+ | message | +---------------+ 1 row in set (0.00 sec) # 查看message表结构 mysql> show create table message; +---------+------------------------------------------------------------------------------------------------------+ | Table | Create Table | +---------+------------------------------------------------------------------------------------------------------+ | message | CREATE TABLE `message` ( `message` varchar(50) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1 | +---------+------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec) mysql> # 查询数据 mysql> select * from message; +---------------+ | message | +---------------+ | hello chenby | +---------------+ 1 row in set (0.00 sec) mysql> # 写入数据 mysql> insert into message values("hello world"); ERROR 1290 (HY000): The MySQL server is running with the --super-read-only option so it cannot execute this statement mysql> # 这是因为mysql-1是一个只读备库,无法进行写操作。