<p><strong>1. I Want to dump data file using YAML file</strong></p>
<p>data.sql file in the volume</p>
<p>the YAML file is</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: mysql
spec:
  ports:
  - port: 3306
  selector:
    app: mysql
  clusterIP: None
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql
  labels:
    app: mysql
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mysql
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: mysql
    spec:
        volumes:
        - name: mydir
          hostPath: 
            path: /var/local/test
            type: DirectoryOrCreate
        - name: myfile
          hostPath:
            path: /var/local/test/data.sql
            type: FileOrCreate 

        containers:
        - image: mysql:5.6
          name: mysql
          env:
            # Use secret in real usage
        
          - name: MYSQL_ROOT_PASSWORD
            value: root
          - name: MYSQL_USERNAME
            value: root
          - name: MYSQL_PASSWORD
            value: root
          - name: MYSQL_DATABASE
            value: sample  
          ports:
          - containerPort: 3306
            name: mysql
          volumeMounts:
          - name: mydir
            mountPath: /var/local/test/data.sql
          command: [&quot;/bin/sh&quot;,&quot;-c&quot;]
          args: [&quot;mysql -u root -proot sample &lt; data.sql&quot;] 
</code></pre>
<p>using a command, args concept but not possible</p>
<p>If I access the pod it can be dump easily but I want dump action through YAML</p>
<p>**2. Restore the database file in local system **</p>
<p>Also want to restore the dumped aur new data in my system or any other location but it will be accessible</p>
<p>Don't share the bash option
kubectl exec -it mysql-598cbfc789-2jlt7 -- mysql -u root -proot sample &lt; data.sql</p>

Saya Kasta
<p>Mostly you can't do this in the way you describe.  Kubernetes YAML isn't great for describing imperative actions like &quot;restore a database dump&quot;.  If you need to do it there, you need to do it in a Job separate from the database proper.</p>
<p>The most portable way to do this is to not attempt it in Kubernetes YAML at all (so remove, for example, the override <code>command:</code> and <code>args:</code> in your Deployment).  Run <code>kubectl apply -f ...</code> as normal, and once the database comes up, run in two separate terminal windows</p>
<pre><code>kubectl port-forward service/mysql 12345:3306
mysql -u root -proot -H127.0.0.1 -P12345 sample &lt; data.sql
</code></pre>
<p>The first establishes a connection to the running database; the second uses the local <code>mysql</code> tool and your local filesystem, connecting to the forwarded port.  12345 can be any port number but needs to be the same in both commands.</p>
<p>Some other alternatives (focusing on <code>kubectl exec</code>) are described in <a href="https://stackoverflow.com/questions/44716207/how-do-i-restore-a-dump-file-from-mysqldump-using-kubernetes">How do I restore a dump file from mysqldump using kubernetes?</a>.</p>
<p>There are a couple of conceptual issues with the layout you show.  The first is that a container runs only one command, and your <code>sh -c 'mysql ...'</code> runs <em>instead of</em> the MySQL daemon.  A Kubernetes Pod also can't access your local filesystem, and if you use a <code>hostPath</code> volume like you show, you need to correctly guess which node the pod will run on and copy the data there; this isn't reliable and can lead to inconsistent results if you run it from different places.</p>
<hr />
<p>If you can get the dump data into Kubernetes somehow, you can run a separate <a href="https://kubernetes.io/docs/concepts/workloads/controllers/job/" rel="nofollow noreferrer">Job</a> to do the restore:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: batch/v1
kind: Job
metadata:
  name: mysql-restore
spec:
  template:
    spec:
      containers:
        - name: mysql-restore
          env: { same as in: the question }
          image: mysql:5.6
          volumeMounts: { ... }
          command: [sh, -c]
          args:
            - &gt;-
                mysql sample &lt; /var/local/test/data.sql
</code></pre>
<p>The question is what to put in the <code>volumeMounts:</code>.  If the database dump is fairly small, you might be able to fit it into a <a href="https://kubernetes.io/docs/concepts/configuration/configmap/" rel="nofollow noreferrer">ConfigMap</a> and then <a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#add-configmap-data-to-a-volume" rel="nofollow noreferrer">mount the ConfigMap into the Pod</a>, but creating the ConfigMap can be awkward and the size is limited to 1 MB.  If you have a way to directly access the volume content (maybe you're mounting an NFS mount you can also access out-of-cluster) then you can copy the dump there.  You also might be able to use network-accessible cloud storage like Amazon S3 or another HTTP-based service where you can <code>curl</code> the dump at startup time.</p>
