Can't connect to local MySQL server through socket

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












All commands in debian9's interactive non-login shell (graphical mode).

I want to backup mysql before reboot or shutdown.



who 
test tty7 2018-02-01 18:26 (:0)
test@world:~$ pwd
/home/test


Now the following three commands can backup mysql database.



USERNAME="xxxx"
PASSWORD="yyyy"
mysqldump -u root -p$PASSWORD database > /home/test/wp.sql.bak


Create a service running before reboot or shutdown.



vim /etc/systemd/system/test.service 

[Unit]
Description=Run command at shutdown
Before=shutdown.target reboot.target

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/bash /home/test/test.sh

[Install]
WantedBy=multi-user.target


The test.sh file .



cat /home/test/test.sh
USERNAME="xxxx"
PASSWORD="yyyy"
mysqldump -u root -p$PASSWORD database > /home/test/wp.sql.bak


Enable the service.



sudo systemctl enable test.service
sudo reboot


Now log into interactive non-login shell (graphical mode).



sudo systemctl enable test.service
-- Logs begin at Thu 2018-02-01 18:26:04 HKT, end at Thu 2018-02-01 18:27:23 HKT. --
Feb 01 18:26:13 world systemd[1]: Starting Run command at shutdown...
Feb 01 18:26:18 world bash[480]: mysqldump: Got error: 2002: "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2 "No such file or directory")"
Feb 01 18:26:19 world systemd[1]: test.service: Main process exited, code=exited, status=2/INVALIDARGUMENT
Feb 01 18:26:19 world systemd[1]: Failed to start Run command at shutdown.
Feb 01 18:26:19 world systemd[1]: test.service: Unit entered failed state.
Feb 01 18:26:19 world systemd[1]: test.service: Failed with result 'exit-code'


The three commands can run in terminal instead of containing in test.service,how to write a service backuping all mysql datadb when reboot or shutdown?







share|improve this question


















  • 1




    Looks like your mysql instance has been stopped before the backup runs.
    – Raman Sailopal
    Feb 1 at 14:35






  • 1




    Use the ExecStop=/bin/bash /home/test/test.sh in the actual mysql service template. This should execute the backup before mysql is killed.
    – Raman Sailopal
    Feb 1 at 14:38














up vote
0
down vote

favorite












All commands in debian9's interactive non-login shell (graphical mode).

I want to backup mysql before reboot or shutdown.



who 
test tty7 2018-02-01 18:26 (:0)
test@world:~$ pwd
/home/test


Now the following three commands can backup mysql database.



USERNAME="xxxx"
PASSWORD="yyyy"
mysqldump -u root -p$PASSWORD database > /home/test/wp.sql.bak


Create a service running before reboot or shutdown.



vim /etc/systemd/system/test.service 

[Unit]
Description=Run command at shutdown
Before=shutdown.target reboot.target

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/bash /home/test/test.sh

[Install]
WantedBy=multi-user.target


The test.sh file .



cat /home/test/test.sh
USERNAME="xxxx"
PASSWORD="yyyy"
mysqldump -u root -p$PASSWORD database > /home/test/wp.sql.bak


Enable the service.



sudo systemctl enable test.service
sudo reboot


Now log into interactive non-login shell (graphical mode).



sudo systemctl enable test.service
-- Logs begin at Thu 2018-02-01 18:26:04 HKT, end at Thu 2018-02-01 18:27:23 HKT. --
Feb 01 18:26:13 world systemd[1]: Starting Run command at shutdown...
Feb 01 18:26:18 world bash[480]: mysqldump: Got error: 2002: "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2 "No such file or directory")"
Feb 01 18:26:19 world systemd[1]: test.service: Main process exited, code=exited, status=2/INVALIDARGUMENT
Feb 01 18:26:19 world systemd[1]: Failed to start Run command at shutdown.
Feb 01 18:26:19 world systemd[1]: test.service: Unit entered failed state.
Feb 01 18:26:19 world systemd[1]: test.service: Failed with result 'exit-code'


The three commands can run in terminal instead of containing in test.service,how to write a service backuping all mysql datadb when reboot or shutdown?







share|improve this question


















  • 1




    Looks like your mysql instance has been stopped before the backup runs.
    – Raman Sailopal
    Feb 1 at 14:35






  • 1




    Use the ExecStop=/bin/bash /home/test/test.sh in the actual mysql service template. This should execute the backup before mysql is killed.
    – Raman Sailopal
    Feb 1 at 14:38












up vote
0
down vote

favorite









up vote
0
down vote

favorite











All commands in debian9's interactive non-login shell (graphical mode).

I want to backup mysql before reboot or shutdown.



who 
test tty7 2018-02-01 18:26 (:0)
test@world:~$ pwd
/home/test


