MySQL数据库二进制软件包的安装、启动和关闭(centOS 6.9 mysql-5.6.16)

    技术2026-03-11  6

    安装环境(X64): centOS 6.9 mysql-5.6.16-linux-glibc2.5-x86_64.tar.gz

    官网MySQL有四个版本(GA版 DMR版 RC版 Beta版) 一般情况下,生产环境或者测试环境要选择GA版(常规可用的版本经过bug修复测试过)

    一.下载MySQL二进制软件包

    下载网址:https://dev.mysql.com/downloads/mysql/ 示例: 对下载下来的软件包进行MD5校验,保证其完整性

    [root@node03 local]# md5sum mysql-5.6.16-linux-glibc2.5-x86_64.tar.gz 41b4533f4fcec2d0132794f26f378f2a mysql-5.6.16-linux-glibc2.5-x86_64.tar.gz​

    二.安装前系统环境检测

    1.关闭SELinux和系统防火墙

    关闭SELinux

    [root@node03 local]# cat /etc/selinux/config 7 SELINUX=disabled

    关闭防火墙

    [root@node03 local]# chkconfig --list | grep iptables iptables 0:off 1:off 2:on 3:on 4:on 5:on 6:off [root@node03 local]# chkconfig iptables off [root@node03 local]# chkconfig --list | grep iptables iptables 0:off 1:off 2:off 3:off 4:off 5:off 6:off [root@node03 local]# 

    2.I/O调度调系统默认是cfq模式,这里强烈建议使用deadline模式

    查看I/O调度文件:

    [root@node01 ~]# cat /sys/block/sda/queue/scheduler noop anticipatory deadline [cfq]

    修改I/O调度器,需要在/etc/grub.conf中加入elevator=deadline,保证永久生效。

    [root@node01 ~]# cat /etc/grub.conf # grub.conf generated by anaconda # # Note that you do not have to rerun grub after making changes to this file # NOTICE: You have a /boot partition. This means that # all kernel and initrd paths are relative to /boot/, eg. # root (hd0,0) # kernel /vmlinuz-version ro root=/dev/sda3 # initrd /initrd-[generic-]version.img #boot=/dev/sda default=0 timeout=5 elevator=deadline

    3.swap分区设置

    swappiness值的大小对如何使用swap分区有着很大的影响。

    0代表最大限度地使用物理内存,然后才使用swap分区,这种行为有可能导致系统内存溢出。出现OOM的错误,从而导致MySQL被意外kill掉,所以需要谨慎设置。100则是积极地使用swap分区,并且把内存上面的数据及时搬到swap分区里(不建议)

    这里建议大家不分配swap,或者分配4GB的空间就足够了。

    查看swappiness文件

    [root@node03 ~]# cat /proc/sys/vm/swappiness 60 [root@node03 ~]# sysctl -a | grep swap vm.swappiness = 60 [root@node03 ~]#

    修改swappiness的值,编辑/etc/sysctl.conf文件,加入vm.swappiness的值即可。

    4.文件系统选择

    这里建议大家使用xfs文件系统,相比ext4,它更方便管理,支持动态扩容,删除文件也很方便。

    5.操作系统的限制

    先来查看一些当前操作系统的限制情况。 使用ulimit -a查看:

    [root@node03 ~]# ulimit -a core file size (blocks, -c) 0 data seg size (kbytes, -d) unlimited scheduling priority (-e) 0 file size (blocks, -f) unlimited pending signals (-i) 14750 max locked memory (kbytes, -l) 64 max memory size (kbytes, -m) unlimited open files (-n) 1024 pipe size (512 bytes, -p) 8 POSIX message queues (bytes, -q) 819200 real-time priority (-r) 0 stack size (kbytes, -s) 10240 cpu time (seconds, -t) unlimited max user processes (-u) 14750 virtual memory (kbytes, -v) unlimited file locks (-x) unlimited

    注意两个最为重要的参数:

    open files如果设置不合理,而当前服务器连接过多或者表过多时,就有可能出现打不开表或者访问不了表的现象。默认情况下,Linux的最大句柄数为1024个,表示单个进程最多可以访问1024个文件句柄,如果要超出默认值,就会出现文件句柄超限的错误“too many open files”。

    max user process参数的用途是,有时候我们可能会跑很多实例,但是发现创建不了新的连接,报出“resource temporarily unavailable”的错误,表示没有足够的资源。

    为了防止以上两种报错情况,我们可以修改系统的软硬限制。编辑/etc/securit/limits.conf,加入限制的相关内容。记得更改完内容之后,需要重启操作系统才能生效。

    [root@node03 ~]# tail /etc/security/limits.conf #@faculty hard nproc 50 #ftp hard nproc 0 #@student - maxlogins 4 * soft nproc 65535 * hard nproc 65535 * soft nofile 65535 * hard nofile 65535 # End of file [root@node03 ~]#​

    6.关闭numa

    简单来讲关闭numa功能,可以更好地分配内存,不需要采用swap的方式来获取内存。因为有经验的系统管理员和DBA都知道使用swap导致的数据库性能下降有多么的恶心。关闭方式也分在BIOS、操作系统中关闭,或者是在数据库启动过程中关闭。

    三.MySQL5.6版本的安装

    1.创建MySQL用户,指定MySQL用户组

    [root@node03 ~]# groupadd mysql [root@node03 ~]# useradd -g mysql mysql -s /sbin/nologin

    软件包的家目录(basedir)统一规范放在/usr/local/下面:

    [root@node03 ~]# cd /usr/local/ [root@node03 local]# ls bin games lib libexec sbin src etc include lib64 mysql-5.6.16-linux-glibc2.5-x86_64.tar.gz share [root@node03 local]# ​

    2.解压MySQL软件包,做软链,授权

    解压

    [root@node03 local]# tar -zxvf mysql-5.6.16-linux-glibc2.5-x86_64.tar.gz​

    做软链,方便日后升级

    [root@node03 local]# ln -s mysql-5.6.16-linux-glibc2.5-x86_64 mysql​

    给MySQL目录授权

    [root@node03 local]# chown mysql:mysql -R mysql​

    3.创建MySQL数据库的数据目录,授权

    创建MySQL数据库的数据目录(datadir),这里可以选择创建在/data/mysql下面,命令如下:

    [root@node03 ~]# mkdir -p /data/mysql [root@node03 ~]# chown mysql:mysql -R /data/mysql/

    4.编辑配置文件

    由于是二进制的安装方式,这里的数据配置文件需要自己配置好。

    vi/etc/my.cnf [client] port = 3306 socket = /tmp/mysql.sock default-character-set = utf8 [mysql] default-character-set = utf8 [mysqld] port = 3306 socket = /tmp/mysql.sock basedir = /usr/local/mysql datadir = /data/mysql open_files_limit = 65535 back_log = 103 max_connections = 512 max_connect_errors = 100000 table_open_cache = 512 external-locking = FALSE max_allowed_packet = 128M sort_buffer_size = 2M join_buffer_size = 2M thread_cache_size = 51 query_cache_size = 32M tmp_table_size = 96M max_heap_table_size = 96M slow_query_log = 1 slow_query_log_file = /data/mysql/slow.log log-error = /data/mysql/error.log long_query_time = 0.5 server-id = 1323306 log-bin = /data/mysql/mysql-bin sync_binlog = 1 binlog_cache_size = 4M max_binlog_cache_size = 128M max_binlog_size = 1024M expire_logs_days = 7 key_buffer_size = 32M read_buffer_size = 1M read_rnd_buffer_size = 16M bulk_insert_buffer_size = 64M character-set-server = utf8 default-storage-engine = InnoDB binlog_format = row #gtid_mode = on #log_slave_updates= 1 #enforce-gtid-consistency = 1 interactive_timeout = 300 wait_timeout = 300 transaction_isolation = REPEATABLE-READ innodb_buffer_pool_size = 1434M innodb_data_file_path = ibdata1:1024M:autoextend innodb_flush_log_at_trx_commit = 1 innodb_log_buffer_size = 16M innodb_log_file_size = 256M innodb_log_files_in_group = 2 innodb_max_dirty_pages_pct = 50 innodb_file_per_table = 1 innodb_locks_unsafe_for_binlog = 0 [mysqldump] quick max_allowed_packet = 32M​

    5.初始化数据库

    [root@node01 scripts]# ./mysql_install_db --basedir=/usr/local/mysql --datadir=/data/mysql --defaults-file=/etc/my.cnf --user=mysql -bash: ./mysql_install_db: /usr/bin/perl: bad interpreter: No such file or directory​

    解决上述报错:

    -bash: ./scripts/mysql_install_db: /usr/bin/perl: bad interpreter: No such file or directory 貌似提示注释器错误,没有/usr/bin/perl文件或者档案,解决办法(安装perl跟perl-devel即可): 执行 yum -y install perl perl-devel 后在初始化数据库即可。 bin/mysql_install_db FATAL ERROR: please install the following Perl modules before executing /usr/bin/mysql_install_db: Data::Dumper yum install -y perl-Data-Dumper 即可

    当出现有两个“OK”的时候,证明初始化数据库成功了

    [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). OK Filling help tables...2020-07-05 02:41:31 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). OK​

    6.启动数据库

    启动MySQL:

    [root@node03 ~]# cd /usr/local/mysql/bin/ [root@node03 bin]# ./mysqld_safe --defaults-file=/etc/my.cnf &

    MySQL的启动过程:

    [root@node03 mysql]# /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf & [1] 4592 [root@node03 mysql]# 200705 04:12:13 mysqld_safe Logging to '/data/mysql/error.log'. 200705 04:12:13 mysqld_safe Starting mysqld daemon with databases from /data/mysql​

    查看MySQL进程,验证是否启动成功:

    [root@node03 mysql]# ps -ef|grep mysql root 4592 1264 0 04:12 pts/1 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf mysql 5233 4592 0 04:12 pts/1 00:00:00 /usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/error.log --open-files-limit=65535 --pid-file=/data/mysql/node01.pid --socket=/tmp/mysql.sock --port=3306 root 5258 1264 0 04:13 pts/1 00:00:00 grep mysql

    7.登录数据库并创建密码

    安装完MySQL之后,进入数据库的方式是无密码进入的,为了保证数据库的安全性,我们需要为数据库root用户创建密码。

    命令如下:

    [root@node03 ~]# cd /usr/local/mysql/bin/ [root@node03 bin]# ./mysql​ mysql> use mysql; mysql> update user set password=password('123456') where user='root'; mysql> flush privileges;

    8.关闭MySQL数据库

    正常关闭方式

    [root@node03 bin]# cd /usr/local/mysql/bin/ [root@node03 bin]# ./mysqladmin -uroot -p123456 shutdown

    非正常关闭就需要kill掉MySQL的进程了。

    Processed: 0.017, SQL: 9