Automatically Start Multiple MySQL instances on boot in Ubuntu Trusty 14.04 - mysql

I'm still learning how to use Linux & MySQL, so please keep answers simple :)
I've been following this tutorial :
http://www.ducea.com/2009/01/19/running-multiple-instances-of-mysql-on-the-same-machine/
Both MySQL instances (the default instance you get on installing MySQL and the new one I created in the aforementioned tutorial) are working correctly.
But, when I boot up my OS, only the first (default instance) starts up, so I have to manually start the second instance.
I've been doing this by running these commands as root:
Start the instance:
mysqld_safe --defaults-file=/etc/mysql2/my.cnf &
Connect:
mysql -h 127.0.0.1 -P 3307
How do I make it so that both of these instances will start at boot time?
Thanks!

I fixed it by simply running the command at startup by using the solution in this question :
Ubuntu - Run command on start-up with "sudo"
Steps I took :
I added this command to the "/etc/rc.local" file as root:
(Before the "exit 0:" line or it will never get executed)
sudo mysqld_safe --defaults-file=/etc/mysql2/my.cnf &
Then I restarted my OS. The new instance is now automatically started :), so now I have both starting on boot!
Though now i wonder.. is this the correct way to handle this issue?

Related

How do you start a MySQL server on linux?

Pretty simple question here. I just want a SQL database on my version of Kali linux so I can practice SQL.
I opened the command line and entered tried to start mysql and get an error.
> mysql -u root
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'
I also made sure it was already installed using apt-get.
What are the steps I need to take to be able to make a database with tables and data that I can query?
I don't know about Kali, but on Ubuntu it would be
$ sudo service mysql start
Once that command returns, the mysqld service has started, so you can use the mysql client to connect to it.
Of course, you also have to make sure you have the mysql-server package installed, not just mysql-client and mysql-common, and that you've initialized the database instance. Complete post-installation instructions can be found in the official documentation, but the short version is
Make sure the installer has created the mysql user account. This is the account that will "own" the server process once it starts.
Change to your data directory. (I used the installer's default of /var/lib/mysql; you can change this by editing my.cnf.)
As root, execute the server daemon with the --initialize switch. Check whereis to determine the correct path, then
$ sudo /path/to/mysqld --initialize --user=mysql
This command will twiddle itself for a while, then display an automatically-generated password and exit. Once the command returns, the database instance has been initialized and the system tables created. You can now start the database instance normally (using service start), then log in as the database user root (which is not the same as the system user root) using the password from above, then change your password, create a new database user, log in as that user, create a user database, and start creating tables.
Again, the official documentation is the place to look for this; if any of the instructions in the official documentation differ from my instructions, you should ignore me and follow the official documentation's instructions.
If sudo service mysql start doesn't work for you, please try running mysqld_safe and don't kill the process. Use another tab to check the status of mysql service. This should solve your mysqld.sock issue.
If it doesn't work out, then please edit your my.cnf file and add the following:
socket=/var/lib/mysql/mysql.sock
And the permissions,
sudo chmod -R 755 /var/lib/mysql/
Hopefully, this should do it.
On my version of kali (2022-rolling), even though it's installed as MySQL, it's run as mariadb. To start it is:
sudo /etc/init.d/mariadb start
Try this
sudo service mysql start

mysql server does not shutdown with the error : 'the server quit without updating pid file'

environment
OS X El Capitan 10.11 , Mysql 5.6.26
What I did
mysql port was 3307 when I installed it. ( I didn't install it using brew.)
I changed the port 3306 to 3306 by editing /Library/LaunchDaemons/com.oracle.oss.mysql.mysqld.plist after using mysql with 3307 port for 6 months well.
I revised <string>--port=3307</string> to 3306.
I tried to stop and It didn't work. So I used following:
sudo kill -9 [PID]
It's killed and didn't start. So I changed the port 3006 to 3007.
I tried to start mysql server with following command and got a error.
sudo /usr/local/mysql/support-files/mysql.server start
ERROR! The server quit without updating PID file (/var/lib/mysql/nhnui-MacBook-Pro.local.pid).
And I found some helpful advices in follwing link.
reference link :
After MySQL install via Brew, I get the error - The server quit without updating PID file
I tried to change ownership of files, checking my.cnf file, checking error log file and all didn't work for me.
Finally, I removed /usr/local/mysql/datamysqld.local.err , /usr/local/mysql/datamysqld.local.pid and rebooted mac.and I can start mysql server.
The Problem
Then, the problem is above tip is not perfect for me.
mysql server was running after restarting mac. I checked it with 'ps-ef | grep mysql'.
Stop and start was success and then it didn't stop it again cause of same error!
Screenshot of the error :
Of course, I did it again (I mean deleting .err , .pid file / rebooting mac / cheking mysql ps / stop - start (well) / stop (got error)) and checked that it has same error.
Here is the error log : /usr/local/mysql/data/mysqld.local.err
Now, mysql server can't stop.
What can I do to solve this?
P.S - If it will be better to reinstall mysql, I should do that.
Make a backup of your system.
Dump your data into text files.
Uninstall everything releated to mySql (a quick Google search will point you to several how-tos)
Reinstall mySql from the package in the .dmg available from mySql website.
You can choose the latest 5.6.x or 5.7.x
Set the mySql root password and you're done.
When things get messy and all you can do is guessing what's the fix it's faster to reinstall given that a package installer is provided for mySql (you don't have to compile it yourself or play with brew/macports).

Cannot connect to MySQL Workbench on mac. Can't connect to MySQL server on '127.0.0.1' (61) Mac Macintosh

Cannot connect to MySQL Workbench on mac. I get the following error: Could not connect, server may not be running. Can't connect to MySQL server on '127.0.0.1' (61)
The help would be appreciated.
Thank You!
Ran into a similar issue and my problem was that MySQL installed itself configured to run on non-default port. I do not know the reason for that, but to find out which port MySQL is running on, run the following in MySql client:
SHOW GLOBAL VARIABLES LIKE 'PORT';
Go to System preferences -> MySql and check the state of your MySql instance.
There may be one or multiple reasons if you are not able to connect to MAC OS X MySQL server with MySQL-workbench.
When you press 'test connection' you might see this error. This could be explained briefly if you go step by step through 'Configure server management..'
On the basis of the red crosses you can filter out the real problem.
The most common problems are associated with the installation of MySQL-server. Few people either forget to install the server prior to installing MySQL-workbench. Some others would install a part of the product. Please check whether you have also installed all the 3 parts that comes with the MySQL-Server dmg(disk image) file which contains mysql-server package. Those 3 parts are: MySQL server, preference pane and startup item.
Note: If you haven't selected preference pane then you won't be able to start mysql server instance from the System preferences.
After you make sure that you have installed each item then you can check server instance of your native mysql-server. Open System preferences from dock and click MySQL. Then click Start MySQL Server to start the server instance. If the server instance is stopped, then MySQL-workbench won't be able to connect to the MySQL server.
If you are still facing issue, then you need to check the port of the connection which you are making. Default port is '3307' and NOT '3306'. You can check it with using the following command in mysql terminal:
SHOW GLOBAL VARIABLES LIKE 'PORT';
Please note that this process helps you to connect to the local instance. If you have to connect to a remote server, then you have to enter that specific IP and Port. Contact your server's administrator if you are facing the issue. As this question specifically states that the problem is related to connecting to the local instance, I am not writing checks that you may need to ensure.
This steps are all in the terminal:)->source
Step make sure your server is running:
sudo /usr/local/mysql/support-files/mysql.server start
Check MySQL version. "This also puts you in to a shell interactive dialogue with mySQL, type q to exit."
/usr/local/mysql/bin/mysql -v
Make your life easier: "After installation, in order to use mysql commands without typing the full path to the commands you need to add the mysql directory to your shell path, (optional step) this is done in your “.bash_profile” file in your home directory, if you don’t have that file just create it using vi or nano:"
cd ; nano .bash_profile
paste in and save:
export PATH="/usr/local/mysql/bin:$PATH"
"The first command brings you to your home directory and opens the .bash_profile file or creates a new one if it doesn’t exist, then add in the line above which adds the mysql binary path to commands that you can run. Exit the file with type “control + x” and when prompted save the change by typing “y”. Last thing to do here is to reload the shell for the above to work straight away."
source ~/.bash_profile
mysql -v
"You will get the version number again, just type “q” to exit."
Check out on which port the server is running:
in your terminal type in: mysql
and then
SHOW GLOBAL VARIABLES LIKE 'PORT';
use everytime a semikolon in the mysql client (shell)!
now you know your port and where you can configure your server(in the terminal with mysql shell/client). but for a successful connection with MySQL Benchmark or an other client you have to know more. username, passwort hostname and port. after the installation the root user has no passwort so set(howtoSetPW) the passwort in terminal with mysql shell/client. and the server is running local. so type in root, yourPW, localhost and 3007. have fun!
Try restarting the mysql or starting it if it wasn't started already. Type this within terminal.
mysql.server restart
To auto start go to the following link below:
How to auto-load MySQL on startup on OS X Yosemite / El Capitan
In my case I had a previous mySQL server installation (with non-standard port), and I re-installed to a different directory & port. Then I got the same issue (in windows). To resolve, you click on home + add new connection.
If you need to know the port of your server, you can find it when you start My SQL command line client and run command status (as below). In windows it is via All Programs -> MySQL -> MySQL ServerX.Y -> MySQL X.Y Command Line Client
I had the same issue, I solved this with the following steps:
Install the MySql (DMG) from this link
If the mysql package comes with the file name "mysql-5.7.13...." and
"MySql.prefPane" then your life is really easy.
Just click on "mysql-5.7.13...." and follow the instructions.
After the installation is done, click on "MySql.prefPane" and checkout "Only
for this user" in the popup. We use "MySql.prefPane" to start the mysql
server as this is really imp because without this you will end up having
errors.
Click on Start MySql Server in the next dialog box.
OR
If you don't see "MySql.prefPane" in the package then follow these steps:
Click on package "mysql-5.7.13...." and this will show you one password as
soon as installation is done. That password is use to start the connection.
You can change it. I will let you know in a while.
After installation save the password (this is really important - you'll need it later), open terminal.
$ cd /usr/local/mysql/bin/
$ ./mysql -u root -h localhost -p
And then type the password from above. This should start mysql>
To change the password:
$ cd /usr/local/mysql/bin/
$ ./mysqladmin -u root -p password 'new_password'
Enter Password: <type new password here>
$ ./mysql -u root -h localhost -p
... and log in with the new password.
After this you can go to MySql workbench and test connection. It should connect.
brew services start mysql defualt set --bind-address=127.0.0.1 with /usr/local/Cellar/mysql/5.6.27/homebrew.mxcl.mysql.plist,so replace --bind-address=127.0.0.1 with --bind-address=* or --bind-address=0.0.0.0
for mac : check the compatible version of mysql server in workbench>preference>MySql
if it's the same version with your mysql server in: cd /usr/local/
I am using those commands on MacOs after getting the same error
sudo /usr/local/mysql/support-files/mysql.server start
sudo /usr/local/mysql/support-files/mysql.server stop
sudo /usr/local/mysql/support-files/mysql.server restart
I had the same problem. I removed mySQL completely, and reinstall it using homebrew.
I had same problem, but it worked for me.
check if you have mysql installed
If you don't have mysql installed, download from this link: https://dev.mysql.com/downloads/mysql/
follow this instructions to install
https://dev.mysql.com/doc/mysql-osx-excerpt/5.7/en/osx-installation-pkg.html
You can test the connection without any problem.
(Sorry for my english, I agree fix me please)
I Hope I've helped.
Greetings.
I ran into the same scenario
I opened "System Preferences", clicked "MySQL", then clicked "Initialize Database" button. I entered a new password and saved it in a safe place.
After that i restarted the MySql Instance (in the System Preferences dialog as well).
After that i opened MySqlWorkbench and opened the default connection, entered the password i set before and: Viola, i can do whatever i want :-)
I had this same issue on Monterey. Make sure you're selecting the correct download according to your processor architecture.
Community download processor architecture
Also, for the Connection Method use the Local Socket/Pipe
connection method
Dropping this to share my experience.
I had the same problem and realise I didn't start MySql server.
The steps below shows how I was able to login:
To start your server, Go to Preference and find MySql.
Click on the instance tab to start the server
Afterward, return back to Mysql Workbench to start the Local Instance and you'll be connected.
Have fun !
I had similar issues in Mac OS Catalina and the easiest way to solve it is by downloading HOMEBREW package manager for mac. Follow the steps to install HOMEBREW and mysql server.
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
$ brew install mysql
$ brew tap homebrew/services
$ brew services start mysql
mysqladmin -u root password 'yourpassword'
Now try using mySQLworkbench it should work.

ERROR 2003 (HY000): Can't connect to MySQL server on localhost (10061)

I installed MySQL on Microsoft Windows 8 Using a noinstall Zip Archive. But when I tested by executing the following commands on windows prompt, the above error showed up.
C:\> "C:\MySQL\bin\mysqlshow"
C:\> "C:\MySQL\bin\mysql" test
You don't need to restart your windows. The easiest way to achieve this is
Go to <Extracted folder location>/bin/
Run mysqld (service)
close the cmd prompt
Run mysql.exe or the better way to do is add the location to PATH environment Variable
Go to Run type services.msc. Check whether MySQL services is running or not. If not, start it manually. Once it started, type mysqlshow to test the service.
Go to bin directory copy the path and set it as a environment variable.
Run the command prompt as admin and cd to bin directory.
Run command : mysqld –install
Now the services are successfully installed
Start the service in service windows of os
Type mysql and go
Though it is an old question, I am adding my answer in it, because the solution that worked for me on Windows 7 as an admin user, is missing in the answers' list. Though my solution is for installed MySQL, I am putting it for those who search for a solution for this error message. Here it is:
Click on the Windows 7 start button and type taskmgr in the search bar
Right click on the taskmgr program icon and select Run as administrator
In the Task Manager window, go to the Services tab
Right click on the MySQL service and click Start Service
The solution that worked for me is:
Downloaded mysql-8.0.22-winx64.zip file
Extracted the zip file
Moved the extracted folder to C:/Program Files
Opened cmd.exe as admin
Navigated to the directory cd C:\Program Files\mysql-8.0.22\mysql-8.0.22-winx64\bin
mysqld -install (Service successfully installed)
mysqld --initialize (no prompt)
Opened services.msc
Found MySQL
Right-click and start
I was stuck on this same issue for what felt like an eternity.
My problem was: I was running mysql from MAMP on port 8889, but when trying to connect to mysql from my command line, it was expecting port 3306. I was running out of ideas, so I tried:
mysql --port 8889
and happiness ensued:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.34-log MySQL Community Server (GPL)
Go to Run type services.msc. Check whether MySQL services is running or not. If not, start it manually.
Run your cmd as administrator.
What you will see is like this:
c:\windows\system32>
Go to your bin location by using cd..
like C:\mysql\bin (my location of bin in my computer is what you are seeing so chose yours correctly)
Run:
$ C:\mysql\bin>mysql --install
Service successfully installed.
Then run:
$ C:\mysql\bin>NET START MySql
The MySql service is starting
The MySql service was started successfully
Then the last step is running:
C:\mysql\bin>mysql -u root - p admin
It will ask for password don't enter anything first time
because it will use blank, and just press enter you are done.
And later you can set password too.
I have a windows 8.1 machine and mysql was not running at all even after trying to start mysqld with no error logs. This solution worked for me:
start cmd in admin mode
type in "net start mysql"
close current cmd window and open new cmd window
type in "mysql"
The mysqld service should now be available.
Had this issue in Windows 10 and MySQL 8. Resolved by following steps:
Searchbar(bottom left) > Task Manager > Run as administrator > Services > MySQL80
Well that could have some reasons.
THe first one is that the MySQL server/service not started.
If he is started you should check out the logfiles, and make sure there are no problems.
You could also uninstall the MySQL service and install XAMPP.
With XAMPP it is easier to manage this services.
Despite that my server was running, I had the same problem.
I found out that it was the port. So you need to specify the port:
mysql -u user -p --port 0000
The port on your machine may be different. To find out on which port mysql is running open the mysql ini file and look for port=. A port often used is 3306.
Example how to log on to mysql
mysql -u root -p --port 3306
Test if the server is running. You can use netstat for this. See https://serverfault.com/questions/260239/unable-to-connect-to-mysql-through-port-3306
If it is running, it may be the firewall. You can turn that off to test if that is the problem.
See the following manual to install Mysql as a service: https://dev.mysql.com/doc/refman/5.5/en/windows-start-service.html
First of all, you need to ensure the port number on which the server is running. Then you can run
mysql -u username -p --port portNumber
on the command line
Executing the mysqld command can solve your problem.
My SQL version is 5.7.
If you're using the no install zip, you need to execute mysqld.exe first to start the service, and then execute mysql.exe to open your connection.
The no install is nice, but if you intend to do any serious work with MySQL, you may want to consider either using the MSI to do a proper installation, or if you're doing web development work give XAMPP a try.
Check Mysqld.exe file is in your bin folder. if it is not there , just copy that file from any other computer and paste it on your bin folder.Just double click. Then run mysql.its solved
This error is flashed when we are trying to open mysql with out stating the service.
Open cmd prompt
to start the service type
mysqld --console
This will start the mysql service
Don't close this cmd prompt and open a new cmd prompt and
type
mysql -u root -p
Then enter ur password
Make sure your mysql is running in default port (3306) , if you are running mysql with different port (for example: 3307), you must specified the port number while connecting to the server.
If your port is different than default port, then try with this command.
mysql -u <username> -p --port<port number>
For eg:
mysql -u root -p --port 3307
Go and search Services in Windows
Now search Mysql. Right Click on it. By Default it's startup type will be 'Automatic'. Click on Properties on right clicked menu.
You will find startup type Switch it to -> Manual So that We can start and stop by our choice.
Now Get Back and Right Click to Mysql on Services and Click Start.
Now Mysql has started successfully
To get started
Start Mysql Shell
& type command
shell.connect({host: 'localhost',user: 'root'})
Now type password
To type sql commands:-
/sql
I had the same problem. I tried all of the answers above (and some from other websites). In the end, my issue was that my cache wasn't configured. I found that info in my error log and fixed it by changing the line in the file:
C:\MAMP\bin\apache\conf\extra\http-ssl.conf
There I removed the double quotes from the line:
SSLSessionCache "shmcb:/some/example/path/ssl_scache(512000)"
to:
SSLSessionCache shmcb:/some/example/path/ssl_scache(512000)
and saved with Ctrl+S and closed the file.
Here's the link that helped me:
https://wiki.apache.org/httpd/SSLSessionCache
I faced the same problem couple of times and each time the reason was different:
The solution that worked first time was that by "Abhishek Oza" which is same as that of "amey91" (see above)
The second time, my server was on a different port number than the default one(3036),so i was not able to connect.So I had to specify
the port number explicitly for making the connection which you can do
simply by writing: "mysql --host=127.0.0.1 --port=8081(specify your
port number here) mysql -u root -p"
In my case, which was a manual install using the .zip file.
I solved this by specifying the nonstandard MySQL and Data install locations in a my.ini.
https://dev.mysql.com/doc/refman/8.0/en/windows-create-option-file.html
Then I needed to run the data directory initialisation commands:
https://dev.mysql.com/doc/refman/8.0/en/data-directory-initialization-mysqld.html
Then running:
net start mysql
There is a possibility that your installation of MYSQL got corrupted. The best thing you can do is to search for MYSQL INSTALLER on your system and then run it again.
It will not download the mysql server again, it will just help you to set it up.
After that, edit your environment variables path and add the bin folder of your mysql to it.
By now, it should work.
In my case I have 2 different version of mysql in Windows OS and I solved the my problem by bottom step:
first stop all mysql service.
I create one config file in C:\mysqldata.cnf with bottom data(my mysql is in "C:/mysql-5.0.96-winx64" directory ):
[mysqld]
datadir = C:/mysql-5.0.96-winx64/data
port = 3307
then I run bottom command in cmd:
C:\mysql-5.0.96-winx64\bin\mysqld --defaults-file=C:\mysqldata.cnf --console
above step reference
then I create txt file in C:\resetpass.txt with bottom data:
UPDATE mysql.user SET password=PASSWORD('ttt') WHERE user='root';
then run mysqld with bottom command:
C:\mysql-5.0.96-winx64\bin\mysqld --init-file=C:\resetpass.txt --install mysql2 --console
net start mysql2
after these step you have one mysql service(with name mysql2) than run with port 3307.
I have 2 version of mysql with different user management tables(in version 5.0.96 user table difference with 5.5 version because of that I must be change table folder in first step)
you can run other mysql service with other port now(and you can run this steps with different datadir, service name and port for it again)
First make sure you have installed MYsql+Sqlyog(if you are using it.).
Start Registry Editor (Regedt32.exe).
Locate the following key in the registry:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters
On the Edit menu, click Add Value, and then add the following registry value:
Value Name: MaxUserPort
Data Type: REG_DWORD
Value: 65534
This sets the number of ephemeral ports available to any user. The valid range is
between 5000 and 65534 (decimal). The default value is 0x1388 (5000 decimal).
On the Edit menu, click Add Value, and then add the following registry value:
Value Name: TcpTimedWaitDelay
Data Type: REG_DWORD
Value: 30
This sets the number of seconds to hold a TCP port connection in TIME_WAIT state before
closing. The valid range is between 30 and 300 decimal, although you may wish to check
with Microsoft for the latest permitted values. The default value is 0x78 (120 decimal).
Quit Registry Editor.
Reboot the machine.
I will advise to use first check if my.ini exist in mysql folder in c drive or in windows folder
mysqld -install (Service successfully installed)
mysqld --initialize (no prompt)
Also another advise is not to use mysql 8, since it is not compatible with wordpress or any other opensource yet, there are lot of changes between version 5 and version 8, so if you are using mysql please use version 5.x.
Steps to resolve the problem ->
Go to command prompt and at root of c (c:\ if environment variable-path is set)
type -> c:\>mysqld -install.
Then type -> c:\mysqld --initialize
Now create one .txt file (here in our example we are taking init.txt) at root of mysql(may vary according to your directory) ->c:\MySQL\init.txt.
Now open that .txt file(init.txt) and write in it ->
5.open Run console and write -> Services.msc
6.Then Services window will be open ,there select MYSQL,Double click It and press start
7.Finally open new Command prompt window and type at root of c (c:) ->c:\>mysqlsh
due to this shell will execute and here you are ready to go....
mysql -u root --port 3308
this one help me
port number from phpmyadmin default port is 3306

Macports mysql5 setup under Snow Leopard

I've managed to get my install all screwed up as I'm a newbie and messing up is what I'm good at.
1) Can anyone reference a credible set of setup instructions for this stuff?
2) Can I just uninstall and start from scratch (passwords and all) through macports? I tried and a bunch of mysql directories stuck around after the unistall, WTF!?
3) Where does mysqld.sock need to reside and what file settings do I need to apply to get it playing nice with php5 and apache2 ?
Does it live in /tmp or in /opt/local/var/run/mysql5/ ?
Do I need to configure /opt/local/etc/php5/php.ini? opt/local/var/db/php5/mysql.ini? /opt/local/etc/mysql5/my.cnf?
4) I've run > sudo -u mysql mysql_install_db5. If I run it again will it mess me up?
I ran mysqladmin -u root password 'mypw' and got an error saying the mysql could not connect through the socket. So does this mean my password is now set? Is there a way I can tell?
5) The syntax of the password statement changes from blog to blog. Is my password 'mypw' or mypw (without the quotes)? (My password is not actually mypw)
6) What are the appropriate commands to start and stop mysql? I DO NOT want it to start when my machine starts.
Thanks Jergason, that's tremendously helpful.
I'd really like to stay consistent with the macports installation of mysql since it limits the damage I can do to my machine.
So the outstanding questions now are...
1) What is the default socket location for the macports mysql install?
2) What are the appropriate commands to start and stop mysql? I DO NOT want it to start when my machine starts. Some blogs say to use launchctl -load others say sudo /path/mysql_safe5. If you havn't set the password (as I don't intend to) do you need to use the -user -p parameters?
3) What are the necessary directory ownerships and permissions to get macports mysql working. It doesn't seem that macports sets any of that.
Nerdling
Check /tmp for the MacPorts mysql socket
Not helpful. As previously stated, I understand that /tmp is the default location for the socket. My problem is that when I attempt to start mysql the socket does not materialize there, nor does it appear in /opt/local/var/run/mysql5/. This tells me I have a problem starting mysql, which means there is something wrong with the setup. What it could be, I don't know. I'm afraid I wasn't born with an a priori knowledge of such things.
-What SPECIFIC directory SHOULD the socket be in for the mysql5 install using macports under Snow Leopard?
If you're using 5 different sites' tips on how to run mysql and you're surprised you've gotten lost … well, I'll just leave it at try to follow just one site at a time and back out all changes before going to the next.
Not helpful. I did exactly what you're suggesting to begin with. What do you think I did, mix and match instructions from 5 blogs? This is why I asked about clean uninstalls. I wanted to know if a state change from a previous install could effect future "clean" installs.
-What SPECIFIC blog describes the confirmed-to-be-correct install procedure of mysql5 using macports under Snow Leopard.
I think the only problem you're having with MacPorts mysql (what problem are you having??) is that it's got a socket where you don't expect it and it's using a default configuration.
Not helpful. Me too, see my original post. I think the problem lies with how mysql is started (or isn't starting).
-Do you have any SPECIFIC suggestions on how to correct the problem?
This should have been explained after the install was completed by MacPorts.
Not helpful. It wasn't, or I didn't see it if it was. Maybe because I was using Porticus.
-Could you be more SPECIFIC? At WHAT point should I have seen WHAT message?
Please see ${prefix}/share/doc/mysql5/ for ideas and/or contact the developer list. Also consider contacting the maintainer of the port; they likely know the most about it since they manage it.
Not helpful. RTM, Google is your friend, and all that. Yep, went there first. Although I will admit I may have overlooked something as I'm not inclined to spend days reading some poorly written docs when I have other work to do to when I'm sure my problem is a simple one.
If you want to give me a SPECIFIC document that I may have overlooked, THAT would be helpful.
I was hoping for some simple, focused help like what Jergason provided. I (and others reading this) appreciate Jergason's willingness to convey his knowledge in order to accelerate the learning process of people new to various technologies. To him, the knowledge is basic, but to new-comers it's a massive time-saver. The true mark of intelligence is the understanding that technology knowledge is not a priori and that even basic help is of great help. Thanks again J-man.
I've just installed mysql5 myself, and since this is always a pain I've noted the process that I followed. This was done on Leopard, but I imagine the process is the same on Snow Leopard and Lion. It doesn't answer all the original questions, but at least it's something of a guide.
Installing Mysql
First, install mysql5 server with:
sudo port install mysql5-server
Just installing mysql5 doesn't install the server.
Pay attention to the console output, it includes instructions for setting up macports. You might want to copy and paste it to a text file. The following is based on it.
Instead of mysql5-server, you could use a port such as mysql55-server, mysql56-server, mariadb-server or percona-server to get a more recent version of mysql, or a fork. If you do, pay attention to the console output, as the following instructions are based on mysql5-server and will need to be adjusted to use the correct executables and paths.
If this is a new install, set up the database:
sudo -u _mysql mysql_install_db5
That outputs some generic instructions, which I don't think are entirely appropriate for macports. In my opinion the best way to load mysql5 as a daemon is to use macport's method:
sudo port load mysql5-server
As well as starting mysql5, this permanently loads it - it will run on boot up. To stop this later:
sudo port unload mysql5-server
If you don't want to run it as a daemon, you can run it at the command line:
sudo /opt/local/lib/mysql5/bin/mysqld_safe
Check that it's running by logging in at the command line:
mysql5 -u root -p
By default, the password is empty, so just press enter when prompted. To set a root password:
/opt/local/lib/mysql5/bin/mysqladmin -u root password 'correct horse battery staple'
Instructions for setting up both macports php and the native php installation follow.
Setup Macports PHP
Assuming you've already got macports php installed and running. You need to install php5-mysql (or something like php54-mysql depending on which version of php you're using):
sudo port install php5-mysql
This installs the mysql, mysqli and pdo drivers.
Now look in your /opt/local/etc/php5 directory, if you don't already have a php.ini configuration file copy either php.ini-development or php.ini-production to php.ini. Now edit php.ini and search for the appropriate lines to add:
pdo_mysql.default_socket=/opt/local/var/run/mysql5/mysqld.sock
and:
mysql.default_socket = /opt/local/var/run/mysql5/mysqld.sock
and:
mysqli.default_socket = /opt/local/var/run/mysql5/mysqld.sock
If you don't want to configure these, you can set them explicitly in your php script when you connect.
If you're having trouble connecting you might want to look at the other nearby settings, and compare with php.ini-development and php.ini-production to see what's been changed.
Then use the script below, or something similar to test that you can connect with php.
Setup System PHP
OS X's php comes with mysql and mysqli support built in (but not pdo), so all you need to do is set the macport unix socket. The default location is /opt/local/var/run/mysql5/mysqld.sock. Find the correct place in /etc/php.ini (if you don't have it already, copy it from /etc/php.ini.default) to add:
mysql.default_socket = /opt/local/var/run/mysql5/mysqld.sock
and:
mysqli.default_socket = /opt/local/var/run/mysql5/mysqld.sock
If you don't want to configure these, you can set them explicitly in your php script when you connect.
Test Script
Here's a php script to check that it can connect. Obviously, you won't normally use the root account in your php scripts so you might want to first create another mysql account for testing the connection. The PDO connection won't work for native php since that doesn't have PDO drivers.
<?php
$username = 'root';
$password = 'correct horse battery staple';
/* Try mysql: */
$connection = mysql_connect('localhost', $username, $password);
if ($connection === FALSE) {
echo "Error connecting using mysql.\n\n";
echo "Error ".mysql_errno().": ".mysql_error()."\n\n";
}
else {
echo "Connected using mysql.\n\n";
mysql_close($connection);
}
/* Try mysqli: */
$connection = mysqli_connect('localhost', $username, $password);
if ($connection->connect_error) {
echo "Error connecting using mysqli:\n\n";
echo "Error ".$connection->connect_errno.": ".$connection->connect_error."\n\n";
}
else {
echo "Connected using mysqli.\n\n";
$connection->close();
}
/* Try pdo:
* Won't work for the version of php supplied with OS X. */
try {
$pdo = new PDO('mysql:host=localhost', $username, $password);
echo "Connected using PDO.\n\n";
$pdo = null;
}
catch(PDOException $e) {
echo "Error connecting using PDO:\n\n";
echo "Error ".$e->getCode().": ".$e->getMessage()."\n\n";
}
I've just run into the same issue myself. To get my MySQL working, I simply had to do
sudo chmod -Rf 777 /opt/local/var/db/mysql5
The attempt to do sudo -u _mysql mysql_db5 failed, but I could then do
mysqladmin -u root password the_new_password
without any problem.
If you don't want MySQL to start automatically when you turn the machine on, simply do this:
sudo launchctl unload -w /Library/LaunchDaemons/org.macports.mysql5.plist
Then to start/stop MySQL:
start:
sudo /opt/local/etc/LaunchDaemons/org.macports.mysql5/mysql5.wrapper start
stop:
sudo /opt/local/etc/LaunchDaemons/org.macports.mysql5/mysql5.wrapper stop
Hope this helps.
1)Hivelogic has a great walkthrough on compiling mysql for Snow Leopard from the source. It is not actually that difficult, and if you do it yourself you will know where it is.
2) You should be able to just run sudo port uninstall mysql5. It may yell at you about dependencies. If so you will have to uninstall those first. Uninstalling MySQL should remove all config settings including passwords.
3) Where does mysqld.sock need to reside and what file settings do I need to apply to get it playing nice with php5 and apache2 ? The location of mysqld.sock is less important than the path to it. You set the path to mysqld.sock in your php.ini file. If you have a php.ini file in /opt/local/bin, then you installed the macports version of php. You can use that one, or you can use the default Apple version. Either way, you need to configure the php.ini file in order to tell your php installation where to go to use it. There should be a line in your php.ini file that says
mysql.default_socket = <whatever>
If it is not there, you can add it. You should set this line equal to the location of your mysqld.sock. Mine looks like this:
mysql.default_socket = /Applications/MAMP/tmp/mysql/mysql.sock
Your Apache configuration
4) I've run > sudo -u mysql mysql_install_db5. If I run it again will it mess me up? I ran mysqladmin -u root password 'mypw' and got an error saying the mysql could not connect through the socket. So does this mean my password is now set? Is there a way I can tell? If you could not connect to the socket, then your password was not set.
5) The syntax of the password statement changes from blog to blog. Is my password 'mypw' or mypw (without the quotes)? (My password is not actually mypw) You don't need the quotes around the password in the command line.
Check /tmp for the MacPorts mysql socket.
If you're using 5 different sites' tips on how to run mysql and you're surprised you've gotten lost … well, I'll just leave it at try to follow just one site at a time and back out all changes before going to the next.
I think the only problem you're having with MacPorts mysql (what problem are you having??) is that it's got a socket where you don't expect it and it's using a default configuration. This should have been explained after the install was completed by MacPorts. Please see ${prefix}/share/doc/mysql5/ for ideas and/or contact the developer list.
Also consider contacting the maintainer of the port; they likely know the most about it since they manage it.
the php.ini being used on the command line may be different than the one being used by apache
You can check this with
phpinfo.php
<?php
phpinfo();
?
by doing a
php phpinfo.php | grep php.ini
if it shows
Configuration File (php.ini) Path => /opt/local/etc/php5
and the test script of Daniel James answer (saved as testconnect.php) also fails
then you might want to fix by creating a symbolic link:
/opt/local/etc/php5>sudo ln -s /etc/php.ini php.ini
after that the testconnect should work
php testconnect.php
Connected using mysql.
Connected using mysqli.
Connected using PDO.
I figured out the problem. Despite the fact that Apache was not running, for some reason Apache was blocking MySQL from running (and thus from creating the socket). (Yea, weird right?) Before I ran the install, I issued the command to stop apache, then the install went swimmingly. Even now, before I start MySQL, I first have to tell Apache (which is not running) to stop.
And just for the record, I know it's not running because I get
"httpd (no pid file) not running"
I'm not having it run automatically on start up or any bone-head crap like that.
I'll try to write up a walk-through so that no one else has to go through this BS.
When installing php5 via macports the package doesn't come automatically with mysql extension, so it cannot connect to mysql via php (but you probably can through command line).
Let's check first if your mysql is running properly:
mysqladmin5 -uroot -pYOURROOTPASSWORD ping
If it's alive, you can verify all the variables by:
mysqladmin5 -uroot -pYOURROOTPASSWORD variables
You should see your "socket" location is defined as
/opt/local/var/run/mysql5/mysqld.sock
Using macports install missing components in 2 minutes... here's how:
sudo port install php5-mysql
To use mysqlnd with a local MySQL server, edit /opt/local/etc/php5/php.ini and set
mysql.default_socket, mysqli.default_socket and pdo_mysql.default_socket
to /opt/local/var/run/mysql5/mysqld.sock
Then to restart your Apache type:
sudo /opt/local/apache2/bin/apachectl -k restart
Now everything should be up and running properly. Hope that helps.