Linux (CentOS 7) 安装 mysql 5.7

来源:互联网 发布:学生证办理 淘宝 编辑:程序博客网 时间:2024/06/17 23:52

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就使用这个用户来运行(注意这也是mysql启动脚本中默认的用户,因此最好不要改名)。

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

三、安装

接下来我们将mysql 安装到 /usr/local/mysql 目录下

 mkdir /usr/local/mysql

【解压】
将前面得到的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 /usr/local/mysql

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

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

cd /usr/local/mysqlchown mysql:mysql -R .

【初始化】

/usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/usr/local/mysql/data --basedir=/usr/local/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一个初始密码,后面要登录的时候要用到这个密码。

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

cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf

my.cnf中关键配置:

[mysqld]basedir = /usr/local/mysqldatadir = /usr/local/mysql/dataport = 3306socket = /usr/local/mysql/tmp/mysql.socksql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

注意,mysql目录下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 =/usr/local/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文件不位于上述位置,则必须考过去或做链接。