• Aucun résultat trouvé

Advanced configuration

Dans le document Ubuntu Server Guide Changes, errors and bugs (Page 131-135)

Creating a tuned configuration

There are a number of parameters that can be adjusted within MySQL’s configuration files that will allow you to improve the performance of the server over time.

Many of the parameters can be adjusted with the existing database, however some may affect the data layout and thus need more care to apply.

First, if you have existing data, you will need to carry out a mysqldump and reload:

mysqldump −−a l l−d a t a b a s e s −−r o u t i n e s −u r o o t −p > ~/ fulldump . s q l

This will then prompt you for the root password before creating a copy of the data. It is advisable to make sure there are no other users or processes using the database whilst this takes place. Depending on how much data you’ve got in your database, this may take a while. You won’t see anything on the screen during this process.

Once the dump has been completed, shut down MySQL:

sudo s e r v i c e mysql s t o p

It’s also a good idea to backup the original configuration:

sudo r s y n c −avz / e t c / mysql / r o o t / mysql−backup Next, make any desired configuration changes.

Then delete and re-initialise the database space and make sure ownership is correct before restarting MySQL:

sudo rm −r f / var / l i b / mysql /*

sudo mysqld −−i n i t i a l i z e

sudo chown −R mysql : / var / l i b / mysql sudo s e r v i c e mysql s t a r t

The final step is re-importation of your data by piping your SQL commands to the database.

c a t ~/ fulldump . s q l | mysql

For large data imports, the ‘Pipe Viewer’ utility can be useful to track import progress. Ignore any ETA times produced by pv, they’re based on the average time taken to handle each row of the file, but the speed of inserting can vary wildly from row to row with mysqldumps:

sudo apt i n s t a l l pv

pv ~/ fulldump . s q l | mysql Once that is complete all is good to go!

Note

This is not necessary for all my.cnf changes. Most of the variables you may wish to change to improve performance are adjustable even whilst the server is running. As with anything, make sure to have a good backup copy of config files and data before making changes.

MySQL Tuner

MySQL Tuner connects to a running MySQL instance and offer configuration suggestions to optimize the database for your workload. The longer the server has been running, the better the advice mysqltuner can provide. In a production environment, consider waiting for at least 24 hours before running the tool. You can install mysqltuner from the Ubuntu repositories:

sudo apt i n s t a l l m ys q lt une r Then once its been installed, run:

m y s q l t u n e r

and wait for its final report. The top section provides general information about the database server, and the bottom section provides tuning suggestions to alter in your my.cnf. Most of these can be altered live on the server without restarting; look through the official MySQL documentation (link in Resources section) for the relevant variables to change in production. The following example is part of a report from a production database showing potential benefits from increasing the query cache:

−−−−−−−− Recommendations −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−

Ge n e ra l recommendations :

Run OPTIMIZE TABLE t o defragment t a b l e s f o r b e t t e r p e r f o r m a n c e I n c r e a s e t a b l e _ c a c h e g r a d u a l l y t o a v o i d f i l e d e s c r i p t o r l i m i t s V a r i a b l e s t o a d j u s t :

k e y _ b u f f e r _ s i z e (> 1 . 4G) query _c ac he_ s i z e (> 32M) t a b l e _ c a c h e (> 6 4 )

i n n o d b _ b u f f e r _ p o o l _ s i z e (>= 22G)

It goes without saying that performance optimization strategies vary from application to application. So for example, what works best for Wordpress might not be the best for Drupal or Joomla. Performance can be dependent on the types of queries, use of indexes, how efficient the database design is and so on. You may find it useful to spend some time searching for database tuning tips based on what applications you’re using.

Once you’ve reached the point of diminishing returns from database configuration adjustments, look to the application itself for improvements, or invest in more powerful hardware and/or scaling up the database environment.

Resources

• See the MySQL Home Page for more information.

• Full documentation is available in both online and offline formats from the MySQL Developers portal

• For general SQL information see the O’Reilly books Getting Started with SQL: A Hands-On Approach for Beginners by Thomas Nield as an entry point and SQL in a Nutshell as a quick reference.

• The Apache MySQL PHP Ubuntu Wiki page also has useful information.

PostgreSQL

PostgreSQL is an object-relational database system that has the features of traditional commercial database systems with enhancements to be found in next-generation DBMS systems.

Installation

To install PostgreSQL, run the following command in the command prompt:

sudo apt i n s t a l l p o s t g r e s q l

The database service is automatically configured with viable defaults, but can be customized based on your specialized needs.

Configuration

PostgreSQL supports multiple client authentication methods. By default, theIDENTauthentication method is used for postgres and local users. Please refer to the PostgreSQL Administrator’s Guide if you would like to configure alternatives like Kerberos.

The following discussion assumes that you wish to enable TCP/IP connections and use the MD5 method for client authentication. PostgreSQL configuration files are stored in the /etc/postgresql/<version>/main directory. For example, if you install PostgreSQL 12, the configuration files are stored in the /etc/postgresql /12/main directory.

Tip

To configureIDENT authentication, add entries to the /etc/postgresql/12/main/pg_ident.conf file. There are detailed comments in the file to guide you.

To enable other computers to connect to your PostgreSQL server, edit the file /etc/postgresql/12/main/

postgresql.conf

Locate the line#listen_addresses = ‘localhost’and change it to:

l i s t e n _ a d d r e s s e s = ’ * ’

Note

To allow both IPv4 and IPv6 connections replace ‘localhost’ with ‘::’

For details on other parameters, refer to the configuration file or to the PostgreSQL documentation for information on how they can be edited.

Now that we can connect to our PostgreSQL server, the next step is to set a password for thepostgresuser.

Run the following command at a terminal prompt to connect to the default PostgreSQL template database:

sudo −u p o s t g r e s p s q l t e m p l a t e 1

The above command connects to PostgreSQL database template1as userpostgres. Once you connect to the PostgreSQL server, you will be at a SQL prompt. You can run the following SQL command at the psql prompt to configure the password for the userpostgres.

ALTER USER p o s t g r e s with e n c r y p t e d password ’ your_password ’ ;

After configuring the password, edit the file /etc/postgresql/12/main/pg_hba.conf to use MD5 authentica-tion with thepostgres user:

l o c a l a l l p o s t g r e s md5

Finally, you should restart the PostgreSQL service to initialize the new configuration. From a terminal prompt enter the following to restart PostgreSQL:

sudo s y s t e m c t l r e s t a r t p o s t g r e s q l . s e r v i c e Warning

The above configuration is not complete by any means. Please refer to the PostgreSQL Admin-istrator’s Guide to configure more parameters.

You can test server connections from other machines by using the PostgreSQL client.

sudo apt i n s t a l l p o s t g r e s q l−c l i e n t

p s q l −h p o s t g r e s . example . com −U p o s t g r e s −W Note

Replace the domain name with your actual server domain name.

Backups

PostgreSQL databases should be backed up regularly. Refer to the PostgreSQL Administrator’s Guide for different approaches.

Resources

• As mentioned above the PostgreSQL Administrator’s Guide is an excellent resource. The guide is also available in the postgresql-doc-12 package. Execute the following in a terminal to install the package:

sudo apt i n s t a l l p o s t g r e s q l−doc−12

To view the guide enter file :///usr/share/doc/postgresql−doc−12/html/index.html into the address bar of your browser.

• For general SQL information see the O’Reilly books Getting Started with SQL: A Hands-On Approach for Beginners by Thomas Nield as an entry point and SQL in a Nutshell as a quick reference.

• Also, see the PostgreSQL Ubuntu Wiki page for more information.

Dans le document Ubuntu Server Guide Changes, errors and bugs (Page 131-135)