1 2 |
sudo apt-get install mysql-admin mysql-query-browser apt-get install mysql-workbench |
Both of these are desktop apps.
1 2 |
sudo apt-get install mysql-admin mysql-query-browser apt-get install mysql-workbench |
Both of these are desktop apps.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
[mysqld] socket=/path/to/mysql.sock datadir=/var/lib/mysql skip-locking skip-innodb # MySQL 4.x has query caching available. # Enable it for vast improvement and it may be all you need to tweak. query_cache_type=1 query_cache_limit=1M query_cache_size=32M # max_connections=500 # Reduced to 200 as memory will not be enough for 500 connections. # memory=key_buffer+(sort_buffer_size+read_buffer_size)*max_connections # which is now: 64 + (1 + 1) * 200 = 464 MB # max_connections = approx. MaxClients setting in httpd.conf file # Default set to 100. #max_connections=200 #interactive_timeout=180 interactive_timeout=100 #wait_timeout=180 #wait_timeout=100 # Reduced wait_timeout to prevent idle clients holding connections. #wait_timeout=30 wait_timeout=15 connect_timeout=10 # max_connect_errors is set to 10 by default #max_connect_errors=10 #table_cache=256 #table_cache=1024 # Checked opened tables and adjusted accordingly after running for a while. table_cache=512 #tmp_table_size=32M by default #thread_cache=128 # Reduced it to 32 to prevent memory hogging. Also, see notes below. thread_cache=32 # key_buffer=258M # Reduced it by checking current size of *.MYI files, see notes below. key_buffer=128M # Commented out the buffer sizes and keeping the default. # sort_buffer_size=2M by default. #sort_buffer_size=1M # read_buffer_size=128K by default. #read_buffer_size=1M # 1Mb of read_rnd_buffer_size for 1GB RAM -- see notes below. # read_rnd_buffer_size=256K by default. #read_rnd_buffer_size=1M # myisam_sort_buffer_size used for ALTER, OPTIMIZE, REPAIR TABLE commands. # myisam_sort_buffer_size=8M by default. #myisam_sort_buffer_size=64M # thread_concurrency = 2 * (no. of CPU) thread_concurrency=2 # log slow queries is a must. Many queries that take more than 2 seconds. # If so, then your tables need enhancement. log_slow_queries=/var/log/mysqld.slow.log long_query_time=2 [mysql.server] user=mysql basedir=/var/lib [safe_mysqld] err-log=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid open_files_limit=8192 [mysqldump] quick max_allowed_packet=16M [mysql] no-auto-rehash # Remove the next comment character if you are not familiar with SQL #safe-updates [isamchk] key_buffer=64M sort_buffer=64M read_buffer=16M write_buffer=16M [myisamchk] key_buffer=64M sort_buffer=64M read_buffer=16M write_buffer=16M [mysqlhotcopy] interactive-timeout [client] socket=/path/to/mysql.sock Below are notes on some of the important variables, I took down while tuning the config file. |
……>
query_cache_size:
MySQL 4 provides one feature that can prove very handy – a query cache. In a situation where the database has to repeatedly run the same queries on the same data set, returning the same results each time, MySQL can cache the result set, avoiding the overhead of running through the data over and over and is extremely helpful on busy servers.
……>
key_buffer_size:
The value of key_buffer_size is the size of the buffer used with indexes. The larger the buffer, the faster the SQL command will finish and a result will be returned. The rule-of-thumb is to set the key_buffer_size to at least a quarter, but no more than half, of the total amount of memory on the server. Ideally, it will be large enough to contain all the indexes (the total size of all .MYI files on the server).
A simple way to check the actual performance of the buffer is to examine four additional variables: key_read_requests, key_reads, key_write_requests, and key_writes.
If you divide the value of key_read by the value of key_reads_requests, the result should be less than 0.01. Also, if you divide the value of key_write by the value of key_writes_requests, the result should be less than 1.
……>
table_cache:
The default is 64. Each time MySQL accesses a table, it places it in the cache. If the system accesses many tables, it is faster to have these in the cache. MySQL, being multi-threaded, may be running many queries on the table at one time, and each of these will open a table. Examine the value of open_tables at peak times. If you find it stays at the same value as your table_cache value, and then the number of opened_tables starts rapidly increasing, you should increase the table_cache if you have enough memory.
……>
sort_buffer:
The sort_buffer is very useful for speeding up myisamchk operations (which is why it is set much higher for that purpose in the default configuration files), but it can also be useful everyday when performing large numbers of sorts.
……>
read_rnd_buffer_size:
The read_rnd_buffer_size is used after a sort, when reading rows in sorted order. If you use many queries with ORDER BY, upping this can improve performance. Remember that, unlike key_buffer_size and table_cache, this buffer is allocated for each thread. This variable was renamed from record_rnd_buffer in MySQL 4.0.3. It defaults to the same size as the read_buffer_size. A rule-of-thumb is to allocate 1KB for each 1MB of memory on the server, for example 1MB on a machine with 1GB memory.
……>
thread_cache:
If you have a busy server that’s getting a lot of quick connections, set your thread cache high enough that the Threads_created value in SHOW STATUS stops increasing. This should take some of the load off of the CPU.
……>
tmp_table_size:
“Created_tmp_disk_tables” are the number of implicit temporary tables on disk created while executing statements and “created_tmp_tables” are memory-based. Obviously it is bad if you have to go to disk instead of memory all the time.
Additional reference material:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
[client] port = 3306 socket = /var/run/mysqld/mysqld.sock [mysqld_safe] socket = /var/run/mysqld/mysqld.sock nice = 0 [mysqld] user = mysql pid-file = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock port = 3306 basedir = /usr datadir = /var/lib/mysql tmpdir = /tmp lc-messages-dir = /usr/share/mysql skip-external-locking #skip-locking bu hatalı satır external diyecen bind-address = 78.41.103.72 # 127.0.0.1 key_buffer = 16K max_allowed_packet = 1M thread_stack = 64K #192 thread_cache_size = 8 table_cache = 4 #sorun yok sort_buffer_size = 64K read_buffer_size = 256K read_rnd_buffer_size = 256K net_buffer_length = 2K #sorun yok #skip-bdb #hatali #skip-innodb # hatali myisam-recover = BACKUP query_cache_limit = 1M query_cache_size = 16M expire_logs_days = 10 max_binlog_size = 5M [mysqldump] quick quote-names max_allowed_packet = 16M [mysql] no-auto-rehash [isamchk] key_buffer = 8M sort_buffer_size = 8M [myisamchk] key_buffer = 8M sort_buffer_size = 8M [mysqlhotcopy] interactive-timeout !includedir /etc/mysql/conf.d/ |
Our application requires to access the MySQL database. For security reasons, we do not want this user to be the root MySQL user, so we define a user named www-data that has read access to the local databases and can only connect locally with some password.
1 2 3 4 5 6 7 8 9 |
user@kutayzorlu.com:$ mysql -p --user=root Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 9 Server version: 5.0.45-Debian_1ubuntu3-log Debian etch distribution Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql> |
1 2 3 4 5 6 |
mysql> GRANT SELECT ON *.* TO 'www-data'@'localhost' IDENTIFIED BY 'password'; Query OK, 0 rows affected (0.00 sec) mysql> exit Bye user@JeOS:$ |
Task: MySQL Server Remote Access
Change the ip address to public ip of server
/etc/mysql/my.cnf
1 |
Sample Setup |
1 2 3 4 5 6 7 8 9 10 11 12 |
+----------- server1 192.168.1.6 | +------------ tomcat1 192.168.1.7 | +------------------+ | | MySQL Server | --------+------------ apache2 192.168.1.8 | 192.168.1.5:3306 | --------+ +------------------+ | LAN 192.168.1.0/24 +------------- pc1 192.168.1.51 | | +-------------- pc25 192.168.1.76 |
You need to allow access to 192.168.1.5 from apache server located at 192.168.1.8.
Step #1: Configure MySQL Server For Remote Access
1 2 3 4 5 |
$ ssh root@192.168.1.5 [/bash] Edit /etc/my.cnf, enter: # vi /etc/my.cnf |
Modify or append as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# make sure the following line is deleted or commented out # skip-networking bind-address = 192.168.1.5 # ------------------ # Save and close the file. Restart the mysql server, enter: # service mysqld restart Verify that the TCP port 3306 is open, enter: Make Sure TCP Port # 3306 is Opened For Business # netstat -tulpn | grep :3306 |
Step #2: Linux Firewall Configuration For TCP Port # 3306
# >>>> You need to open TCP port # 3306 at the firewall level, enter:
1 2 3 4 5 6 7 8 9 10 11 |
# iptables -A INPUT -i eth0 -s 192.168.1.8 -p tcp --destination-port 3306 -j ACCEPT # service iptables save Step #3: Configure Database Remote Access # mysql -u root -p mysql mysql> update db set Host='192.168.1.8' where Db='salesdb'; mysql> update user set Host='192.1681.8' where user='foo'; mysql> \q # Login to 192.168.1.8 and type the following command to test mysql server remote access: $ mysql -u foo -h 192.168.1.5 -p salesdb |
Sample outputs:
1 2 3 4 5 6 7 |
Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 27720995 Server version: 5.0.77 Source distribution Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql> |
How Do I Enable Remote Access To MySQL Database Server?
You need type the following commands which will allow remote connections.
Step # 1: Login Using SSH (if server is outside your data center)
First, login over ssh to remote MySQL database server:
1 2 |
ssh user@server1.cyberciti.biz Step # 2: Edit my.cnf File |
Once connected you need to edit the MySQL server configuration file my.cnf using a text editor such as vi.
1 2 3 |
If you are using Debian Linux file is located at /etc/mysql/my.cnf location If you are using Red Hat Linux/Fedora/Centos Linux file is located at /etc/my.cnf location If you are using FreeBSD you need to create a file /var/db/mysql/my.cnf |
1 2 |
Edit /etc/my.cnf, run: # vi /etc/my.cnf |
Step # 3: Once file opened, locate line that read as follows
1 2 3 4 5 |
[mysqld] Make sure line skip-networking is commented (or remove line) and add following line bind-address=YOUR-SERVER-IP For example, if your MySQL server IP is 65.55.55.2 then entire block should be look like as follows: |
1 2 3 4 5 6 7 8 9 10 11 |
[mysqld] user = mysql pid-file = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock port = 3306 basedir = /usr datadir = /var/lib/mysql tmpdir = /tmp language = /usr/share/mysql/English bind-address = 65.55.55.2 # skip-networking |
Where,
bind-address : IP address to bind to.
skip-networking : Don’t listen for TCP/IP connections at all. All interaction with mysqld must be made via Unix sockets. This option is highly recommended for systems where only local requests are allowed. Since you need to allow remote connection this line should be removed from my.cnf or put it in comment state.
Step# 4 Save and Close the file
1 2 3 4 5 6 7 8 9 10 11 12 |
If you are using Debian / Ubuntu Linux, type the following command to restart the mysql server: # /etc/init.d/mysql restart If you are using RHEL / CentOS / Fedora / Scientific Linux, type the following command to restart the mysql server: # /etc/init.d/mysqld restart If you are using FreeBSD, type the following command to restart the mysql server: # /usr/local/etc/rc.d/mysql-server stop # /usr/local/etc/rc.d/mysql-server start OR # /usr/local/etc/rc.d/mysql-server restart |
Step # 5 Grant access to remote IP address
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
Connect to mysql server: $ mysql -u root -p mysql Grant access to a new database If you want to add a new database called foo for user bar and remote IP 202.54.10.20 then you need to type the following commands at mysql> prompt: mysql> CREATE DATABASE foo; mysql> GRANT ALL ON foo.* TO bar@'202.54.10.20' IDENTIFIED BY 'PASSWORD'; How Do I Grant Access To An Existing Database? Let us assume that you are always making connection from remote IP called 202.54.10.20 for database called webdb for user webadmin, To grant access to this IP address type the following command At mysql> prompt for existing database, enter: mysql> update db set Host='202.54.10.20' where Db='webdb'; mysql> update user set Host='202.54.10.20' where user='webadmin'; |
Step # 6: Logout of MySQL
Type exit command to logout mysql:
mysql> exit
Step # 7: Open port 3306
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
You need to open TCP port 3306 using iptables or BSD pf firewall. A sample iptables rule to open Linux iptables firewall /sbin/iptables -A INPUT -i eth0 -p tcp --destination-port 3306 -j ACCEPT OR only allow remote connection from your web server located at 10.5.1.3: /sbin/iptables -A INPUT -i eth0 -s 10.5.1.3 -p tcp --destination-port 3306 -j ACCEPT OR only allow remote connection from your lan subnet 192.168.1.0/24: /sbin/iptables -A INPUT -i eth0 -s 192.168.1.0/24 -p tcp --destination-port 3306 -j ACCEPT Finally save all rules (RHEL / CentOS specific command): # service iptables save A sample FreeBSD / OpenBSD pf rule ( /etc/pf.conf) pass in on $ext_if proto tcp from any to any port 3306 OR allow only access from your web server located at 10.5.1.3: pass in on $ext_if proto tcp from 10.5.1.3 to any port 3306 flags S/SA synproxy state |
Step # 8: Test it
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
From your remote system or your desktop type the following command: $ mysql -u webadmin –h 65.55.55.2 –p Where, -u webadmin: webadmin is MySQL username -h IP or hostname: 65.55.55.2 is MySQL server IP address or hostname (FQDN) -p : Prompt for password You can also use the telnet or nc command to connect to port 3306 for testing purpose: $ echo X | telnet -e X 65.55.55.2 3306 OR $ nc -z -w1 65.55.55.2 3306 Sample outputs: Connection to 65.55.55.2 3306 port [tcp/mysql] succeeded! |