linux下安装mysql5.7.17_Linux安装mysql-5.7.17

一.检查系统是否有自带安装MySQL

1.检查

[root@centos ~]# rpm -qa | grep -i mysql

mysql-libs-5.1.71-1.el6.x86_64

2.卸载

[root@centos ~]# rpm -e --nodeps mysql-libs-5.1.71-1.el6.x86_64

[root@centos ~]# rpm -qa | grep -i mysql

rpm -e --noscripts MySQL-client-5.5.25a-1.rhel5

rm -fr 删除文件

二、解压及重命名

[root@centos ~]# tar -zxvf mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz -C /data/

[root@centos ~]# mv mysql-5.7.17-linux-glibc2.5-x86_64/ mysql-5.7.17

三、建立用户和用户组给mysql赋权限

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

[root@centos mysql-5.7.17]# groupadd mysql

[root@centos mysql-5.7.17]# useradd -r -g mysql mysql

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

2.手动创建MySQL data目录

[root@centos data]# mkdir /data/mysql-5.7/data

3.目录权限设置

将mysql及其下所有的目录所有者和组均设为mysql:

[root@centos mysql-5.7]# chown -R mysql:mysql /data/mysql-5.7/

[root@centos mysql-5.7]# chmod -R 755 /data/mysql-5.7./

四、数据库初始化

[root@centos bin]# ./mysqld --initialize --user=mysql --datadir=/data/mysql-5.7/data --basedir=/data/mysql-5.7

初始化完成记录下初始密码,登录数据库要用到

root@localhost: TsYB;K9rwrK6

五、配置

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

[root@centos support-files]# cp /data/mysql-5.7.17/support-files/my-default.cnf /etc/my.cnf

修改/etc/my.cnf中关键配置:

[root@centos support-files]# vim /etc/my.cnf

[mysqld]

basedir = /data/mysql-5.7.17

datadir = /data/mysql-5.7.17/data

port = 3306

socket = /data/mysql-5.7.17/tmp/mysql.sock

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

[root@centos data]# mkdir /data/mysql-5.7.17/tmp

六、运行

1.运行之前重新给MySQL目录赋予权限

[root@centos mysql-5.7.17]# chown -R mysql:mysql /data/mysql-5.7/

[root@centos data]# chmod -R 755 /data/mysql-5.7.17/

2.运行服务器程序

[root@centos bin]# ./mysqld_safe &

注:在这个启动脚本里已默认设置--user=mysql;在脚本末尾加&表示设置此进程为后台进程,区别就是在控制台输入bg,即可将当前进程转入后台,当前shell可进行其他操作。

可能会报错:

2017-10-11T13:04:21.482778Z mysqld_safe Logging to '/data/mysql-5.7/data/centos.xd.err'.

2017-10-11T13:04:21.485731Z mysqld_safe The file /usr/local/mysql/bin/mysqld

does not exist or is not executable. Please cd to the mysql installation

directory and restart this script from there as follows:

./bin/mysqld_safe&

See http://dev.mysql.com/doc/mysql/en/mysqld-safe.html for more information

说明:mysqld_safe启动脚本默认的从/usr/local/mysql目录中读取另外一个启动脚本mysqld,因为我的安装目录为/data/mysql-5.17/bin/mysqld,所以找不到相关文件。

解决方法:

[root@centos mysql-5.7]# mkdir -p /usr/local/mysql/bin/[root@centos mysql-5.7]# ln -s /data/mysql-5.7/bin/mysqld /usr/local/mysql/bin/

3、设置mysql以服务运行

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

[root@centos data]# cp /data/mysql-5.7/support-files/mysql.server /etc/init.d/mysql

[root@centos data]#chmod +x /etc/init.d/mysql

4.启动mysql

[root@centos data]# /etc/init.d/mysql start

可能会出现错误:

1:Starting MySQL.Logging to ‘/data/mysqldata/localhost.localdomain.err’.

ERROR! The server quit without updating PID file (/data/mysqldata/localhost.localdomain.pid).

bin/mysqld –initialize –user=mysql 是关键,再重新安装下

[root@centos mysql-5.7]# /etc/init.d/mysql start

Starting MySQL. SUCCESS!

5.连接数据库

[root@centos ~]# mysql -uroot -p

可能会出现错误:

1:-bash: mysql: command not found

将mysql/bin/mysql命令 链接到usr/bin/mysql x

[root@centos ~]# ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql

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

则需要在在my.cnf中填加:

[client]

socket = /data/mysql-5.7/tmp/mysql.sock

如果不行则使用软链接:

[root@centos tmp]# ln -s /data/mysql-5.7/tmp/mysql.sock /tmp/mysql.sock

启动成功

[root@centos mysql-5.7]# mysql -uroot -p

Enter password: 输入初始化数据库时记录的密码

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 3

Server version: 5.7.17 MySQL Community Server (GPL)

设置root用户密码

mysql> set password=password('123456');

Query OK, 0 rows affected, 1 warning (0.03 sec)

给root账户赋予全部权限

mysql> grant all privileges on *.* to root@'localhost' identified by '123456';

Query OK,0 rows affected, 1 warning (0.00 sec)

刷新

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

退出mysql

mysql> exit;

Bye

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

#chkconfig --add mysql

使用范例:

chkconfig --list #列出所有的系统服务

chkconfig --add httpd #增加httpd服务

chkconfig --del httpd #删除httpd服务

chkconfig --level httpd 2345 on #设置httpd在运行级别为2、3、4、5的情况下都是on(开启)的状态

chkconfig --list #列出系统所有的服务启动情况

chkconfig --list mysqld #列出mysqld服务设置情况

chkconfig --level 35 mysqld on #设定mysqld在等级3和5为开机运行服务,--level 35表示操作只在等级3和5执行,on表示启动,off表示关闭

chkconfig mysqld on #设定mysqld在各等级为on,“各等级”包括2、3、4、5等级

如何增加一个服务:

1.服务脚本必须存放在/etc/ini.d/目录下;

2.chkconfig --add servicename

在chkconfig工具服务列表中增加此服务,此时服务会被在/etc/rc.d/rcN.d中赋予K/S入口了;

3.chkconfig --level 35 mysqld on

修改服务的默认启动等级。

mysql服务的开启和关闭:

[root@centos mysql-5.7]# /etc/init.d/mysql start

[root@centos mysql-5.7]# /etc/init.d/mysql stop

或:

[root@centos mysql-5.7]# service mysql stop

Shutting down MySQL.. SUCCESS!

[root@centos mysql-5.7]# service mysql start

Starting MySQL. SUCCESS!

查看MySQL的安装目录

[root@centos mysql-5.7]# whereis mysql

mysql: /usr/bin/mysql /usr/lib64/mysql /usr/local/mysql /usr/share/mysql

查看Mysql的运行目录

[root@centos mysql-5.7]# which mysql

/usr/bin/mysql

忘记root密码

# mysqld_safe --skip-grant-tables &

为了安全可以这样禁止远程连接:

# mysqld_safe --skip-grant-tables --skip-networking &

使用mysql连接server:

# mysql -p

更改密码:

mysql> update mysql.user set authentication_string=password('123456') where user='root' and Host = 'localhost';

mysql> flush privileges;

mysql> quit;

修改完毕。重启

# service mysql restart

登录MySQL

mysql> set password for 'root'@'localhost'=password('123456');

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值