tags:

views:

53

answers:

3

Is it possible to make changes in mysql's my.ini file using PHP script?

+3  A: 

Sure - it's just a text file.

However, you would have to locate the correct INI file, stop the mySQL service, make the change, and start the service again. That's going to be tough. Your PHP script would probably need root privileges to stop and restart the service, and a PHP script should never have root privileges.

Update: A combination of these should work:

PHP commands to change the file: (enter www.php.net/commandname to be redirected to the manual)

  • fopen()
  • fwrite
  • fclose

PHP command to execute an external command:

  • exec()

Windows commands to stop and restart a service: net stop / net start

As to how to locate the my.ini programmatically, I have no idea. If you can, set that manually.

Pekka
could u pls give me an example code?
OM The Eternity
@Parth read my update. This is not a trivial thing to do. Most likely, it is not possible. If you want to try, you need to give much, much more information about your platform.
Pekka
Starting and stopping of mysqld can be achieved via exec() or passthru(). It's not that hard.
Techpriester
See something like http://www.tizag.com/phpT/filewrite.php
Joel L
@Techpriester it's not hard when the user account running your PHP script is actually allowed to stop services, which it shouldn't be. Otherwise you'd have to fiddle with `sudo`. I agree it's not impossible if you administer the system by yourself but it's not trivial, either. But at this point, we don't even know whether the OP is on Windows or Linux.
Pekka
M On Windows OS
OM The Eternity
@Parth I updated my answer with some pointers in case you really want to do this.
Pekka
@Pekka please do look out for my next question after few minutes related to same Replication mechanism
OM The Eternity
@Parth I saw the question, I don't know much about mySQL Replication, sorry.
Pekka
Thanks BTW, @Pekka and Fellow COders, can any body suggest me where can i put such queries to get the replies?
OM The Eternity
@Parth this specific question would be well off on serverfault.com. Generally, I recommend you be more specific in your questions. Tell a bit what you are doing, what system(s) you are running, what dimensions you are talking about (Is this a hobby project or a huge server system?) etc. etc. the more detailed your question is, the better the answers can be.
Pekka
Thanx Again Pekka
OM The Eternity
+1  A: 

Assuming the user PHP runs as has permissions to edit that file, then yes.

The question is more why do you need to edit the my.ini file with a php script?

Ali Lown
I need toi make the changes for configuring the mysql for replication
OM The Eternity
+1  A: 

Since it is a regular file, you can open it with PHP file handling functions and make any change you want, if you have the right permissions on the file.

Techpriester