linux下mysql安装

准备工作

下载mysql安装包,地址
https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz

上传

将本地下载好的安装包mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz上传到/usr/local目录下

解压

1
2
3
4
5
6
#切换到安装目录下
[root@kiko ~]# cd /usr/local/
#解压文件
[root@kiko local]# tar -zxvf mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz
#重命名
[root@kiko local]# mv mysql-5.7.21-linux-glibc2.12-x86_64 mysql

新建用户和用户组

1
2
3
4
#新建用户
[root@kiko local]# useradd mysql
#新建用户组
[root@kiko local]# groupadd mysql

初始化mysql数据库

初始化,记录临时密码的值,在root@localhost:后面,如本人为:Wkj14!mSJsvS

1
2
3
4
5
6
7
8
[root@kiko local]# ./mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
2018-04-16T08:59:34.146011Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-04-16T08:59:35.362600Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-04-16T08:59:35.542848Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-04-16T08:59:35.613718Z 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: 7c789a4b-4154-11e8-9f03-005056b460a8.
2018-04-16T08:59:35.616751Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-04-16T08:59:35.617762Z 1 [Note] A temporary password is generated for root@localhost: Wkj14!mSJsvS
[root@kiko local]#

将mysql启动服务添加只启动项

1
[root@kiko local]# cp ./mysql/support-files/mysql.server /etc/init.d/mysql

配置mysql环境变量

打开环境变量配置文件

1
[root@kiko local]# vim /etc/profile

/etc/profile文件中添加MYSQL_HOME配置,PATH变量追加$MYSQL_HOME/bin

1
2
3
4
MYSQL_HOME=/usr/local/mysql
PATH=$JAVA_HOME/bin:$MYSQL_HOME/bin:$PATH
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export JAVA_HOME PATH CLASSPATH

使配置立即生效

1
[root@kiko local]# source /etc/profile

启动mysql服务

1
2
[root@kiko /]# service mysql start
Starting MySQL. [ OK ]

mysql服务启动 service mysql start

mysql服务停止 service mysql stop

mysql服务重启 service mysql restart

mysql服务状态 service mysql status

测试登陆,输入临时密码 ,修改密码,退出后再次登陆测试

1
2
[root@kiko /]# mysql -u root -p
Enter password: ##这里输入刚才生成的临时密码Wkj14!mSJsvS

出现以下提示代表成功

1
2
3
4
5
6
7
8
9
10
11
12
13
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.21
Copyright (c) 2000, 2018, 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>

重置临时密码

1
2
mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('admin');
Query OK, 0 rows affected, 1 warning (0.00 sec)

用新密码测试登录

1
2
[root@kiko /]# mysql -u root -p
Enter password: ##这里 输入新密码admin

设置远程登录权限

1
2
3
4
5
6
7
8
9
10
11
##切换到mysql库
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
## root用户授权远程登录权限
mysql> grant all privileges on *.* to 'root' @'%' identified by 'admin';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql>

设置开机自启动

1
2
3
4
# 添加到系统自启服务中
chkconfig --add mysql
# 设置开机自启
chkconfig mysql on

安装中错误处理

  1. 在初次启动mysql服务时出现了报错现象如下
    1
    2
    [root@kiko local]# service mysql start
    Starting MySQL.The server quit without updating PID file (/[FAILED]mysql/kiko.pid).

rm -rf /etc/my.cnf删除掉这个文件后重启OK

  1. 卸载mysql,有时候在安装前linux服务器中曾经安装过或者自带有mysql安装文件,会导致安装错误,用以下命令对系统中的mysql文件进行搜索并强制删除
    1
    find / -name "*mysql*" | xargs rm -rf