How can i install and run mysql database for my backend?

how can i install, create and connect to a databases like mysql for my backend

Hi @DudeFuturistic - if I understand you correctly, are you looking to install MySQL locally in your IDX workspace with a localhost (or equivalent) URL?

Thanks,
Kirupa

yes, I needed an equivalent way of working with a database just like I normally do with wsl terminal if you are familiar with it, in wsl(ubuntu or any other debian based linux distro), the package can be installed using the command “sudo apt install mysql-server”, run the mysql daemon with “sudo service mysql start” etc…, how can I do the same in idx?, because commands like “sudo”, “apt” or many other commands that work on linux terminal do not work/require a package to be installed and put in dev.nix under packages, thank you for your reply, and sorry about my English

Hey @kirupac , I am also trying to do the similar thing but with postgresql. The problem is I am able to install the package but the service is not starting. Here is the dev.nix

{ pkgs, ... }: {
  # Which nixpkgs channel to use.
  channel = "stable-23.11"; # or "unstable"

  # Use https://search.nixos.org/packages to find packages
  packages = [
    pkgs.nodejs_21
    pkgs.corepack_21
    pkgs.postgresql
    pkgs.redis
  ];

  # Sets environment variables in the workspace
  env = {};
  idx = {
    # Search for the extensions you want on https://open-vsx.org/ and use "publisher.id"
    extensions = [
      # "vscodevim.vim"
    ];

    # Enable previews
    previews = {
      enable = true;
      previews = {
        # web = {
        #   # Example: run "npm run dev" with PORT set to IDX's defined port for previews,
        #   # and show it in IDX's web preview panel
        #   command = ["npm" "run" "dev"];
        #   manager = "web";
        #   env = {
        #     # Environment variables to set for your server
        #     PORT = "$PORT";
        #   };
        # };
      };
    };

    # Workspace lifecycle hooks
    workspace = {
      # Runs when a workspace is first created
      onCreate = {
        # Example: install JS dependencies from NPM
        pnpm-install = "pnpm install";
      };
      # Runs when the workspace is (re)started
      onStart = {
        # Example: start a background task to watch and re-build backend code
        # watch-backend = "npm run watch-backend";
      };
    };
  };
}

If You wan to run mysql on Project IDX workspce. Watch this video.

Updated my response above. If MariaDB enough for You. Add services.mysql below your packages.

  packages = [
    pkgs.php82
    pkgs.php82Packages.composer
    pkgs.nodejs_20
  ];

  # See: https://nixos.wiki/wiki/Mysql
  services.mysql = {
    enable = true;
    package = pkgs.mariadb;
  };

  # Sets environment variables in the workspace
  env = {};

Thank for the insight through this video

1 Like

After I try again today. You can run MySQL or MariaDB as service on Project IDX.

  packages = [
    ...
  ];

  # See: https://nixos.wiki/wiki/Mysql
  services.mysql = {
    enable = true;
    package = pkgs.mariadb;
    # package = pkgs.mysql80; # For MySQL 8.0
  };

  # Sets environment variables in the workspace
  env = {};
``