<p>I followed the official walkthrough on how to deploy MySQL as a statefulset here <a href="https://kubernetes.io/docs/tasks/run-application/run-replicated-stateful-application/" rel="nofollow noreferrer">https://kubernetes.io/docs/tasks/run-application/run-replicated-stateful-application/</a></p>
<p>I have it up and running well but the guide says:</p>
<blockquote>
<p>The Client Service, called mysql-read, is a normal Service with its own cluster IP that distributes connections across all MySQL Pods that report being Ready. The set of potential endpoints includes the primary MySQL server and all replicas.
Note that only read queries can use the load-balanced Client Service. Because there is only one primary MySQL server, clients should connect directly to the primary MySQL Pod (through its DNS entry within the Headless Service) to execute writes.</p>
</blockquote>
<p>this is my connection code:</p>
<pre><code>func NewMysqlClient() *sqlx.DB {

//username:password@protocol(address)/dbname?param=value
dataSourceName := fmt.Sprintf(&quot;%s:%s@tcp(%s)/%s?parseTime=true&amp;multiStatements=true&quot;,
    username, password, host, schema,
)
log.Println(dataSourceName)
var mysqlClient *sqlx.DB
var err error
connected := false

log.Println(&quot;trying to connect to db&quot;)
for i:=0; i&lt;7; i++{
    mysqlClient, err = sqlx.Connect(&quot;mysql&quot;, dataSourceName)
    if err == nil {
        connected = true
        break
    } else {
        log.Println(err)
        log.Println(&quot;failed will try again in 30 secs!&quot;)
        time.Sleep(30*time.Second)
    }
}

if (!connected){
    log.Println(err)
    log.Println(&quot;Couldn't connect to db will exit&quot;)
    os.Exit(1)
}

log.Println(&quot;database successfully configured&quot;)

return mysqlClient
</code></pre>
<p>}</p>
<p>when I connect the app to the headless MySQL service, I get:</p>
<pre><code>Error 1290: The MySQL server is running with the --super-read-only option so it cannot execute this statement&quot;
</code></pre>
<p>I am guessing it is connecting to one of the slave replicas, when I connect to mysql-0.mysql host, everything works fine which is what is expected as this the master node.</p>
<p>My question is how will my application be able to read from the slave nodes when we are only connecting to the master as the application needs to be able to write data.</p>
<p>I tried using <code>mysql-0.mysql,mysql-1.mysql,mysql-2.mysql</code> but then I get:</p>
<pre><code>dial tcp: lookup mysql-0.mysql;mysql-1.mysql,mysql-2.mysql: no such host
</code></pre>
<p>So I want to know if there is anyway to connect to the three replicas together so that we write to the master and read from any as with other databases like mongo etc.
If there is no way to connect to all the replicas, how would you suggest that I read from the slaves and write to the master.</p>
<p>Thank you!</p>

Gasser A
<p>You have to use the <strong>service name</strong> for connecting with the <strong>MySQL</strong> from Go application.</p>
<p>So your traffic flow like</p>
<p>Go appliction POD running inside same K8s cluster as POD inside the container</p>
<p>send a request to MySQL service -&gt; MySQL service forward traffic to <strong>MySQL stateful sets</strong> (PODs or in other merge replicas)</p>
<p>So if you have created the service in your case host name will be service name : <strong>mysql</strong></p>
<p>For example you can refer this : <a href="https://kubernetes.io/docs/tutorials/stateful-application/mysql-wordpress-persistent-volume/" rel="nofollow noreferrer">https://kubernetes.io/docs/tutorials/stateful-application/mysql-wordpress-persistent-volume/</a></p>
<p>If you notice how <strong>WordPress</strong> is connceting to <strong>mysql</strong></p>
<pre><code>containers:
      - image: wordpress:4.8-apache
        name: wordpress
        env:
        - name: WORDPRESS_DB_HOST
          value: wordpress-mysql
</code></pre>
<p>it's using the <strong>MySQL</strong> service name <code>wordpress-mysql</code> as hostname to connect.</p>
<p>If you just want to connect with <strong>Read</strong> Replica you can use the service name <code>mysql-read</code></p>
<p>OR</p>
<p>you can also use try connecting with</p>
<p><code>kubectl run mysql-client --image=mysql:5.7 -i --rm --restart=Never --\ mysql -h mysql-0.mysql</code></p>
<p><strong>Option -2</strong></p>
<p>if you just to connect with specific POD or write a replica you can use the</p>
<p><code>&lt;pod-name&gt;.mysql</code></p>
<blockquote>
<p>The Headless Service provides a home for the DNS entries that the
StatefulSet controller creates for each Pod that's part of the set.
Because the Headless Service is named mysql, the Pods are accessible
by resolving .mysql from within any other Pod in the same
Kubernetes cluster and namespace.</p>
</blockquote>
