CONTENT
  • CHANGES
Szukaj
counter

#top Instalacja


Instalację serwera MySQL można wykonać na różne sposoby: od kompilacji ze źdródeł poprzez instalację pojedynczych paczek rpm za pomocą polecenia rpm skończywszy na instalacja z użyciem menedżera pakietów, który zainstaluje wszystkie niezbędne paczki wraz zależnościami.
Celem niniejszego dokumentu nie jest rozważanie różnych metod instalacji tylko nieco usprawnienie użyteczności konfiguracji serwera MySQL po zainstalowaniu oraz wprowadzenie porządku / hierarchi położenia plików z których korzysta usługa w strukturze katalogowej.
Dobrą konwencją jest przechowywanie plików konfiguracyjnych każdego demona we własnym osobnym podkatalogu w katalogu etc, w przypadku instalacji MySQLa z paczki w systemach Linux z rodziny RedHat taki katalog należy utworzyć i przenieć do niego plik konfiguracyjny my.cnf. Więszkość poleceń ma wkompilowaną na sztywno defaultową ścieżkę do pliku my.cnf, w związku z czym aby każdemu poleceniu nie podawać przy wywołaniu ścieżki do nowej lokalizacji pliku my.cnf poprzez wywołanie z argumentem --defaults-file= potrzebne będzie utworzenie linku symbolicznego do pierwotnej lokalizacji.
Zgodnie definicja FHS: katalog /srv zawiera: Data for services provided by this system toteż jako katalog w którym MySQL będzie przechowywał dane został wybrany: /srv/mysql i zostanie on utworzony z odpowiednimi atrybutami.
Wszystko to zostało opisane poniżej.



#top Poinstalacyjna konfiguracja


#top keep oryginal start script


mv /etc/init.d/mysqld /etc/init.d/mysqld-rpm
/bin/cp -av /etc/init.d/mysqld-rpm /etc/init.d/mysqld



#top update start script


Run below command only on CentOS 5.* system
sed 's,/etc/sysconfig/network,/etc/sysconfig/network\n\n# Source mysql configuration.\n[ -f /etc/sysconfig/mysqld ] \&\& . /etc/sysconfig/mysqld,g' -i /etc/init.d/mysqld

below says that --defaults-file must be specified as first option in command line argument provided to mysqld daemon
130708 10:13:44 [ERROR] /usr/libexec/mysqld: unknown variable 'defaults-file=/etc/mysqld/my2.cnf'
When present, they must be specified ahead of any other mysqld options.
The following options may be given as the first argument:
--print-defaults        Print the program argument list and exit.
--no-defaults           Don't read default options from any option file.
--defaults-file=#       Only read default options from the given file #.
--defaults-extra-file=# Read this file after the global files are read."

/etc/init.d/mysqld
On CentOS 5.* system mysqld_safe is defined as static command
/usr/bin/mysqld_safe   ...

On CentOS 6.* system mysqld_safe is defined in variable $exec and is called by variable
$exec   ...

update init script for both os version mysqld
sed -e 's,/usr/bin/mysqld_safe   ,/usr/bin/mysqld_safe $MYSQLD_PREOPTS   ,g' -e 's,\$exec   ,$exec $MYSQLD_PREOPTS   ,g' -i /etc/init.d/mysqld



#top respect config file in start script


sed "s,/usr/bin/my_print_defaults ,/usr/bin/my_print_defaults --config-file=\$MYCNF ,g" -i /etc/init.d/mysqld
sed 's,/usr/bin/mysql_install_db,/usr/bin/mysql_install_db --defaults-file=$MYCNF,g' -i /etc/init.d/mysqld
sed 's,/usr/bin/mysqladmin,/usr/bin/mysqladmin --socket=$socket,g' -i /etc/init.d/mysqld
sed 's,^\(mypidfile=.*\)$,\1\nget_mysql_option mysqld socket "/var/run/mysqld/mysqld.sock"\nsocket="$result",g' -i /etc/init.d/mysqld
sed 's,^\(socket=.*\)$,\1\nget_mysql_option mysqld port "3306"\nport="$result",g' -i /etc/init.d/mysqld
sed 's,^\(socketfile.*\)$,\1\nget_mysql_option mysqld port 3306\nport="$result",g' -i /etc/init.d/mysqld
sed "s,\(--pid-file=[^ ]*\) ,\1 --log-error=\$errlogfile --port=\$port \$MYSQLD_OPTIONS ,g" -i /etc/init.d/mysqld



