当前位置: 移动技术网 > IT编程>开发语言>Java > CentOS 7下MySQL 5.7安装

CentOS 7下MySQL 5.7安装

2019年11月08日  | 移动技术网IT编程  | 我要评论

5.7和之前版本的mysql有一些不同,现把centos 7下mysql 5.7安装、配置与应用完整过程记下来,或许对新手来说有用。

本文描述的安装是采用通用的二进制压缩包(linux – generic)以解压方式安装,相当于绿色安装了。

一、下载通用安装二进制包

先下载mysql安装包:打开 http://dev.mysql.com/downloads/mysql/
选择 linux – generic并在其下选择
linux – generic (glibc 2.5) (x86, 64-bit), compressed tar archive
进行下载。可以先下载到一个临时目录里,解压后,得到两个包:
mysql-5.7.11-linux-glibc2.5-x86_64.tar.gz
mysql-test-5.7.11-linux-glibc2.5-x86_64.tar.gz
只需要mysql-5.7.11-linux-glibc2.5-x86_64.tar.gz 这个包就行了。

二、建立用户和目录

建立用户mysql,组mysql。后面mysql就使用这个用户来运行(注意这也是mysql启动脚本中默认的用户,因此最好不要改名)。

groupadd mysql

useradd -r -g mysql mysql

(使用-r参数表示mysql用户是一个系统用户,不能登录)

建立目录/work/program,后面mysql就安装在这个目录下面。

mkdir /work/program

三、安装

【解压】
将前面得到的mysql-5.7.11-linux-glibc2.5-x86_64.tar.gz解压至/work/program目录下

tar zxvf mysql-5.7.11-linux-glibc2.5-x86_64.tar.gz -c /work/program

这时在program下得到的目录名很长,如果不想改名,则可以建立一个联接:

ln -s mysql-5.7.11-linux-glibc2.5-x86_64 mysql

此后就可以用/work/program/mysql来找到mysql的安装目录了

注意,如果mysql目录下没有data目录,手动建一个。

【目录权限设置】
将mysql及其下所有的目录所有者和组均设为mysql:

cd /work/program/mysql

chown mysql:mysql -r .

【初始化】

/work/program/mysql/bin/mysqld –initialize –user=mysql –datadir=/work/program/mysql/data –basedir=/work/program/mysql

注意:

  1. data目录解压后没有,需要手动建立(见上文);
  2. mysql5.7和之前版本不同,很多资料上都是这个命令
    …../scripts/mysql_install_db –user=mysql
    而5.7版本根本没有这个。

初始化成功后出现如下信息:
201x-xx-xxt07:10:13.583130z 0 [warning] timestamp with implicit default value is deprecated. please use –explicit_defaults_for_timestamp server option (see documentation for more details).
201x-xx-xx t07:10:13.976219z 0 [warning] innodb: new log files created, lsn=45790
201x-xx-xx t07:10:14.085666z 0 [warning] innodb: creating foreign key constraint system tables.
201x-xx-xx t07:10:14.161899z 0 [warning] no existing uuid has been found, so we assume that this is the first time that this server has been started. generating a new uuid: 1fa941f9-effd-11e5-b67d-000c2958cdc8.
201x-xx-xx t07:10:14.165534z 0 [warning] gtid table is not ready to be used. table ‘mysql.gtid_executed’ cannot be opened.
201x-xx-xx t07:10:14.168555z 1 [note] a temporary password is generated for root@localhost: q1slew5t_6k,

注意最后一行,这也是和之有版本不同的地方,它给了root一个初始密码,后面要登录的时候要用到这个密码。

【配置】
将mysql/support-files下的my-default.cnf改名为my.cnf,拷到/etc下(或者考到{mysql}下,然后作一个软链接到/etc下):

cp /work/program/mysql/support-files/my-default.cnf /etc/my.cnf

my.cnf中关键配置:
[mysqld]
basedir = /work/program/mysql
datadir = /work/program/mysql/data
port = 3306
socket = /work/program/mysql/tmp/mysql.sock

sql_mode=no_engine_substitution,strict_trans_tables

注意,tmp目录不存在,请创建之。

如果不把my.cnf拷到/etc下,运行时会出现:
mysqld: can’t change dir to ‘/usr/local/mysql/data/’ (errcode: 2 – no such file or directory)
这样的出错提示,说明它没找到my.cnf中的配置;而去找了程序编译时的默认安装位置:/usr/local/mysql

四、运行

【运行服务器程序】

{mysql}/bin/mysqld_safe&

注:在这个启动脚本里已默认设置–user=mysql;在脚本末尾加&表示设置此进程为后台进程,区别就是在控制台输入bg,即可将当前进程转入后台,当前shell可进行其他操作。
【停止mysql】
{mysql}/bin/mysqladmin -uroot -p
(注意此时的root是指mysql的root用户)

五、设置mysql以服务运行并且开机启动

将{mysql}/ support-files/mysql.server 拷贝为/etc/init.d/mysql并设置运行权限

cp mysql.server /etc/init.d/mysql

chmod +x /etc/init.d/mysql

把mysql注册为开机启动的服务

chkconfig –add mysql

当然也可以手动进行服务的开启和关闭:

/etc/init.d/mysql start

/etc/init.d/mysql stop

六、客户端连接测试

{mysql}/bin/mysql -uroot -p

此时要求输入密码,就是前面初始化时生成的密码。
这时如果连接服务的时候出现错误:
error 2002 (hy000): can’t connect to local mysql server through socket ‘/tmp/mysql.sock’ (2)
则需要在在my.cnf中填加:
[client]
socket = /work/program/mysql/tmp/mysql.sock

连上后,在做任何操作前,mysql要求要改掉root的密码后才能进行操作。
error 1820 (hy000): you must reset your password using alter user statement before executing this statement.
mysql> alter user ‘root’@’localhost’ identified by ‘xxxxxxx';

七、tips

【查看mysql是否运行】
ps -ef|grep mysqld
netstat -lnp | grep -i mysql

【mysql启动时读取配置文件my.cnf的顺序】
可以运行如下命令查看:
./bin/mysqld –verbose –help |more
default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cnf
可以看到,启动时可以从上述目录下读取配置文件my.cnf。如果当前my.cnf文件不位于上述位置,则必须考过去或做链接。
mysql 5.7新特性之generated column(函数索引) http://www.linuxidc.com/linux/2016-02/128066.htm

升级到mysql 5.7 解决分区问题 http://www.linuxidc.com/linux/2016-02/128060.htm

mysql 5.7 完美的分布式事务支持 http://www.linuxidc.com/linux/2016-02/128053.htm

mysql 5.7 新特性详解 http://www.linuxidc.com/linux/2016-01/127636.htm

mysql 5.7.11 发布下载 http://www.linuxidc.com/linux/2016-02/128268.htm

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网