Now the following three commands can backup mysql database.



USERNAME="xxxx"
PASSWORD="yyyy"
mysqldump -u root -p$PASSWORD database > /home/test/wp.sql.bak


Create a service running before reboot or shutdown.



vim /etc/systemd/system/test.service 

[Unit]
Description=Run command at shutdown
Before=shutdown.target reboot.target

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/bash /home/test/test.sh

[Install]
WantedBy=multi-user.target


The test.sh file .



cat /home/test/test.sh
USERNAME="xxxx"
PASSWORD="yyyy"
mysqldump -u root -p$PASSWORD database > /home/test/wp.sql.bak


Enable the service.



sudo systemctl enable test.service
sudo reboot


Now log into interactive non-login shell (graphical mode).



sudo systemctl enable test.service
-- Logs begin at Thu 2018-02-01 18:26:04 HKT, end at Thu 2018-02-01 18:27:23 HKT. --
Feb 01 18:26:13 world systemd[1]: Starting Run command at shutdown...
Feb 01 18:26:18 world bash[480]: mysqldump: Got error: 2002: "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2 "No such file or directory")"
Feb 01 18:26:19 world systemd[1]: test.service: Main process exited, code=exited, status=2/INVALIDARGUMENT
Feb 01 18:26:19 world systemd[1]: Failed to start Run command at shutdown.
Feb 01 18:26:19 world systemd[1]: test.service: Unit entered failed state.
Feb 01 18:26:19 world systemd[1]: test.service: Failed with result 'exit-code'


The three commands can run in terminal instead of containing in test.service,how to write a service backuping all mysql datadb when reboot or shutdown?







share|improve this question














All commands in debian9's interactive non-login shell (graphical mode).

I want to backup mysql before reboot or shutdown.



who 
test tty7 2018-02-01 18:26 (:0)
test@world:~$ pwd
/home/test


Now the following three commands can backup mysql database.



USERNAME="xxxx"
PASSWORD="yyyy"
mysqldump -u root -p$PASSWORD database > /home/test/wp.sql.bak


Create a service running before reboot or shutdown.



vim /etc/systemd/system/test.service 

[Unit]
Description=Run command at shutdown
Before=shutdown.target reboot.target

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/bash /home/test/test.sh

[Install]
WantedBy=multi-user.target


The test.sh file .



cat /home/test/test.sh
USERNAME="xxxx"
PASSWORD="yyyy"
mysqldump -u root -p$PASSWORD database > /home/test/wp.sql.bak


Enable the service.



sudo systemctl enable test.service
sudo reboot


Now log into interactive non-login shell (graphical mode).



sudo systemctl enable test.service
-- Logs begin at Thu 2018-02-01 18:26:04 HKT, end at Thu 2018-02-01 18:27:23 HKT. --
Feb 01 18:26:13 world systemd[1]: Starting Run command at shutdown...
Feb 01 18:26:18 world bash[480]: mysqldump: Got error: 2002: "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2 "No such file or directory")"
Feb 01 18:26:19 world systemd[1]: test.service: Main process exited, code=exited, status=2/INVALIDARGUMENT
Feb 01 18:26:19 world systemd[1]: Failed to start Run command at shutdown.
Feb 01 18:26:19 world systemd[1]: test.service: Unit entered failed state.
Feb 01 18:26:19 world systemd[1]: test.service: Failed with result 'exit-code'


The three commands can run in terminal instead of containing in test.service,how to write a service backuping all mysql datadb when reboot or shutdown?









share|improve this question













share|improve this question




share|improve this question








edited Feb 1 at 14:38









Jeff Schaller

31.4k846105




31.4k846105










asked Feb 1 at 14:28









scrapy

317213




317213







  • 1




    Looks like your mysql instance has been stopped before the backup runs.
    – Raman Sailopal
    Feb 1 at 14:35






  • 1




    Use the ExecStop=/bin/bash /home/test/test.sh in the actual mysql service template. This should execute the backup before mysql is killed.
    – Raman Sailopal
    Feb 1 at 14:38












  • 1




    Looks like your mysql instance has been stopped before the backup runs.
    – Raman Sailopal
    Feb 1 at 14:35






  • 1




    Use the ExecStop=/bin/bash /home/test/test.sh in the actual mysql service template. This should execute the backup before mysql is killed.
    – Raman Sailopal
    Feb 1 at 14:38







1




1




Looks like your mysql instance has been stopped before the backup runs.
– Raman Sailopal
Feb 1 at 14:35




Looks like your mysql instance has been stopped before the backup runs.
– Raman Sailopal
Feb 1 at 14:35




1




1




