Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How are the MySQL sockets getting read by PHP? #1494

Open
Nebucatnetzer opened this issue Oct 4, 2024 · 5 comments
Open

How are the MySQL sockets getting read by PHP? #1494

Nebucatnetzer opened this issue Oct 4, 2024 · 5 comments
Labels
question Further information is requested

Comments

@Nebucatnetzer
Copy link
Contributor

I see that there is a reference to the environment variables but when I look at the generated ini file the actual lines don't contain a value.
https://github.com/cachix/devenv/blob/main/src/modules/languages/php.nix#L323-L325

We the mysql service enabled and running.

We recently had a case where a child PHP process wasn't able to access the database.
It was called within PHP like this exec("php ./somefile.php") but the code in somefile.php wasn't able to access the database until we set the following in the ini section of devenv.
So I was a bit surprised to discover that the variables are already present in the devenv code.
One thing I noticed is that the parent PHP process is able to access the DB without a problem, only the child doesn't see the env variables.

pdo_mysql.default_socket="${config.env.MYSQL_UNIX_PORT}"
mysqli.default_socket = "${config.env.MYSQL_UNIX_PORT}"
@Nebucatnetzer Nebucatnetzer added the question Further information is requested label Oct 4, 2024
@MrHamel
Copy link

MrHamel commented Oct 7, 2024

It sounds like environment variables are not getting passed through. Is there a difference if ran with PHP's shell_exec function?

Is there a possibility that somefile.php could be modified so that it can fit into something that works with include/require instead? Another option (although admittedly very dangerous), is create a function that reads a file into a string variable, and evals it.

@Nebucatnetzer
Copy link
Contributor Author

It sounds like environment variables are not getting passed through. Is there a difference if ran with PHP's shell_exec function?

No that didn't help.

Is there a possibility that somefile.php could be modified so that it can fit into something that works with include/require instead? Another option (although admittedly very dangerous), is create a function that reads a file into a string variable, and evals it.

It's legacy code which we would rather not touch.

@sandydoo
Copy link
Member

sandydoo commented Oct 10, 2024

pdo_mysql.default_socket="${config.env.MYSQL_UNIX_PORT}"
mysqli.default_socket = "${config.env.MYSQL_UNIX_PORT}"

I don't use php, but this is a totally reasonable solution. I imagine the only reason this wasn't done in the first place was to make the env var the single "source of truth" for the port.

@shyim, do you have any ideas/thoughts on this?

@shyim
Copy link
Contributor

shyim commented Oct 10, 2024

you could inline the env var in the ini if it's not changing 🤔, but yea MYSQL_UNIX_PORT is respect by mysql itself too

@Nebucatnetzer
Copy link
Contributor Author

Nebucatnetzer commented Oct 11, 2024

I think there is a bit of a misunderstanding here.
The configuration I've shown works perfectly fine and is the way I fixed the problem.

However I discovered that in devenv PHP is already configured in a way that it should get the values from the environment variable MYSQL_UNIX_PORT.
Which then led me to the question that I posted here.
Why can a sub-process of PHP-FPM not see/use the environment variable?
Hard-coding value into the config worked so it is using the same ini file.

Edit: This isn't a problem either, I'm just trying to understand how things are working together.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants