[Nix-dev] How to use MySQL

Linus Arver linusarver at gmail.com
Wed Oct 5 09:48:20 CEST 2016


Hello Tomasz,

On Mon, Oct 03, 2016 at 09:33:24AM +0100, Tomasz Czyż wrote:
> By looking at errors you pasted I assume you are not using proper
> configuration or default configuration is used when you don't provide any.
> And by default probably paths are set to /var/lib which is writeable only
> by root or system services.
> 
> To run mysql in userspace you need to adjust all those paths in your
> configuration to point to directories accessible/writeable by user.
> 
> I'm not sure if this actually worth the effort.

After browsing the mysql.nix file [1] in depth, I've reached the same
conclusion. There's a great deal of customization that happens in there
and it was not clear how to modify it to get a minimum working example
that plays nicely with `nix-env -iA nixos.mysql55`. I could fork that
derivation and apply my own changes and create a modified package, but
again that seemed like too much effort.

Using GitHub code search I was able to find a sample configuration [2].
For posterity, this is what I added to my configuration.nix:

    services.mysql = {
      enable = true;
      dataDir = "/var/db/mysql";
      package = pkgs.mysql;
    };

The

    dataDir = "/var/db/mysql";

line was to keep with tradition in
other Linux distros that package MySQL. (Funnily enough, the link at [1]
suggests the same, but only as a comment. [3]) Lastly the

    package = pkgs.mysql;

