[root@slave ~]# /var/lib/mysql/auto.cnf bash: /var/lib/mysql/auto.cnf: 权限不够
时间: 2023-08-05 12:05:12 浏览: 135
你在尝试访问 /var/lib/mysql/auto.cnf 时遇到了权限不足的错误。这意味着当前用户没有足够的权限来读取或执行该文件。你可以尝试使用 root 用户或具有足够权限的用户来访问该文件。你可以使用 sudo 命令以 root 用户身份执行操作,例如:
```
sudo cat /var/lib/mysql/auto.cnf
```
这将使用 root 权限打开 auto.cnf 文件并显示其内容。
相关问题
GTID mysql双主模式docker8.0
GTID(Global Transaction Identifier)是MySQL 5.6及更高版本引入的一种复制标识符。它用于标识在主从复制中所有操作的全局唯一ID,可以确保在复制过程中不会产生重复的事务。GTID是在双主模式下非常有用的,因为它可以简化双主复制的配置和管理。
在Docker 8.0中配置GTID双主模式,可以按照以下步骤进行操作:
1. 创建一个可连接的overlay网络:
```shell
docker network create --driver overlay common-network --attachable
```
2. 在两个MySQL容器中分别编辑配置文件`master.cnf`和`slave.cnf`,并添加以下内容:
```
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/
[mysqld]
log-bin=...
gtid_mode=ON
enforce_gtid_consistency=true
binlog_format=ROW
```
3. 分别启动两个MySQL容器,并将配置文件和数据目录挂载到容器内部:
```shell
docker run --restart=always -d -v /data/software/mysql8/master/config/my.cnf:/etc/mysql/my.cnf -v /data/software/mysql8/master/logs:/logs -v /data/software/mysql8/master/data/mysql:/var/lib/mysql -p 3306:3306 --name mysql_master -e MYSQL_ROOT_PASSWORD=root mysql:8.0.23
```
```shell
docker run --restart=always -d -v /data/software/mysql8/slave/config/my.cnf:/etc/mysql/my.cnf -v /data/software/mysql8/slave/logs:/logs -v /data/software/mysql8/slave/data/mysql:/var/lib/mysql -p 3307:3306 --name mysql_slave -e MYSQL_ROOT_PASSWORD=root mysql:8.0.23
```
4. 在主节点上创建一个用于复制的用户,并授予适当的权限:
```shell
docker exec -it mysql_master bash
mysql -uroot -p
GRANT REPLICATION SLAVE ON *.* TO 'replication_user'@'%' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;
```
5. 在从节点上配置主节点的连接信息,并启动复制:
```shell
docker exec -it mysql_slave bash
mysql -uroot -p
CHANGE MASTER TO MASTER_HOST='mysql_master', MASTER_PORT=3306, MASTER_USER='replication_user', MASTER_PASSWORD='password', MASTER_AUTO_POSITION=1;
START SLAVE;
```
完成上述步骤后,GTID双主模式的MySQL容器就配置好了。
k8s 部署 mysql 高可用 示例
k8s 部署 mysql 高可用示例可以采用主从复制和集群方式。
1. 主从复制方式
在 k8s 中采用 StatefulSet 进行部署,首先需要创建一个 ConfigMap,用于存放 MySQL 的配置文件。示例:
```
apiVersion: v1
kind: ConfigMap
metadata:
name: mysql-config
data:
my.cnf: |
[mysqld]
server-id=1
log-bin=mysql-bin
binlog-do-db=testdb
binlog-ignore-db=mysql
relay-log=mysqld-relay-bin
relay-log-index=mysqld-relay-bin.index
```
然后创建一个 PersistentVolumeClaim(PVC)用于存储数据,示例:
```
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-data
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
```
创建一个主节点的 StatefulSet,示例:
```
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mysql-master
spec:
serviceName: mysql
replicas: 1
selector:
matchLabels:
app: mysql
role: master
template:
metadata:
labels:
app: mysql
role: master
spec:
containers:
- name: mysql
image: mysql:5.7
ports:
- containerPort: 3306
env:
- name: MYSQL_ROOT_PASSWORD
value: "root"
volumeMounts:
- name: mysql-data
mountPath: /var/lib/mysql
- name: mysql-config
mountPath: /etc/mysql/conf.d
volumes:
- name: mysql-data
persistentVolumeClaim:
claimName: mysql-data
- name: mysql-config
configMap:
name: mysql-config
```
创建一个从节点的 StatefulSet,示例:
```
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mysql-slave
spec:
serviceName: mysql
replicas: 2
selector:
matchLabels:
app: mysql
role: slave
template:
metadata:
labels:
app: mysql
role: slave
spec:
containers:
- name: mysql
image: mysql:5.7
ports:
- containerPort: 3306
env:
- name: MYSQL_ROOT_PASSWORD
value: "root"
- name: MYSQL_MASTER_SERVICE_HOST
value: mysql-master
- name: MYSQL_MASTER_SERVICE_PORT
value: "3306"
volumeMounts:
- name: mysql-data
mountPath: /var/lib/mysql
- name: mysql-config
mountPath: /etc/mysql/conf.d
volumes:
- name: mysql-data
persistentVolumeClaim:
claimName: mysql-data
- name: mysql-config
configMap:
name: mysql-config
```
2. 集群方式
在 k8s 中采用 StatefulSet 进行部署,首先需要创建一个 ConfigMap,用于存放 MySQL 的配置文件。示例:
```
apiVersion: v1
kind: ConfigMap
metadata:
name: mysql-config
data:
my.cnf: |
[mysqld]
server-id=1
log-bin=mysql-bin
binlog-do-db=testdb
binlog-ignore-db=mysql
relay-log=mysqld-relay-bin
relay-log-index=mysqld-relay-bin.index
skip-name-resolve
bind-address=0.0.0.0
```
然后创建一个 PersistentVolumeClaim(PVC)用于存储数据,示例:
```
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-data
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
```
创建一个 StatefulSet,示例:
```
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mysql
spec:
serviceName: mysql
replicas: 3
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:5.7
ports:
- containerPort: 3306
env:
- name: MYSQL_ROOT_PASSWORD
value: "root"
volumeMounts:
- name: mysql-data
mountPath: /var/lib/mysql
- name: mysql-config
mountPath: /etc/mysql/conf.d
volumes:
- name: mysql-data
persistentVolumeClaim:
claimName: mysql-data
- name: mysql-config
configMap:
name: mysql-config
```
在 Kubernetes 中可以使用 StatefulSet 对于 MySQL 的集群进行管理,StatefulSet 可以保证 Pod 的唯一性,同时也可以保证 Pod 的有序性。在集群中,每个 Pod 会有一个唯一的 hostname,在 StatefulSet 中我们可以使用 hostname 来访问不同的 Pod。例如:mysql-0、mysql-1、mysql-2。在 MySQL 中,我们需要配置 GTID(Global Transaction Identifier)来保证所有节点的数据都是同步的。创建一个 ConfigMap,用于存放 GTID 相关的配置文件。示例:
```
apiVersion: v1
kind: ConfigMap
metadata:
name: mysql-gtid-config
data:
init.sql: |
SET @@GLOBAL.GTID_PURGED='';
CREATE USER repluser@'%' IDENTIFIED BY 'password';
GRANT REPLICATION SLAVE ON *.* TO 'repluser'@'%';
FLUSH PRIVILEGES;
```
然后在 StatefulSet 中添加一个容器用于初始化集群,示例:
```
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mysql
spec:
serviceName: mysql
replicas: 3
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:5.7
ports:
- containerPort: 3306
env:
- name: MYSQL_ROOT_PASSWORD
value: "root"
volumeMounts:
- name: mysql-data
mountPath: /var/lib/mysql
- name: mysql-config
mountPath: /etc/mysql/conf.d
- name: init-mysql
image: mysql:5.7
command: ["/bin/bash", "-c", "while ! mysqladmin ping -h mysql-0 --silent; do sleep 1; done; mysql -h mysql-0 -uroot -proot -e \"$(cat /etc/mysql/init.sql)\""]
env:
- name: MYSQL_ROOT_PASSWORD
value: "root"
volumeMounts:
- name: mysql-gtid-config
mountPath: /etc/mysql
volumes:
- name: mysql-data
persistentVolumeClaim:
claimName: mysql-data
- name: mysql-config
configMap:
name: mysql-config
- name: mysql-gtid-config
configMap:
name: mysql-gtid-config
```
这样就完成了 k8s 部署 mysql 高可用的示例。
阅读全文