I have a MySQL server running on my work computer, which runs Mac OS X Maverick. I want to access it from home computer, which also has Mac OS Maverick. From terminal, if in 1 tab, I ssh into my.
13 3 likes 51,019 views Last modified Nov 24, 2013 4:14 PM
This User Tip describes how to download, install, and setup MySQL for use on OS X. It includes an optional section for Perl support. Please read through the entire User Tip before starting.
Requirements:
- Basic understanding of Terminal.app and how to run command-line programs.
- Basic usage of vi. You can substitute nano if you want.
Optional:
Xcode (with command line tools) is required for the MySQL Perl driver.
Lines in bold are what you will have to type in at the Terminal.
Replace <your local host> with the name of your machine. Ideally, it should be a one-word name with no spaces or punctuation. It just makes life easier.
Replace <your short user name> with your short user name.
Download MySQL from a local mirror. You want the Mac OS X ver. 10.6 (x86, 64-bit), DMG Archive. At the time of writing this User Tip, the current version is 5.5.34. By the time you read this, there may be a newer version to use instead. Open the archive mysql-5.5.34-osx10.6-x86_64.dmg.
Install only the mysql-5.5.34-osx10.6-x86_64.pkg package. Ignore everything else.
MySQL comes with a Preference Pane and Startup Item that are based on technologies that Apple deprecated years ago. If you install the Preference Pane or Startup Item, your MySQL installation could fail at some point in the future when Apple removes support for Startup Items. This was reported to MySQL a long time ago. Until MySQL gets around to fixing this bug, we will have to do it ourselves.
Create a launchd config file for MySQL:
sudo vi /Library/LaunchDaemons/com.mysql.mysql.plist
Use the following content:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE plist PUBLIC '-//Apple//DTD PLIST 1.0//EN' 'http://www.apple.com/DTDs/PropertyList-1.0.dtd'>
<plist version='1.0'>
<dict>
<key>KeepAlive</key>
<true/>
<key>Label</key>
<string>com.mysql.mysqld</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/mysql/bin/mysqld_safe</string>
<string>--user=mysql</string>
</array>
</dict>
</plist>
(if you are new to vi, type 'i' to enter insert mode. Then ⌘-v to paste the above content. Press escape to get back to command mode. Then type 'ZZ' to save and exit.)
The default path for the MySQL socket is not appropriate for MacOS X. Until it is changed, MySQL will not be able to communicate with PHP should you choose to enable that. Let's fix this now.
Create a config file for MySQL that matches the Apple PHP build:
sudo vi /etc/my.cnf
Use the following content:
[client]
socket=/var/mysql/mysql.sock
[mysqld]
socket=/var/mysql/mysql.sock
Start MySQL:
sudo launchctl load -w /Library/LaunchDaemons/com.mysql.mysql.plist
MySQL is now up and running. Don't forget to set a root password and configure your users if this is all you need.
If you need to configure Perl support for MySQL, don't setup a root password just yet. Having no password will make testing the Perl module easier. Keep reading...
You will need Xcode and command line tools for this part.
Download the MySQL Perl driver.
Extract the archive with:
tar zxvf DBD-mysql-4.020.tar.gz
Move into the directory:
cd DBD-mysql-4.020
First, fix the MySQL client library. (credit)
For Lion, type:
sudo install_name_tool -id /usr/local/mysql-5.5.34-osx10.6-x86_64/lib/libmysqlclient.18.dylib /usr/local/mysql-5.5.34-osx10.6-x86_64/lib/libmysqlclient.18.dylib
Next, build DBD::mysql with:
perl Makefile.PL --mysql_config=/usr/local/mysql/bin/mysql_config --testsocket=/var/mysql/mysql.sock --testuser=root
make
make test
sudo make install
Now you can set a root password and configure your users.
MySQL JDBC for macOS
This tutorial shows how to set up a data source and connect to a MySQL® database using the Database Explorer app or the command line. This tutorial uses the MySQL Connector/J 5.1.46 driver to connect to a MySQL Version 5.5.16 database.
Step 1. Verify the driver installation.
If the JDBC driver for MySQL is not installed on your computer, find the link on the Driver Installation page to install the driver. Follow the instructions to download and install this driver on your computer.
Step 2. Set up the data source.
You set up a data source using the Database Explorer app or the command line.
Set Up Data Source Using Database Explorer
- Open the Database Explorer app by clicking the Apps tab on the MATLAB® Toolstrip. Then, on the right of the Apps section, click the Show more arrow to open the apps gallery. Under Database Connectivity and Reporting, click Database Explorer. Alternatively, enter
databaseExplorer
at the command line. - In the Data Source section, select Configure Data Source > Configure JDBC data source.The JDBC Data Source Configuration dialog box opens.
- In the Name box, enter a name for your data source. (This example uses a data source named
MySQL
.) You use this name to establish a connection to your database. - From the Vendor list, select
MySQL
. - In the Driver Location box, enter the full path to the JDBC driver file.
- In the Database box, enter the name of your database. In the Server box, enter the name of your database server. Consult your database administrator for the name of your database server. In the Port Number box, enter the port number.
- Under Connection Options, in the Name column, enter the name of an additional driver-specific option. Then, in the Value column, enter the value of the driver-specific option. Click to specify additional driver-specific options.
- Click . The Test Connection dialog box opens. Enter the user name and password for your database, or leave these boxes blank if your database does not require them. Click .If your connection succeeds, the Database Explorer dialog box displays a message indicating the connection is successful. Otherwise, it displays an error message.
- Click . The JDBC Data Source Configuration dialog box displays a message indicating the data source is saved successfully. Close this dialog box.
Set Up Data Source Using Command Line
- Create a JDBC data source for a MySQL database.
- Set the JDBC connection options. For example, this code assumes that you are connecting to a JDBC data source named
MySQL
, database serverdbtb01
, port number3306
, and full path of the JDBC driver file/home/user/DB_Drivers/mysql-connector-java-5.1.17-bin.jar
.To add JDBC driver-specific connection options, use theaddConnectionOptions
function. - Test the database connection by specifying the user name
username
and passwordpwd
, or leave these arguments blank if your database does not require them. - Save the JDBC data source.
After you complete the data source setup, connect to the MySQL database using the Database Explorer app or the JDBC driver and command line.
Step 3. Connect using the Database Explorer app or the command line.
Connect to MySQL Using Database Explorer App
- On the Database Explorer tab, in the Connections section, click and select the data source for the connection.
- In the connection dialog box, enter a user name and password, or leave these boxes blank if your database does not require them. Click .The Catalog and Schema dialog box opens.
- In the Catalog list, select the catalog. Click .The app connects to the database and displays its tables in the Data Browser pane. A data source tab appears to the right of the pane. The title of the data source tab is the data source name that you defined during the setup. The data source tab contains empty SQL Query and Data Preview panes.
- Select tables in the Data Browser pane to query the database.
- Close the data source tab to close the SQL query. In the Connections section, close the database connection by clicking .
Note
If multiple connections are open, close the database connection of your choice by selecting the corresponding data source from the list.
Connect to MySQL Using JDBC Driver and Command Line
- Connect to a MySQL database using the configured JDBC data source, user name
username
, and passwordpwd
. - Close the database connection.
See Also
Apps
Functions
close
|configureJDBCDataSource
|database
|saveAsJDBCDataSource
|setConnectionOptions
|testConnection