line seems mandatory as the manual does not state a default
value for it. With this configuration, I am able to run `mysql` and
connect to the mysqld server instance (which is running as follows ---
`ps aux' output):

mysql    30247  0.0  0.6 772632 99388 ?        Ssl  Oct04   0:02 /nix/store/piv5d6085k35h43n5smdv9g7cs1zrh4w-mariadb-10.1.17/bin/mysqld --defaults-extra-file=/nix/store/vlx9v98ll7gx00c2s79maf9axiql6ij7-my.cnf --user=mysql --datadir=/var/db/mysql --basedir=/nix/store/piv5d6085k35h43n5smdv9g7cs1zrh4w-mariadb-10.1.17 --pid-file=/run/mysqld/mysqld.pid

> You could try to run mysql using nixos-container, so you run mysql as
> "service" but in container and you can start/stop the container whenever
> you want (and it's easier to start because you can use system level
> "service" inside).

I am not familiar with nixos-container --- but I'll keep it in mind for
the future.

Thanks again for your help!

Linus

[1]: https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/services/databases/mysql.nix
[2]: https://github.com/search?q=%22services.mysql%22+extension%3Anix&ref=searchresults&type=Code&utf8=%E2%9C%93
[3]: https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/services/databases/mysql.nix#L76

> 
> 2016-10-03 9:30 GMT+01:00 Tomasz Czyż <tomasz.czyz at gmail.com>:
> 
> > Linus,
> >
> > by installing it with nix-env you install only the binary and all
> > application execution is in user hands. By using "service" in
> > configuration.nix you actually start a daemon (like /etc/init.d/mysql in
> > ubuntu for instance).
> >
> > If you want to start it in user space you have to follow exactly the same
> > steps as in other distros when you are not using /etc/init.d/ or upstart or
> > whatever system level daemon manager.
> >
> > If you want to try to reproduce what "service" is doing in your userspace,
> > you can check config of service and try to execture those commands in
> > userspace:
> >
> > https://github.com/NixOS/nixpkgs/blob/master/nixos/
> > modules/services/databases/mysql.nix
> >
> > So you can see, that configuration is generated first and then service is
> > started with
> >
> > serviceConfig.ExecStart = "${mysql}/bin/mysqld
> > --defaults-extra-file=${myCnf} ${mysqldOptions}";
> >
> >
> > 2016-10-03 9:21 GMT+01:00 Linus Arver <linusarver at gmail.com>:
> >
> >> Hello all,
> >>
> >> I want to install and use MySQL on NixOS, but am not sure how to
> >> proceed. I've managed to do
> >>
> >>     nix-env -iA nixos.mysql55
> >>
> >> but trying to get it to run is difficult. Here is what I tried:
> >>
> >>     [月 03 0:50:47] - l_k0 ~/prog/sqfmm/mysql
> >>      > mysql
> >>     ERROR 2002 (HY000): Can't connect to local MySQL server through
> >> socket '/run/mysqld/mysqld.sock' (2)
> >>     [月 03 0:50:53] 1 - l_k0 ~/prog/sqfmm/mysql
> >>      > systemctl start mysqld
> >>     ==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
> >>     Authentication is required to start 'mysqld.service'.
> >>     Multiple identities can be used for authentication:
> >>      1.  System administrator (root)
> >>      2.  Linus Arver (l)
> >>     Choose identity to authenticate as (1-2): 2
> >>     Password:
> >>     ==== AUTHENTICATION COMPLETE ===
> >>     Failed to start mysqld.service: Unit mysqld.service not found.
> >>     [月 03 0:52:33] 5 - l_k0 ~/prog/sqfmm/mysql
> >>      > systemctl start mysql
> >>     ==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
> >>     Authentication is required to start 'mysql.service'.
> >>     Multiple identities can be used for authentication:
> >>      1.  System administrator (root)
> >>      2.  Linus Arver (l)
> >>     Choose identity to authenticate as (1-2): 2
> >>     Password:
> >>     ==== AUTHENTICATION COMPLETE ===
> >>     Failed to start mysql.service: Unit mysql.service not found.
> >>     [月 03 0:52:57] 5 - l_k0 ~/prog/sqfmm/mysql
> >>      > mysqld
> >>     161003  0:53:16 [Note] mysqld (mysqld 5.5.50) starting as process
> >> 12363 ...
> >>     161003  0:53:16 [Warning] Can't create test file
> >> /var/lib/mysql/k0.lower-test
> >>     161003  0:53:16 [Warning] Can't create test file
> >> /var/lib/mysql/k0.lower-test
> >>     mysqld: Can't change dir to '/var/lib/mysql/' (Errcode: 2)
> >>     161003  0:53:16 [ERROR] Aborting
> >>
> >>     161003  0:53:16 [Note] mysqld: Shutdown complete
> >>
> >>     [月 03 0:53:16] 1 - l_k0 ~/prog/sqfmm/mysql
> >>      > mysqld_safe
> >>     161003 00:54:16 mysqld_safe Logging to '/var/lib/mysql/k0.err'.
> >>     touch: cannot touch '/var/lib/mysql/k0.err': No such file or directory
> >>     chmod: cannot access '/var/lib/mysql/k0.err': No such file or
> >> directory
> >>     mkdir: cannot create directory ‘/run/mysqld’: Permission denied
> >>     chown: invalid user: ‘mysql’
> >>     chmod: cannot access '/run/mysqld': No such file or directory
> >>     161003 00:54:16 mysqld_safe Starting mysqld daemon with databases
> >> from /var/lib/mysql
> >>     /home/l/.nix-profile/bin/mysqld_safe: line 128:
> >> /var/lib/mysql/k0.err: No such file or directory
> >>     /home/l/.nix-profile/bin/mysqld_safe: line 165:
> >> /var/lib/mysql/k0.err: No such file or directory
> >>     touch: cannot touch '/var/lib/mysql/k0.err': No such file or directory
> >>     chown: invalid user: ‘mysql’
> >>     chmod: cannot access '/var/lib/mysql/k0.err': No such file or
> >> directory
> >>     161003 00:54:16 mysqld_safe mysqld from pid file
> >> /var/lib/mysql/k0.pid ended
> >>     /home/l/.nix-profile/bin/mysqld_safe: line 128:
> >> /var/lib/mysql/k0.err: No such file or directory
> >>
> >> The attempt to run *mysqld_safe* was taken from the official docs at
> >> http://dev.mysql.com/doc/refman/5.6/en/starting-server.html.
> >>
> >> I see that there are several configuration options for MySQL
> >> (services.mysql.*) defined in the NixOS manual
> >> (https://nixos.org/nixos/manual/options.html#opt-services.mysql.enable)
> >> but this seems to be for enabling it as a service that starts
> >> unconditionally on every boot (configuration.nix), which I want to avoid.
> >>
> >> How do I start MySQL without touching configuration.nix? If
> >> configuration.nix is the only sane/preferred way to use MySQL on NixOS,
> >> then is there a resource/wiki/blogpost that discusses a minimal example?
> >> I don't mind trial-and-error with editing my system configs but seeing a
> >> second-hand account of the entire process would be very helpful.
> >>
> >> Please forgive me if I have missed any obvious documentation.
> >>
> >> Best,
> >> Linus
> >> _______________________________________________
> >> nix-dev mailing list
> >> nix-dev at lists.science.uu.nl
> >> http://lists.science.uu.nl/mailman/listinfo/nix-dev
> >>
> >
> >
> >
> > --
> > Tomasz Czyż
> >
> 
> 
> 
> -- 
> Tomasz Czyż


More information about the nix-dev mailing list