比如阿里云的 kubernetes的namespace:test 里部署了一个mysql-a你想在本地命令行操作
1、查看mysql service和port
$ kubectl get svc -n test
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
mysql-a ClusterIP xxx.xx.xx.xx <none> 3306/TCP 1h
2、port-forward
$ kubectl port-forward service/mysql-a -n test 3309:3306
Forwarding from 127.0.0.1:3309 -> 3306
Forwarding from [::1]:3309 -> 3306
3、登陆mysql,用户名是xxx,所以用-uxxx
$ mysql -h127.0.0.1 --port 3309 -uxxx -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
…………
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
3_1、登陆特定database -Ddatabasename 或 --database=name,这样写sql就不用databaseName.table这样麻烦了
$ mysql -h127.0.0.1 --port 3309 -Dxxxxx -uxxx -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
…………
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
记住在这里写sql要以;结尾
出错:
(1)我本地有mysql,不能在第二部的时候映射成3306端口
$ kubectl port-forward service/mysql-a -n test 3306:3306
Forwarding from [::1]:3306 -> 3306
这样登陆的时候就会出现以下,它会觉得你登的是你本地的mysql
$ mysql -h127.0.0.1 --port 3306 -uxxx -p
Enter password:
ERROR 1045 (28000): Access denied for user 'xxx'@'localhost' (using password: YES)
(2)不能用localhost登陆,必须用上面步骤2里出现的127.0.0.1
$ mysql -hlocalhost --port 3309 -uxxx -p
Enter password:
ERROR 1045 (28000): Access denied for user 'xxx'@'localhost' (using password: YES)