一、下载tar包mysql
https://cdn.mysql.com/archives/mysql-5.6/mysql-5.6.27-linux-glibc2.5-x86_64.tar.gzlinux
二、解压sql
$tar -zxvf mysql-5.6.27-linux-glibc2.5-x86_64.tar.gz
三、将解压包放到mysql目录数据库
$mv mysql-5.6.27-linux-glibc2.5-x86_64 /usr/local/mysql
四、添加mysql用户组和mysql用户centos
$groupadd mysql $useradd -r -g mysql mysql
-g:指定用户所属的群组。值可使组名也能够是GID。用户组必须已经存在的,期默认值为100,即users安全
-r:创建系统帐号。服务器
五、配置my.cnfsocket
这里将mysql的data文件放到/data下,因此需新增文件夹:ui
/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
六、安装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 ./
七、启动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种:
方法一、设置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设置到交换分区中
方法二、修改/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
注:
一、若是内存不大(1G内)玩mysql5.6以上版本,按默认配置的话机器会很吃力,因此你们若是只是本身玩玩或实际对数据库不会有太多压力的话,能够减少相应配置来下降mysql对内存的占用,知足实际需求才是王道,毕竟杀鸡用牛刀也仍是蛮心疼的。
二、也能够将mysql下降到5.5之内,或关闭innodb,但这样作的话,不是很明智的选择。还有一个不差钱的好选择就是专门再买个mysql服务器!
-------------------------------------------------
-------------------------------------------------
一、设置mysql软链接
$ln -s /usr/local/mysql/bin/mysql /usr/bin
二、密码设置,默认状况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;
好了,这时候你就能够对它随心所欲了!