提示:为了简单起见,在安装过程中已默认关闭了firewalld防火墙 和 SELinux。实际生产中,则不这样子做,而是开放相应的端口,以增强安全性。
关闭firewalld防火墙 和 SELinux关闭命令如下:
//-----------------------------------关闭Firewalld防火墙 //1、停止firewalld服务 systemctl stop firewalld //2、禁止firewalld开机启动 systemctl disable firewalld //-----------------------------------关闭SELinux //1、临时关闭 setenforce 0 //2、永久关闭SELinux sed -i "s/^SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
1、查看自己本机电脑的Mysql版本是多少(这里我的Mysql版本是5.7.13),然后去下载相对应的Mysql版本 或者 更高的版本
注意:Mysql5.6以下版本是不允许同时设置两个字段为CURRENT_TIMESTAMP的
2、进入Mysql下载官网,请点击:MySql官网下载链接,在Product Version选项框中选择与自己本机对应的Mysql版本(这里我选择5.7.13),Operating System选项框中选择Linux - Generic
如果自己本机电脑是64位的,就选择下载 Linux - Generic (glibc 2.5) (x86, 64-bit), Compressed TAR Archive
如果自己本机电脑是32位的,就选择下载 Linux - Generic (glibc 2.5) (x86, 32-bit), Compressed TAR Archive
如果觉得下载比较繁琐,我已经提供了Mysql安装包:
链接:https://pan.baidu.com/s/1hUcRvrZncYXbsn-4uzPA0A
提取码:p8j3
3、下载完成之后,打开WinSCP,把我们下载好的Mysql压缩包,上传到Linux的 /mnt/ 文件目录下
4、使用putty连接到我们的Linux服务器,进入到/mnt/ 文件目录中,解压 mysql-5.7.13-linux-glibc2.5-x86_64.tar.gz,把jdk1.8.0_191重命名为 jdk1.8,并把jdk1.8移动到/usr/local/ 目录下
[root@localhost ~]# cd /mnt/ //进入mnt文件目录
[root@localhost mnt]# tar xzf mysql-5.7.13-linux-glibc2.5-x86_64.tar.gz //解压mysql
[root@localhost mnt]# ls //查看mnt目录
mysql-5.7.13-linux-glibc2.5-x86_64 mysql-5.7.13-linux-glibc2.5-x86_64.tar.gz
//重命名为mysql-5.7.13
[root@localhost mnt]# mv mysql-5.7.13-linux-glibc2.5-x86_64 mysql-5.7.13
[root@localhost mnt]# mv mysql-5.7.13 /usr/local/ //把mysql-5.7.13移动到/usr/local/ 目录下
5、添加mysql组和mysql用户
[root@localhost mnt]# groupadd mysql //添加mysql组
[root@localhost mnt]# useradd -r -g mysql mysql //添加mysql用户
6、进入到/usr/local/mysql-5.7.13/的安装目录,在mysql-5.7.13目录下创建一个空的data文件夹,最后修改当前目录的拥有者为mysql用户
[root@localhost mnt]# cd /usr/local/mysql-5.7.13/ //进入到mysql-5.7.13的安装目录下
[root@localhost mysql-5.7.13]# mkdir data //在mysql-5.7.13目录下创建一个空的data文件夹
[root@localhost mysql-5.7.13]# ls //查看mysql-5.7.13目录
bin COPYING data docs include lib man README share support-files
[root@localhost mysql-5.7.13]# chown -R mysql:mysql ./ //修改当前目录拥有者为mysql用户
7、进入到mysql-5.7.13/bin/ 目录下,执行Mysql数据库的安装命令
[root@localhost mysql-5.7.13]# cd ./bin/ //进入到mysql-5.7.13/bin 目录下
//执行Mysql数据库的安装命令 --basedir=mysql安装目录 --datadir=data数据表存放目录
[root@localhost bin]# ./mysqld --user=mysql --basedir=/usr/local/mysql-5.7.13 --datadir=/usr/local/mysql-5.7.13/data --initialize
显示结果如下,注意最后一行,root@localhost: 后面跟的是mysql数据库登录的临时密码,各人安装生成的临时密码不一样
2018-10-29T21:21:22.817985Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-10-29T21:21:29.738086Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-10-29T21:21:30.878586Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-10-29T21:21:31.056265Z 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: 9ab51456-dbc0-11e8-9733-000c297c5610.
2018-10-29T21:21:31.058465Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-10-29T21:21:31.062466Z 1 [Note] A temporary password is generated for root@localhost: d*V/j:ewN9r(
8、进入到mysql-5.7.13/support-files/ 目录下,复制一份 my-default.cnf 模板配置文件到 /etc/my.cnf
[root@localhost bin]# cd /usr/local/mysql-5.7.13/support-files/ //进入到mysql-5.7.13/support-files/ 目录下
[root@localhost support-files]# ls //查看support-files目录
magic my-default.cnf mysqld_multi.server mysql-log-rotate mysql.server
[root@localhost support-files]# cp my-default.cnf /etc/my.cnf //复制一份 my-default.cnf 模板配置文件到 /etc/my.cnf
9、编辑/etc/my.cnf 配置文件,设置mysql数据库字符集编码为utf8 和 存储引擎为InnoDB
[root@localhost support-files]# vi /etc/my.cnf //编辑/etc/my.cnf配置文件
在[mysqld]上面加入下面两句话
[client]
default-character-set=utf8 //客户端编码方式为utf-8,最好和服务端编码保存一致
default-character-set=utf8mb4 //推荐更改为utf8mb4,因为utf8编码只支持3字节的数据,而移动端的表情数据是4个字节的字符,同时utf8mb4是兼容utf8的,所以不用担心原来的数据出现乱码情况。
在[mysqld]最下面加入下面几句话
default-storage-engine=InnoDB //存储引擎为InnoDB
character-set-server=utf8 //服务端的编码方式为utf-8
character-set-server=utf8mb4 //推荐更改为utf8mb4
collation-server=utf8_general_ci //排序规则为utf8_general_ci
collation-server=utf8mb4_unicode_ci //如果编码设置为utf8mb4,则排序规则设置为utf8mb4_unicode_ci
10、进入到mysql-5.7.13 目录下,启动mysql服务
[root@localhost support-files]# cd .. //进入到mysql-5.7.13 目录下
[root@localhost mysql-5.7.13]# ./support-files/mysql.server start //启动mysql服务
启动mysql服务命令报错,这里是因为我们没有修改 mysql-5.7.13/support-files/mysql.server配置文件,报错内容如下
./support-files/mysql.server: line 276: cd: /usr/local/mysql: No such file or directory
Starting MySQLCouldn't find MySQL server (/usr/local/mysql/[FAILED]ld_safe)
11、修改 mysql-5.7.13/support-files/mysql.server配置文件
[root@localhost mysql-5.7.13]# vi ./support-files/mysql.server
修改前:
if test -z "$basedir"
then
basedir=/usr/local/mysql
bindir=/usr/local/mysql/bin
if test -z "$datadir"
then
datadir=/usr/local/mysql/data
fi
sbindir=/usr/local/mysql/bin
libexecdir=/usr/local/mysql/bin
else
bindir="$basedir/bin"
if test -z "$datadir"
then
datadir="$basedir/data"
fi
sbindir="$basedir/sbin"
libexecdir="$basedir/libexec"
fi
修改后:
if test -z "$basedir"
then
basedir=/usr/local/mysql-5.7.13
bindir=/usr/local/mysql-5.7.13/bin
if test -z "$datadir"
then
datadir=/usr/local/mysql-5.7.13/data
fi
sbindir=/usr/local/mysql-5.7.13/bin
libexecdir=/usr/local/mysql-5.7.13/bin
else
bindir="$basedir/bin"
if test -z "$datadir"
then
datadir="$basedir/data"
fi
sbindir="$basedir/sbin"
libexecdir="$basedir/libexec"
fi
12、设置Mysql开机启动
[root@localhost mysql-5.7.13]# vi /etc/rc.d/rc.local
添加如下代码到 /etc/rc.d/rc.local 中:
/usr/local/mysql-5.7.13/support-files/mysql.server start
13、修改mysql.server配置文件后,在mysql-5.7.13 目录下,再次启动mysql服务,启动成功
[root@localhost mysql-5.7.13]# ./support-files/mysql.server start //启动mysql服务
14、把mysql客户端放到默认路径,便于启动的时候找/usr/local/bin/目录下的mysql
注意:建议使用软链过去,不要直接包文件复制,便于系统安装多个版本的mysql
[root@localhost mysql-5.7.13]# ln -s /usr/local/mysql-5.7.13/bin/mysql /usr/local/bin/mysql
15、登录mysql,密码为之前生成的临时密码(临时密码就是之前生成的root@localhost:后面的内容)
[root@localhost mysql-5.7.13]# /usr/local/mysql-5.7.13/bin/mysql -u root -p //登录mysql
16、修改mysql的登录密码
mysql> set password=password('123456'); //修改mysql的登录密码为123456
17、创建用户,并赋予远程登录权限
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION; //创建root用户,密码为123456,并赋予远程登录权限
mysql> FLUSH PRIVILEGES; //刷新权限
mysql> SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user; //查看数据库所有用户
18、远程连接mysql成功
Mysql的整个安装过程到此结束。