How to connect to the proper MySQL database on OS X? (Two mysqld instances running)
I have two instances of MySQL server running on my machine. One is the mysql package that came bundled with MAMP, and the other is a 64 bit installation that I downloaded so I could work with a python library of mine.
This is strictly for development.
Both are running:
:~ zachary$ ps -awwx | grep mysql
1944 ?? 0:00.02 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-file=/usr/local/mysql/data/MyMachine-2.local.pid
2016 ?? 0:10.17 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/usr/local/mysql/data/MyMachine-2.local.err --pid-file=/usr/local/mysql/data/MyMachine-2.local.pid
6824 ?? 0:00.02 /bin/sh /Applications/MAMP/Library/bin/mysqld_safe --port=8889 --socket=/Applications/MAMP/tmp/mysql/mysql.sock --lower_case_table_names=0 --pid-file=/Applications/MAMP/tmp/mysql/mysql.pid --log-error=/Applications/MAMP/logs/mysql_error_log
6894 ?? 0:00.36 /Applications/MAMP/Library/libexec/mysqld --basedir=/Applications/MAMP/Library --datadir=/Applications/MAMP/db/mysql --user=mysql --lower_case_table_names=0 --log-error=/Applications/MAMP/logs/mysql_error_log.err --pid-file=/Applications/MAMP/tmp/mysql/mysql.pid --socket=/Applications/MAMP/tmp/mysql/mysql.sock --port=8889
Yet my PHP only seems to want to be able to connect to MAMP's MySQL server. I believe that mysql_connect() lets you specify a port but I'm not sure how to deal with this.
Thanks.
Edit: it seems that MAMP has a preference for its own MySQL server when you use mysql_connect() on a MAMP ins开发者_JAVA百科tallation -- not sure why -- can anyone please explain? But I was able to connect to my manually installed MySQL server using this syntax:
mysql_connect(':/tmp/mysql.sock', 'username', 'pw');
It looks like MAMP is running on port 8889 and the other instance is on the default port (3306). To be sure, run netstat -lnp
to see what ports all listening processes are using. (Warning: double check the options to netstat, as I am a Linux user, not a Mac user).
AJ right.
And to connect to MySQL server on specific port (other than 3306)
mysql_connect("{$mysqlServerNameOrIp}:{$mysqlSpecificServerPort}", 'username', 'password');
精彩评论