Use the ExecStop=/bin/bash /home/test/test.sh in the actual mysql service template. This should execute the backup before mysql is killed.
– Raman Sailopal
Feb 1 at 14:38




Use the ExecStop=/bin/bash /home/test/test.sh in the actual mysql service template. This should execute the backup before mysql is killed.
– Raman Sailopal
Feb 1 at 14:38










2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










First, use ExecStop=/bin/bash /home/test/test.sh rather than ExecStart. See How to run a script with systemd right before shutdown? for more information. You appear to be running your script on startup, presumably before mysql is fully started.



Second, consider adding After=mariadb.service to be sure the script is run before mysql is shut down. Remember that, when considering shutdown order, everything is in reverse.






share|improve this answer





























    up vote
    0
    down vote













    [Unit]
    Description=Run command at shutdown
    Before=shutdown.target reboot.target
    After=mariadb.service

    [Service]
    Type=oneshot
    RemainAfterExit=true
    ExecStop=/bin/bash /home/test/test.sh

    [Install]
    WantedBy=multi-user.target


    It is mariadb in debian9 instead of mysql.






    share|improve this answer




















      Your Answer







      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "106"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );








       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f421214%2fcant-connect-to-local-mysql-server-through-socket%23new-answer', 'question_page');

      );

      Post as a guest






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      1
      down vote



      accepted










      First, use ExecStop=/bin/bash /home/test/test.sh rather than ExecStart. See How to run a script with systemd right before shutdown? for more information. You appear to be running your script on startup, presumably before mysql is fully started.



      Second, consider adding After=mariadb.service to be sure the script is run before mysql is shut down. Remember that, when considering shutdown order, everything is in reverse.






      share|improve this answer


























        up vote
        1
        down vote



        accepted










        First, use ExecStop=/bin/bash /home/test/test.sh rather than ExecStart. See How to run a script with systemd right before shutdown? for more information. You appear to be running your script on startup, presumably before mysql is fully started.



        Second, consider adding After=mariadb.service to be sure the script is run before mysql is shut down. Remember that, when considering shutdown order, everything is in reverse.






        share|improve this answer
























          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          First, use ExecStop=/bin/bash /home/test/test.sh rather than ExecStart. See How to run a script with systemd right before shutdown? for more information. You appear to be running your script on startup, presumably before mysql is fully started.



          Second, consider adding After=mariadb.service to be sure the script is run before mysql is shut down. Remember that, when considering shutdown order, everything is in reverse.






          share|improve this answer














          First, use ExecStop=/bin/bash /home/test/test.sh rather than ExecStart. See How to run a script with systemd right before shutdown? for more information. You appear to be running your script on startup, presumably before mysql is fully started.



          Second, consider adding After=mariadb.service to be sure the script is run before mysql is shut down. Remember that, when considering shutdown order, everything is in reverse.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Feb 2 at 3:13

























          answered Feb 1 at 18:19









          Steve Brandli

          1265




          1265






















              up vote
              0
              down vote













              [Unit]
              Description=Run command at shutdown
              Before=shutdown.target reboot.target
              After=mariadb.service

              [Service]
              Type=oneshot
              RemainAfterExit=true
              ExecStop=/bin/bash /home/test/test.sh

              [Install]
              WantedBy=multi-user.target


              It is mariadb in debian9 instead of mysql.






              share|improve this answer
























                up vote
                0
                down vote













                [Unit]
                Description=Run command at shutdown
                Before=shutdown.target reboot.target
                After=mariadb.service

                [Service]
                Type=oneshot
                RemainAfterExit=true
                ExecStop=/bin/bash /home/test/test.sh

                [Install]
                WantedBy=multi-user.target


                It is mariadb in debian9 instead of mysql.






                share|improve this answer






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  [Unit]
                  Description=Run command at shutdown
                  Before=shutdown.target reboot.target
                  After=mariadb.service

                  [Service]
                  Type=oneshot
                  RemainAfterExit=true
                  ExecStop=/bin/bash /home/test/test.sh

                  [Install]
                  WantedBy=multi-user.target


                  It is mariadb in debian9 instead of mysql.






                  share|improve this answer












                  [Unit]
                  Description=Run command at shutdown
                  Before=shutdown.target reboot.target
                  After=mariadb.service

                  [Service]
                  Type=oneshot
                  RemainAfterExit=true
                  ExecStop=/bin/bash /home/test/test.sh

                  [Install]
                  WantedBy=multi-user.target


                  It is mariadb in debian9 instead of mysql.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Feb 2 at 1:33









                  scrapy

                  317213




                  317213






















                       

                      draft saved


                      draft discarded


























                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f421214%2fcant-connect-to-local-mysql-server-through-socket%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      Peggy Mitchell

                      Palaiologos

                      The Forum (Inglewood, California)