Using the path to the current mysqlXX-server
port, download, install and clean (note the MySQL version here is 5.6, but this can change as future versions get released):
sudo make config-recursive install distclean -C /usr/ports/databases/mysql56-server
To add MySQL to startup, edit the rc.conf
file with:
sudo ee /etc/rc.conf
Now add the following to the files, with the bind-address argument that sets MySQL to only allow connections on the localhost. Save and exit (alternatively, use sudo sysrc mysql_enable=yes
command to add each line to the end of the file):
mysql_enable="YES"
mysql_args="--bind-address=127.0.0.1"
Startup MySQL now with the following command:
sudo service mysql-server start
Once MySQL has started, configure MySQL for a production enviroment with the following (be sure to generate a password for root user when prompted, for the rest hit RETURN
for the default settings):
sudo mysql_secure_installation
Next start the MySQL terminal interface with the following (inputting the password when prompted):
mysql -u root -p
Change the root user's username to something a little more entropic and flush the currently loaded priveledges for it to take effect with:
UPDATE mysql.user set user = '[username]' where user = 'root';
flush privileges;
Exit the MySQL CLI (exit
) and login again (entering password again when prompted) using the new username to ensure the change was successful:
mysql -u [username] -p
MySQL does not create a config file (.cnf
) for customisation by default, but it does have a default config file that should not be edited directly. To setup a customisable config file for MySQL, copy my-default.cnf
to /var/db/mysql/my.cnf
:
sudo cp /usr/local/share/mysql/my-default.cnf /var/db/mysql/my.cnf
To see what the currently loaded MySQL variables are, use the following MySQL command (will need to do in terminal with the command above to enter mode, or perhaps a MySQL program like Sequel Pro)(use SHOW VARIABLES LIKE '%variable_name%'
to show a specific loaded variable):
SHOW VARIABLES
Edit the MySQL config file using the below command, then remove the warning at the top of the file about editing this file:
sudo ee /var/db/mysql/my.cnf
Uncomment and change the innodb_buffer_pool_size
to the required size (note the MySQL commentation that recommends if the system is used as a dedicated server it is set to 70% of the total server RAM. i.e. 512mb / 100 * 70 = ~358mb)
innodb_buffer_pool_size = 358M
Add max_connections
directive with a required value to set the number of concurrent MySQL requests (note max_connections
is max_connections
+ 1 for admin usage). Note that there is no easy way to calculate this, MySQL say it "depends on the quality of the thread library on a given platform, the amount of RAM available, how much RAM is used for each connection, the workload from each connection, and the desired response time":
max_connections = 501
Add query_cache_size
directive and set it to either 0
to disable the query cache size limit, or very small value (Warning: setting this too high can slow down the system. Benchmarking and research will be needed):
query_cache_size = 0
Restart MySQL with:
sudo service mysql-server restart
can i suggest a tiny change?
:4 MYSQL_CNF_DIR="/usr/local/etc/my.cnf"; <-- easyer to find after install
:141 set_line "mysql_optfile='" $MYSQL_CNF_DIR; <-- not sure if this is the way shell script isnt my thing :P
:148 sudo cp /usr/local/share/mysql/my-default.cnf $MYSQL_CNF_DIR;