MySQL基于二进制格式安装的优点在于,它是针对特定平台专门优化过的,安装的时候也不需要考虑环境的要求,直接解压就可以,本文基于MySQL5.6.38版本,下载地址为:
https://cdn.mysql.com//Downloads/MySQL-5.6/mysql-5.6.38-linux-glibc2.12-x86_64.tar.gz
1,下载MySQL二进制包:
[root@localhost ~]# mkdir /root/soft/ -pv
[root@localhost ~]# cd soft/
[root@localhost soft]# wget https://cdn.mysql.com//Downloads/MySQL-5.6/mysql-5.6.38-linux-glibc2.12-x86_64.tar.gz
2,解压到/usr/local下:
[root@localhost soft]# tar xf mysql-5.6.38-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
3,创建软链接:
[root@localhost soft]# cd /usr/local/
[root@localhost local]# ln -sv mysql-5.6.38-linux-glibc2.12-x86_64/ mysql
4,创建mysql数据目录:
[root@localhost ~]# mkdir /data/mydata -pv
4,创建mysql用户,mysql组,授权数据目录:
[root@localhost ~]# groupadd -r mysql
[root@localhost ~]# useradd -r -g mysql -s /sbin/nologin -M -d /data/mydata mysql
[root@localhost ~]# chown -R mysql:mysql /data/mydata/
5,初始化数据库:
[root@localhost ~]# cd /usr/local/mysql
[root@localhost mysql]# chown -R mysql.mysql .
[root@localhost mysql]# ./scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/data/mydata/ --user=mysql
FATAL ERROR: please install the following Perl modules before executing ./scripts/mysql_install_db:
Data::Dumper
这里出现一个报错提示我有Perl模块没有装,于是我照常安装了perl-devel,perl等软件包,再初始化数据发现还是报错,搜索了下原来是perl-Data-Dumper.x86_64 0:2.145-3.el7这个包,报错的信息提示的很明显,好吧,下次一定好好看报错信息。解决方法也很简单,直接yum install即可。
[root@localhost mysql]# yum list perl-Data-Dumper
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.cn99.com
* extras: mirrors.cn99.com
* updates: centos.ustc.edu.cn
Available Packages
perl-Data-Dumper.x86_64
[root@localhost mysql]# yum install perl-Data-Dumper -y
再次执行:
[root@localhost mysql]# ./scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/data/mydata/ --user=mysql
Installing MySQL system tables.../usr/local/mysql/bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
又一个报错出现,这个提示表明libaio这个模块不存在,好,缺啥咱就装啥。
[root@localhost mysql]# yum install libaio -y
然后再执行mysql_install_db脚本初始化数据库,这次没有报错,当看到OK字样时,表示数据库初始化完成。
6,设置mysql数据库配置文件。这里要注意mysql5.6解压目录下的 support-files目录中只有my-default.cnf这样一个默认配置文件,而且里面基本没有配置,多为注释,这和mysql5.5的区别比较大,5.5的该目录下会有 my-huge.cnf my-innodb-heavy-4G.cnf my-large.cnf my-medium.cnf my-small.cnf 这样的配置文件,可以根据自身服务器的情况自行选择。既然如此,就只能自己配置my.cnf了。
[root@localhost ~]# vi /etc/my.cnf
[client]
#password = your_password
port = 3306
socket = /tmp/mysql.sock
[mysqld]
port = 3306
socket = /tmp/mysql.sock
#datadir = /usr/local/mysql/var
datadir = /data/mydata/
basedir = /usr/local/mysql/
pid-file = /data/mydata/mysql.pid
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
thread_cache_size = 8
query_cache_size = 8M
tmp_table_size = 16M
#skip-networking
max_connections = 500
max_connect_errors = 100
open_files_limit = 65535
log-bin=mysql-bin
binlog_format=mixed
server-id = 1
expire_logs_days = 10
default_storage_engine = InnoDB
innodb_file_per_table = 1
innodb_data_home_dir = /data/mydata/
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = /data/mydata/
innodb_buffer_pool_size = 16M
innodb_additional_mem_pool_size = 2M
innodb_log_file_size = 5M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
7,导出PATH环境变量:
[root@localhost ~]# echo "export PATH=$PATH:/usr/local/mysql/bin" > /etc/profile.d/mysql.sh
[root@localhost ~]# . /etc/profile.d/mysql.sh
8,导出mysql头文件到系统头文件目录:
[root@localhost ~]# ln -sv /usr/local/mysql/include /usr/include/mysql
9,输出mysql库文件到系统库文件的路径:
[root@localhost ~]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@localhost ~]# ldconfig -v
10,启动mysqld服务:
[root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@localhost mysql]# chkconfig mysqld on
[root@localhost mysql]# /etc/init.d/mysqld start
这里要注意一下,/etc/init.d/mysqld这个脚本中有如下几个地方要修改:
basedir=/usr/local/mysql
datadir=/data/mydata/
mysqld_pid_file_path=/data/mydata/mysql.pid
这时候mysql就已经启动了:
[root@localhost mysql]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.38-log MySQL Community Server (GPL)
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
mysql> use mysql
Database changed
mysql> delete from mysql.user where (user,host) not in (select 'root', 'localhost'); #删除多余的用户
mysql> update user set password=password('QWER@@111') where user='root'; #修改root密码
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select user,host,password from user;
+------+-----------+-------------------------------------------+
| user | host | password |
+------+-----------+-------------------------------------------+
| root | localhost | *16FF293CF039130E27DC275D8B47EBB0754FFE9F |
+------+-----------+-------------------------------------------+
1 row in set (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
至此,MySQL数据库安装完成。