#top create /etc/sysconfig/mysqld file


cat > /etc/sysconfig/mysqld << EOF
# /etc/sysconfig/mysqld
# mysql config file
MYCNF=/etc/mysqld/my.cnf
# (oe) Remove --skip-networking to enable network access from
# non local clients. Access from localhost will still work.
#MYSQLD_OPTIONS="--nice=19 --skip-grant-tables" # --skip-networking
# When I try to remotely connect it would take quite a while to connect.
# I fixed this delay by adding the "skip-name-resolve" to my.ini
# [Warning] 'user' entry 'root@cen05' ignored in --skip-name-resolve mode.
MYSQLD_OPTIONS="" # "--master-connect-retry=10" "--skip-networking"
MYSQLD_PREOPTS="--defaults-file=$MYCNF"
# (oe) set TMPDIR and TMP environment variables
# datadir defined also in my.cnf
# but sometimes mysqldump dies because
# cannot write to files /.tmp dir
datadir=/srv/mysql
TMPDIR="\${datadir}/.tmp"
TMP="\${TMPDIR}"
EOF



#top create config directory


mkdir /etc/mysqld
mv /etc/my.cnf /etc/mysqld/my.cnf
ln -s /etc/mysqld/my.cnf /etc/my.cnf


#top create/copy data directory


rsync -rav /var/lib/mysql/ /srv/mysql/
# mkdir -p /srv/mysql
# chown mysql:mysql /srv/mysql


#top create log directory


/usr/libexec/mysqld: File '/var/log/mysqld/slow-queries.log' not found (Errcode: 2)
101113 15:51:25 [ERROR] Could not use /var/log/mysqld/slow-queries.log for logging (error 2).
Turning logging off for the whole duration of the MySQL server process.
To turn it on again: fix the cause, shutdown the MySQL server and restart it.

because error above we need create needed files
mkdir -p /var/log/mysqld
chown mysql:mysql /var/log/mysqld
touch /var/log/mysqld/slow-queries.log
chown mysql:mysql /var/log/mysqld/slow-queries.log





#top update config /etc/mysqld/my.cnf file


/etc/mysqld/my.cnf
#
# mysql config file
#
[client]
socket  = /var/run/mysqld/mysqld.sock
port    = 3306
default-character-set=utf8

[mysql]
# [client] # look like has the same result
# with one little difference
# mysql accept [client]:pager
# mysql* (mysqldump,mysqlshow) DOES NOT accept [client]:pager
#no-auto-rehash # faster start of mysql but no tab completition
pager   = less



[mysqld_safe]
log-error   = /var/log/mysqld/mysqld.log
pid-file    = /var/run/mysqld/mysqld.pid



[mysqld]
# The MySQL server
datadir      = /srv/mysql
socket       = /var/run/mysqld/mysqld.sock
user         = mysql
# (cen06) [Warning] '--default-character-set' is deprecated and will be removed in a future release.
# default-character-set=utf8
character-set-server=utf8
# [Warning] '--skip-locking' is deprecated and will be removed in a future release.
# Please use '--skip-external-locking' instead.
# skip-locking
skip-external-locking
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0


# bind to loopback interface
#bind-address = 127.0.0.1
# bind to all interfaces
bind-address = 0.0.0.0
port         = 3306


# Reduced to 25 as memory will not be enough for 100 connections.
# memory=key_buffer_size+(sort_buffer_size+read_buffer_size)*max_connections
# sort_buffer_size:max=64MB, read_buffer_size:max=64MB
# which is now: 12288 + (64 + 64) * 50 = 18688 MB
# max_connections = approx. MaxClients setting in httpd.conf file
# Default set to 100.
max_connections=25
# It is possible that mysqld could use up to:
# key_buffer_size+(read_buffer_size+sort_buffer_size)*max_connections = 10.250 GB
# bytes of memory
# Hope that's ok; if not, decrease some variables in the equation.


#
# * Logging and Replication
#
# Both location gets rotated by the cronjob.
# Be aware that this log type is a performance killer.
# log all queries sended to mysqld
# (cen06) [Warning] '--log' is deprecated and will be removed in a future release. Please use '--general_log'/'--general_log_file' instead.
# cen05: log, cen06: general_log_file
#general_log_file = /var/log/mysqld/queries.log
#log              = /var/log/mysqld/mysqld.log

