Access by Port-forwarding to Localhost
If you want to access the PolarDB-X database from outside the K8s cluster and have not configured a LoadBalancer, you can use the following command to forward the service's 3306 port to your local machine and keep the forwarding process alive.
kubectl port-forward svc/{PolarDB-X cluster name} 3306
If port 3306 on your machine is occupied, you can use the following command to forward the service to a specified port: kubectl port-forward svc/{PolarDB-X cluster name} {new port}:3306
Open a new terminal and execute the following command to connect to PolarDB-X:
mysql -h127.0.0.1 -P{forwarded port} -upolardbx_root -p
Note:
- Here, -P is uppercase, and the default port is 3306.
- To ensure password security, do not fill in the password after -p. You will be prompted to enter the password after executing the entire command, and you can log in by pressing Enter afterward.
Access through NodePort
If the PolarDB-X cluster was created with serviceType: LoadBalancer ,specified, you can also directly access it via the NodePort method.
Use the following command to get all the nodePorts:
kubectl get svc -l polardbx/name={cluster name},polardbx/cn-type=rw -o jsonpath="{.items[0].spec.ports[0].nodePort}" | xargs echo "NodePort:"
Use the following command to get the list of IPs:
kubectl get pods -l polardbx/name={cluster name},polardbx/role=cn -o jsonpath="{range .items[*]}{.status.hostIP}{'\n'}{end}"
You can access PolarDB-X using any IP from the above results combined with the NodePort: