Linux MySQL安装

相比之前的Oracle,MySQL就简单多了。

准备:

创建用户:

groupadd mysql
useradd -g mysql mysql

创建MySQL目录:

#主目录
mkdir /usr/local/mysql

#数据目录
mkdir /usr/local/mysql/data

下载安装包:

https://dev.mysql.com/downloads/mysql/5.6.html#downloads

选择如下图,选择Linux通用版本:

MySQL下载

下载解压到/usr/local/mysql目录下,MySQL安装目录/usr/local/mysql/mysql-5.6.40-linux-glibc2.12-x86_64

然后授权:

chown -R mysql:mysql /usr/local/mysql/

安装:

安装命令:

#安装数据库(初始化数据库)
./scripts/mysql_install_db --user=mysql --datadir=/usr/local/mysql/data

#添加MySQL服务
cp support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
chkconfig mysqld on
service mysqld start

#覆盖配置
cp support-files/my-default.cnf /etc/my.cnf

注:一定要到MySQL解压目录下,否者会提示:FATAL ERROR: Could not find ./bin/my_print_defaults
5.7以后版本的安装命令:

./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql/mysql-5.7.x --datadir=/usr/local/mysql/data

修改配置my.cnf

[client]
default-character-set=utf8mb4

[mysql]
default-character-set=utf8mb4

[mysqld]
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
basedir=/usr/local/mysql/mysql-5.6.40-linux-glibc2.12-x86_64
datadir=/usr/local/mysql/data

上面设置了编码和数据库目录配置。
如果你可以根据需要优化其他参数。

启动数据库服务:

service mysqld start

这个时候我们可以进入${basedir}/bin里面登陆MySQL:

./mysql -u root

#查询状态
status

显示数据库信息:

--------------
./mysql  Ver 14.14 Distrib 5.6.40, for linux-glibc2.12 (x86_64) using  EditLine wrapper

Connection id:		2
Current database:	
Current user:		root@localhost
SSL:			Not in use
Current pager:		stdout
Using outfile:		''
Using delimiter:	;
Server version:		5.6.40 MySQL Community Server (GPL)
Protocol version:	10
Connection:		Localhost via UNIX socket
Server characterset:	utf8mb4
Db     characterset:	utf8mb4
Client characterset:	utf8mb4
Conn.  characterset:	utf8mb4
UNIX socket:		/tmp/mysql.sock
Uptime:			38 sec

Threads: 1  Questions: 7  Slow queries: 0  Opens: 67  Flush tables: 1  Open tables: 60  Queries per second avg: 0.184
--------------

安装完成了,现在的root用户是没有密码的,但是其他IP登陆不了,后面需要分配权限,这里不操作了。
不过这里要说一点,修改权限要执行:FLUSH PRIVILEGES进行刷新。

注:如果是5.7以后的版本,需要使用密码,密码会在安装命令执行后输出在控制台。

如果为了方便你还可以设置一下环境变量,这里也不操作了。

如果提示:

FATAL ERROR: please install the following Perl modules before executing mysql/mysql-5.6.40-linux-glibc2.12-x86_64/scripts/mysql_install_db:
Data::Dumper

那么需要安装依赖:yum install autoconf

MySQL5.7

5.7以后my.cnf不需要拷贝,直接会生成一个默认的到/etc目录下面。
并且里面多了几个默认配置:

[mysqld]
basedir=/usr/local/mysql/mysql-5.7.21-linux-glibc2.12-x86_64
socket=/usr/local/mysql/mysql.sock
[mysqld_safe]
log-error=/usr/local/mysql/mariadb/mariadb.log
pid-file=/usr/local/mysql/mariadb/mariadb.pid

上面的四个配置时默认存在,我已经把路径修改过了,不过建议不要修改socket这个配置。
其中[mysqld_safe]下的两个配置的文件需要手动创建并赋予权限,否者提示:

Starting MySQL.2018-06-28T08:36:23.588675Z mysqld_safe error: log-error set to '/usr/local/mysql/mariadb/mariadb.log', however file don't exists. Create writable for user 'mysql'.
The server quit without updating PID file (/usr/local/mysql[FAILED]ishao.pid).

如果提示:

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

这个是因为修改了默认的socket导致的,添加如下配置:

[client]
socket=/usr/local/mysql/mysql.sock