1、下载tar包
https://cdn.mysql.com/archives/mysql-5.6/mysql-5.6.27-linux-glibc2.5-x86_64.tar.gz
2、解压
$tar -zxvf mysql-5.6.27-linux-glibc2.5-x86_64.tar.gz
3、将解压包放到mysql目录
$mv mysql-5.6.27-linux-glibc2.5-x86_64 /usr/local/mysql
4、添加mysql用户组和mysql用户
$groupadd mysql
$useradd -r -g mysql mysql
-g:指定用户所属的群组。值可以使组名也可以是GID。用户组必须已经存在的,期默认值为100,即users
-r:建立系统账号。
5、配置my.cnf
这里将mysql的data文件放到/data下,所以需新增文件夹:
/data/mysql/data 、/data/mysql/binlog/ 、/data/mysql/ibdata 、/data/mysql/log
对应的配置文件如下:
[client]
port = 3306
socket = /data/mysql/mysql.sock
[mysqld]
port = 3306
socket = /data/mysql/mysql.sock
basedir = /usr/local/mysql
datadir = /data/mysql/data
character_set_server = utf8
pid-file = /data/mysql/mysql.pid
# SAFETY #
back_log = 300
skip-name-resolve
max_connections = 5000
max_user_connections = 4000
max_connect_errors = 999999999999999
wait_timeout = 30
interactive_timeout = 30
table_open_cache = 5000
external-locking = false
# CACHES AND LIMITS #
max_allowed_packet = 32M
binlog_cache_size = 1M
max_heap_table_size = 8M
read_buffer_size = 4M
read_rnd_buffer_size = 4M
sort_buffer_size = 4M
join_buffer_size = 8M
thread_cache_size = 300
thread_concurrency = 4
query_cache_type = 0
query_cache_size = 0
query_cache_min_res_unit = 4K
query_cache_limit = 1M
ft_min_word_len = 4
default-storage-engine = innodb
thread_stack = 512K
transaction_isolation = REPEATABLE-READ
tmp_table_size = 8M
server-id = 247
#replicate-ignore-db = mysql
replicate-wild-ignore-table = mysql.%
#slave-skip-errors=all
slave-skip-errors=1054,1062
symbolic-links = 0
# BINARY LOGGING #
log-bin=/data/mysql/binlog/mysql-bin
relay_log=/data/mysql/binlog/mysql-relay-bin
relay_log_index=/data/mysql/binlog/mysql-relay-bin.index
relay_log_info_file=/data/mysql/binlog/relay-log.info
binlog_format=mixed
expire_logs_days = 1
# LOGGING #
slow_query_log
long_query_time = 2
key_buffer_size = 8M
bulk_insert_buffer_size = 8M
# MyISAM #
myisam_sort_buffer_size = 8M
myisam_max_sort_file_size = 8M
myisam_repair_threads = 1
# INNODB #
innodb_buffer_pool_size = 128M
innodb_file_per_table = 1
innodb_data_file_path = ibdata1:128M;ibdata2:10M:autoextend
innodb_flush_method = O_DIRECT
innodb_data_home_dir = /data/mysql/ibdata
innodb_write_io_threads = 2
innodb_read_io_threads = 2
innodb_thread_concurrency = 4
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 16M
innodb_log_file_size = 256M
innodb_log_files_in_group = 3
innodb_log_group_home_dir = /data/mysql/log
innodb_io_capacity = 2000
innodb_adaptive_flushing=1
innodb_max_dirty_pages_pct = 40
innodb_flush_method=O_DIRECT
innodb_lock_wait_timeout = 30
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
read_buffer = 8M
write_buffer = 8M
[mysqlhotcopy]
interactive-timeout
[mysqld_safe]
open-files-limit = 40960
6、安装mysql数据库
$cd /usr/local/mysql
#修改当前目录拥有者为mysql
$chown -R mysql:mysql ./
#执行安装数据库命令
$/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/data/mysql/data/ --user=mysql --defaults-file=/etc/my.cnf
#重新修改目前拥有者为root
$chown -R root:root ./
7、启动mysql并添加开机启动mysql服务
#开机初始化
$cp support-files/mysql.server /etc/init.d/mysql
#启动mysql
$service mysql start
Starting MySQL. SUCCESS!
注:如果启动时报错:Starting MySQL.. ERROR! The server quit without updating PID file
$vi /data/mysql/data/VM_0_17_centos.err 查看错误信息,发现错误信息:
130728 6:50:14 InnoDB: Initializing buffer pool, size = 128.0M
InnoDB: mmap(137363456 bytes) failed; errno 12
130728 6:50:14 InnoDB: Completed initialization of buffer pool
130728 6:50:14 InnoDB: Fatal error: cannot allocate memory for the buffer pool
那就是你服务器内存不足导致(我的服务器才1G内存,上面配置文件innodb_buffer_pool_size = 128M,free了下发现mysql本身吃了700M内存,可使用内存才70M左右),解决方案有2种:
方法1、设置swap分区,即所谓的虚拟内存
$dd if=/dev/zero of=/swapfile bs=1M count=1024 #添加一个交换文件,还有一种时添加交换分区
$mkswap /swapfile #设置交换文件
$swapon /swapfile #启用交换文件
$vi /etc/fstab
写入/swapfile swap swap defaults 0 0 #写入/etc/fstab,以便在引导时启用
#swapoff #删除交换分区,并从 /etc/fstab 中删除项目
#free下查看是否添加ok
$free
注:如果你有多个分区,可以将swap设置到交换分区中
方法2、修改/etc/my.cnf配置,减小配置,以减少内存占用
主要改下以下几个配置值:
max_connections = 200
table_open_cache = 1000
table_definition_cache = 700
performance_schema_max_table_instances = 500
最后配置文件如下:
[client]
port = 3306
socket = /data/mysql/mysql.sock
[mysqld]
port = 3306
socket = /data/mysql/mysql.sock
basedir = /usr/local/mysql
datadir = /data/mysql/data
character_set_server = utf8
pid-file = /data/mysql/mysql.pid
# SAFETY #
back_log = 100
skip-name-resolve
max_connections = 200
max_user_connections = 4000
max_connect_errors = 999999999999999
wait_timeout = 30
interactive_timeout = 30
table_open_cache = 1000
external-locking = false
table_definition_cache = 700
performance_schema_max_table_instances = 500
# CACHES AND LIMITS #
max_allowed_packet = 32M
binlog_cache_size = 1M
max_heap_table_size = 4M
read_buffer_size = 2M
read_rnd_buffer_size = 2M
sort_buffer_size = 2M
join_buffer_size = 4M
thread_cache_size = 150
thread_concurrency = 2
query_cache_type = 0
query_cache_size = 0
query_cache_min_res_unit = 4K
query_cache_limit = 1M
ft_min_word_len = 4
default-storage-engine = innodb
thread_stack = 512K
transaction_isolation = REPEATABLE-READ
tmp_table_size = 8M
server-id = 247
#replicate-ignore-db = mysql
replicate-wild-ignore-table = mysql.%
#slave-skip-errors=all
slave-skip-errors=1054,1062
symbolic-links = 0
# BINARY LOGGING #
log-bin=/data/mysql/binlog/mysql-bin
relay_log=/data/mysql/binlog/mysql-relay-bin
relay_log_index=/data/mysql/binlog/mysql-relay-bin.index
relay_log_info_file=/data/mysql/binlog/relay-log.info
binlog_format=mixed
expire_logs_days = 1
# LOGGING #
slow_query_log
long_query_time = 2
key_buffer_size = 8M
bulk_insert_buffer_size = 8M
# MyISAM #
myisam_sort_buffer_size = 8M
myisam_max_sort_file_size = 8M
myisam_repair_threads = 1
# INNODB #
innodb_buffer_pool_size = 64M
innodb_file_per_table = 1
innodb_data_file_path = ibdata1:128M;ibdata2:10M:autoextend
innodb_flush_method = O_DIRECT
innodb_data_home_dir = /data/mysql/ibdata
innodb_write_io_threads = 2
innodb_read_io_threads = 2
innodb_thread_concurrency = 4
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 16M
innodb_log_file_size = 256M
innodb_log_files_in_group = 3
innodb_log_group_home_dir = /data/mysql/log
innodb_io_capacity = 2000
innodb_adaptive_flushing=1
innodb_max_dirty_pages_pct = 40
innodb_flush_method=O_DIRECT
innodb_lock_wait_timeout = 30
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
read_buffer = 8M
write_buffer = 8M
[mysqlhotcopy]
interactive-timeout
[mysqld_safe]
open-files-limit = 40960
注:
1、如果内存不大(1G内)玩mysql5.6以上版本,按默认配置的话机器会很吃力,所以大家如果只是自己玩玩或实际对数据库不会有太多压力的话,可以减小相应配置来降低mysql对内存的占用,满足实际需求才是王道,毕竟杀鸡用牛刀也还是蛮心疼的。
2、也可以将mysql降低到5.5以内,或关闭innodb,但这样做的话,不是很明智的选择。还有一个不差钱的好选择就是专门再买个mysql服务器!
-------------------------------------------------
-------------------------------------------------
- 数据库设置
1、设置mysql软连接
$ln -s /usr/local/mysql/bin/mysql /usr/bin
2、密码设置,默认情况root密码为空
$./bin/mysqladmin -u root password 'root' #/usr/local/mysql/
为了安全mysql会限制在命令行修改,可登陆到mysql命令行模式下修改:
mysql> use mysql;
mysql> UPDATE user SET password=password("root") WHERE user='root';
mysql> flush privileges;
mysql> exit;
好了,这时候你就可以对它为所欲为了!