mysql 数据库的默认安装的路径在/var/lib/mysql,但是实际中,我们在安装时常常就需要定制安装数据库路径,比如/data/mysql,或者/opt/mysql,通常是一个单独的一块盘或者分区,这样有利于性能调优和保护数据安全,同时也方便进行维护。
重新初始化mysql 的datadir方法是官方提供的perl脚本mysql_install_db。
perocna是一个优秀的mysql衍生版,以高性能为特色,这里以Percona 最新版5.6.12作为示范,迁移目标是/data/mysql
1、确保关闭了selinux,减少排障干扰。
事实证明,selinux是一大败笔,其规则不易维护,还常导致莫名其妙的问题。
2、安装percona
rpm -Uhv http://www.percona.com/downloads/percona-release/percona-release-0.0-1.x86_64.rpm
yum install Percona-Server-server-56 --enablerepo=percona
3、创建目标目录
mkdir -p /data/mysql
chown -R mysql:mysql /data/mysql
4、迁移命令
mysql_install_db --user=mysql --basedir=/usr --datadir=/data/mysql
5、为避免夜长梦多,干掉/var/lib/mysql
rm -rf /var/lib/mysql
6、创建合适的/etc/my.cnf,一定要指定datadir,其他具体配置没有定论,不过mysql5.6 默认的配置比较OK,推荐通过percona的工具在线配置一个https://tools.percona.com/
这里暂且贴一份,机器配置是6核,16G内存
[mysql]
# CLIENT #
port = 3306
socket = /data/mysql/mysql.sock
default-character-set = utf8
[mysqld]
# GENERAL #
user = mysql
default_storage_engine = InnoDB
socket = /data/mysql/mysql.sock
pid-file = /data/mysql/mysql.pid
character-set-server = utf8
# MyISAM #
key_buffer_size = 32M
myisam_recover = FORCE,BACKUP
# SAFETY #
max_allowed_packet = 16M
max_connect_errors = 1000000
skip_name_resolve
# DATA STORAGE #
datadir = /data/mysql/
# BINARY LOGGING #
log_bin = /data/mysql/mysql-bin
expire_logs_days = 14
sync_binlog = 1
# CACHES AND LIMITS #
tmp_table_size = 32M
max_heap_table_size = 32M
query_cache_type = 0
query_cache_size = 0
max_connections = 500
thread_cache_size = 50
open_files_limit = 65535
table_definition_cache = 1024
table_open_cache = 2048
# INNODB #
innodb_flush_method = O_DIRECT
innodb_log_files_in_group = 2
innodb_log_file_size = 256M
innodb_flush_log_at_trx_commit = 1
innodb_file_per_table = 1
innodb_buffer_pool_size = 10G
innodb_thread_concurrency = 12
thread_handling = pool-of-threads
#auto_increment_increment = 2
#auto_increment_offset = 0
# LOGGING #
log_error = /data/mysql/mysql-error.log
log_queries_not_using_indexes = 1
slow_query_log = 1
slow_query_log_file = /data/mysql/mysql-slow.log
7、启动mysql
12 chkconfig mysql on
service mysql start
至于报错,原因肯定有不止一百个,不解释了。按照上面的步骤执行就可以了,收集的常见报错如下,仅供参考:
A版,mysql启动报错
Starting MySQL...The server quit without updating PID file ,xxx.pid
B版,mysql_install_db 脚本运行出错
FATAL ERROR: Could not find my-default cnf, could not find my-default cnf, fata error my-default cnf, fatal error could not find my-default cnf, mysql_install_db my-default cnf error
另外,还有一个偷懒的办法,先创建/data/mysql,然后软链接到/var/lib/mysql,再安装mysql-server,这样反而可以规避一些常见错误。