# log slow queries is a must. Many queries that take more than 2 seconds.
# If so, then your tables need enhancement.
# (cen06) [Warning] '--log_slow_queries' is deprecated and will be removed in a future release.
# cen05: log_slow_queries, cen06: slow_query_log
log_slow_queries = /var/log/mysqld/slow-queries.log
slow_query_log = /var/log/mysqld/slow-queries.log
#long_query_time=2
#log-queries-not-using-indexes


# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
# Reference: http://dev.mysql.com/doc/mysql/en/Password_hashing.html
old_passwords=1


# increase mysql open file limit, to not often close and open table files
open-files-limit = 65536
table_cache      = 65536


# How many threads the server should cache for reuse. When a client disconnects,
# the client's threads are put in the cache if there are fewer than thread_cache_size threads there.
# Requests for threads are satisfied by reusing threads taken from the cache if possible,
# and only when the cache is empty is a new thread created.
thread_cache_size     =    8

# The maximum size of one packet or any generated/intermediate string.
# The packet message buffer is initialized to net_buffer_length bytes,
# but can grow up to max_allowed_packet bytes when needed.
# This value by default is small, to catch large (possibly incorrect) packets.
max_allowed_packet    =    1M

# Each session that needs to do a sort allocates a buffer of this size.
# sort_buffer_size is not specific to any storage engine and applies in a general manner for optimization.
sort_buffer_size      =    2M

# Each thread that does a sequential scan allocates a buffer of this size (in bytes) for each table it scans.
# If you do many sequential scans, you might want to increase this value, which defaults to 131072.
# The value of this variable should be a multiple of 4KB. If it is set to a value that is not a multiple of 4KB,
# its value will be rounded down to the nearest multiple of 4KB.
read_buffer_size      =    2M

# When reading rows in sorted order following a key-sorting operation,
# the rows are read through this buffer to avoid disk seeks.
# Setting the variable to a large value can improve ORDER BY performance by a lot.
# However, this is a buffer allocated for each client, so you should not set the global variable to a large value.
# Instead, change the session variable only from within those clients that need to run large queries.
read_rnd_buffer_size  =    8M

# Index blocks for MyISAM tables are buffered and are shared by all threads.
# key_buffer_size is the size of the buffer used for index blocks.
# The key buffer is also known as the key cache.
key_buffer_size       =   96M

# The amount of memory allocated for caching query results. The default value is 0, which disables the query cache.
# The permissible values are multiples of 1024; other values are rounded down to the nearest multiple.
# Note that query_cache_size bytes of memory are allocated even if query_cache_type is set to 0.
query_cache_size      =   64M

# MyISAM
# The size of the buffer that is allocated when sorting MyISAM indexes
# during a REPAIR TABLE or when creating indexes with CREATE INDEX or ALTER TABLE.
myisam_sort_buffer_size = 64M

# InnoDB
# If innodb_file_per_table is disabled (the default), InnoDB creates tables in the shared tablespace.
# If innodb_file_per_table is enabled, InnoDB creates each new table using its own .ibd file
# for storing data and indexes, rather than in the shared tablespace.
innodb_file_per_table = 1





#top starting mysql server


/etc/init.d/mysqld start
tail -f /var/log/mysqld/mysqld.log


#top post config/cleaning environment


ll /var/log/mysqld.log
rm -f /var/log/mysqld.log

mysql -u root -p mysql
DROP DATABASE test;
DROP USER ''@'%';
FLUSH PRIVILEGES;





#top create database nagios3 (for monitoring)


mysql -u root -p mysql
mysql -u root -p mysql << EOF
CREATE DATABASE nagios3 DEFAULT charset=utf8;
GRANT SELECT,INSERT,UPDATE,DELETE ON nagios3.* TO nagios3@10.5.5.5 IDENTIFIED BY 'nagios3';
FLUSH PRIVILEGES;
CREATE TABLE nagios3.nagios3_table (idkey int(11) NOT NULL auto_increment, name varchar(255), PRIMARY KEY (idkey)) DEFAULT charset=utf8;
EOF

mysql -u root -p mysql
mysql -u root -p mysql << EOF
CREATE DATABASE nagios3 DEFAULT charset=utf8;
GRANT SELECT,INSERT,UPDATE,DELETE ON nagios3.* TO nagios3@10.41.0.250 IDENTIFIED BY 'nagios3';
FLUSH PRIVILEGES;
CREATE TABLE nagios3.nagios3_table (idkey int(11) NOT NULL auto_increment, name varchar(255), PRIMARY KEY (idkey)) DEFAULT charset=utf8;
EOF





