Skip to content

Commit d0a22e8

Browse files
authored
feat: allow native password (#248)
1 parent c07ae7c commit d0a22e8

File tree

4 files changed

+90
-16
lines changed

4 files changed

+90
-16
lines changed
+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
resources:
22
- mysql_v1alpha1_mysqluser.yaml
33
- mysql_v1alpha1_mysql.yaml
4+
- mysql_v1alpha1_mysqldb.yaml
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
apiVersion: mysql.nakamasato.com/v1alpha1
2+
kind: MySQLDB
3+
metadata:
4+
name: sample-db # this is not a name for MySQL database but just a Kubernetes object name
5+
spec:
6+
dbName: sample_db # this is MySQL database name
7+
mysqlName: mysql-sample

controllers/mysql_controller.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -237,10 +237,11 @@ func (r *MySQLReconciler) getMySQLConfig(ctx context.Context, mysql *mysqlv1alph
237237
}
238238

239239
return Config{
240-
User: user,
241-
Passwd: password,
242-
Addr: fmt.Sprintf("%s:%d", mysql.Spec.Host, mysql.Spec.Port),
243-
Net: "tcp",
240+
User: user,
241+
Passwd: password,
242+
Addr: fmt.Sprintf("%s:%d", mysql.Spec.Host, mysql.Spec.Port),
243+
Net: "tcp",
244+
AllowNativePasswords: true,
244245
}, nil
245246
}
246247

docs/index.md

+77-12
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@
2323
```
2424
kubectl apply -k https://github.com/nakamasato/mysql-operator/config/mysql
2525
```
26-
1. Apply custom resources (`MySQL`, `MySQLUser`, `MySQLDB`).
2726
28-
`mysql.yaml` credentials to connect to the MySQL:
27+
1. Configure MySQL credentials for the operator using the custom resources `MySQL`.
28+
29+
`mysql.yaml` credentials to connect to the MySQL: **This user is used to manage MySQL users and databases, which is ususally an admin user.**
2930
3031
```yaml
3132
apiVersion: mysql.nakamasato.com/v1alpha1
@@ -42,6 +43,22 @@
4243
type: raw
4344
```
4445
46+
If you installed mysql sample with the command above, the password for the root user is `password`. You can apply `MySQL` with the following command.
47+
48+
```
49+
kubectl apply -f https://raw.githubusercontent.com/nakamasato/mysql-operator/main/config/samples-on-k8s/mysql_v1alpha1_mysql.yaml
50+
```
51+
52+
You can check the `MySQL` object and status:
53+
54+
```
55+
kubectl get mysql
56+
NAME HOST ADMINUSER CONNECTED USERCOUNT DBCOUNT REASON
57+
mysql-sample mysql.default root true 0 0 Ping succeded and updated MySQLClients
58+
```
59+
60+
1. Create a new MySQL user with custom resource `MySQLUser`.
61+
4562
`mysqluser.yaml`: MySQL user
4663
4764
```yaml
@@ -54,6 +71,36 @@
5471
host: '%'
5572
```
5673
74+
1. Create a new MySQL user `nakamasato`
75+
76+
```
77+
kubectl apply -f https://raw.githubusercontent.com/nakamasato/mysql-operator/main/config/samples-on-k8s/mysql_v1alpha1_mysqluser.yaml
78+
```
79+
80+
1. You can check the status of `MySQLUser` object
81+
82+
```
83+
kubectl get mysqluser
84+
NAME MYSQLUSER SECRET PHASE REASON
85+
nakamasato true true Ready Both secret and mysql user are successfully created.
86+
```
87+
88+
1. You can also confirm the Secret for the new MySQL user is created.
89+
90+
```
91+
kubectl get secret
92+
NAME TYPE DATA AGE
93+
mysql-mysql-sample-nakamasato Opaque 1 4m3s
94+
```
95+
96+
1. Connect to MySQL with the newly created user
97+
98+
```
99+
kubectl exec -it $(kubectl get po | grep mysql | head -1 | awk '{print $1}') -- mysql -unakamasato -p$(kubectl get secret mysql-mysql-sample-nakamasato -o jsonpath='{.data.password}' | base64 --decode)
100+
```
101+
102+
1. Create a new MySQL database with custom resource `MySQLDB`.
103+
57104
`mysqldb.yaml`: MySQL database
58105
59106
```yaml
@@ -67,25 +114,43 @@
67114
```
68115
69116
```
70-
kubectl apply -k https://github.com/nakamasato/mysql-operator/config/samples-on-k8s
117+
kubectl apply -f https://raw.githubusercontent.com/nakamasato/mysql-operator/main/config/samples-on-k8s/mysql_v1alpha1_mysqldb.yaml
118+
```
119+
120+
```
121+
kubectl get mysqldb
122+
NAME PHASE REASON SCHEMAMIGRATION
123+
sample-db Ready Database successfully created {"dirty":false,"version":0}
71124
```
72-
1. Check `MySQLUser` and `Secret` for the MySQL user
125+
126+
1. Grant all priviledges of the created db (`sample_db`) to the create user (`nakamasato`) (TODO: Currently there's no way to manage user permissions with operator.)
73127
74128
```
75-
kubectl get mysqluser
76-
NAME PHASE REASON
77-
nakamasato Ready Both secret and mysql user are successfully created.
129+
kubectl exec -it $(kubectl get po | grep mysql | head -1 | awk '{print $1}') -- mysql -uroot -ppassword
78130
```
79131
132+
```sql
133+
GRANT ALL PRIVILEGES ON sample_db.* TO 'nakamasato'@'%';
80134
```
81-
kubectl get secret
82-
NAME TYPE DATA AGE
83-
mysql-mysql-sample-nakamasato Opaque 1 10s
135+
136+
Now the created user got the permission to use `sample_db`.
137+
84138
```
85-
1. Connect to MySQL with the secret
139+
ubectl exec -it $(kubectl get po | grep mysql | head -1 | awk '{print $1}') -- mysql -unakamasato -p$(kubectl get secret mysql-mysql-sample-nakamasato -o jsonpath='{.data.password}' | base64 --decode)
86140
```
87-
kubectl exec -it $(kubectl get po | grep mysql | head -1 | awk '{print $1}') -- mysql -unakamasato -p$(kubectl get secret mysql-mysql-sample-nakamasato -o jsonpath='{.data.password}' | base64 --decode)
141+
142+
```
143+
mysql> show databases;
144+
+--------------------+
145+
| Database |
146+
+--------------------+
147+
| information_schema |
148+
| performance_schema |
149+
| sample_db |
150+
+--------------------+
151+
3 rows in set (0.00 sec)
88152
```
153+
89154
1. Delete custom resources (`MySQL`, `MySQLUser`, `MySQLDB`).
90155
Example:
91156
```

0 commit comments

Comments
 (0)