一、下載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;
好了,這時候你就能夠對它隨心所欲了!