#top grant privileges replication slave (monitoring)


mysql -u root -p mysql
mysql -u root -p mysql << EOF
GRANT REPLICATION CLIENT,REPLICATION SLAVE ON *.* TO nagios3@10.5.5.5 IDENTIFIED BY 'nagios3';
FLUSH PRIVILEGES;
EOF

mysql -u root -p mysql
mysql -u root -p mysql << EOF
GRANT REPLICATION CLIENT,REPLICATION SLAVE ON *.* TO nagios3@10.41.0.250 IDENTIFIED BY 'nagios3';
FLUSH PRIVILEGES;
EOF





#top logrotate mysqld logs


logrorate for CentOS 5.* version system
/etc/logrotate.d/mysqld
/var/log/mysqld/mysqld.log
{
	sharedscripts
	postrotate
	mv /var/log/mysqld/mysqld.log.1 /var/log/mysqld/mysqld.log
	cat /var/log/mysqld/mysqld.log > /var/log/mysqld/mysqld.log.1
	chown --reference=/var/log/mysqld/mysqld.log /var/log/mysqld/mysqld.log.1
	echo -n > /var/log/mysqld/mysqld.log
	/bin/kill -HUP `cat /var/run/mysqld/mysqld.pid 2> /dev/null` 2> /dev/null || true
	endscript
}

logrorate for CentOS 6.* version system
/etc/logrotate.d/mysqld
/var/log/mysqld/mysqld.log
{
	sharedscripts
	postrotate
	create 0640 mysql mysql
	/bin/kill -HUP `cat /var/run/mysqld/mysqld.pid 2> /dev/null` 2> /dev/null || true
	endscript
}





#top multiple instances


mkdir -p /srv/mysql2
chown mysql:mysql /srv/mysql2

cp -av /etc/init.d/mysqld /etc/init.d/mysqld2

Run below command only on CentOS 5.* system
sed 's,/etc/sysconfig/mysqld,/etc/sysconfig/mysqld2,g' -i /etc/init.d/mysqld2

Run below command only on CentOS 6.* system
sed 's,^prog=.*$,prog="mysqld2",g' -i /etc/init.d/mysqld2

touch /var/log/mysqld/slow-queries2.log
chown mysql:mysql /var/log/mysqld/slow-queries2.log

cp -av /etc/sysconfig/mysqld /etc/sysconfig/mysqld2
sed -e 's,^MYCNF.*$,MYCNF=/etc/mysqld/my2.cnf,g' -e 's,^datadir.*$,datadir=/srv/mysql2,g' -i /etc/sysconfig/mysqld2

cp -av /etc/mysqld/my.cnf /etc/mysqld/my2.cnf

sed 's,^socket.*$,socket=/var/run/mysqld/mysqld2.sock,g' -i /etc/mysqld/my2.cnf
sed 's,^log-error.*$,log-error=/var/log/mysqld/mysqld2.log,g' -i /etc/mysqld/my2.cnf
sed 's,^pid-file.*$,pid-file=/var/run/mysqld/mysqld2.pid,g' -i /etc/mysqld/my2.cnf
sed 's,^datadir.*$,datadir=/srv/mysql2,g' -i /etc/mysqld/my2.cnf
sed 's,^port.*$,port=3307,g' -i /etc/mysqld/my2.cnf
sed 's,^log .*$,log=/var/log/mysqld/mysqld2.log,g' -i /etc/mysqld/my2.cnf
sed 's,^log_slow_queries.*$,log_slow_queries=/var/log/mysqld/slow-queries2.log,g' -i /etc/mysqld/my2.cnf

/etc/init.d/mysqld2 stop
/etc/init.d/mysqld2 start
tail -f /var/log/mysqld/mysqld2.log

touch /usr/bin/mysql2
chmod a+x /usr/bin/mysql2
echo -en '#!/bin/sh\n\n/usr/bin/mysql --defaults-file=/etc/mysqld/my2.cnf $*\n\n' > /usr/bin/mysql2
/usr/bin/mysql2 -u root -p mysql




Zmodyfikowany ostatnio: 2014/02/02 11:47:40 (10 lat temu), textsize: 15,4 kB, htmlsize: 30,0 kB

Zapraszam do komentowania, zgłaszania sugestii, propozycji, własnych przykładów, ...
Dodaj komentarzKomentarze użytkowników