目录
1.检查操作系统是否自带mysql
2.下载mysql5.7
3.创建mysql用户组和mysql用户
4.设置mysql目录访问权限,用户组
5.配置mysql
6.初始化mysql
7.启动mysql
方式一
方式二
8.配置mysql到环境变量
9.mysql忘记密码
第一步:跳过MySQL的密码认证过程
第二步:重启mysql
第三步:登录mysql
第四步:使用sql语句修改密码
第五步:重新编辑my.cnf
10.设置mysql远程登录
10.1报错
10.2继续配置
10.3重启mysql
10.4设置防火墙
10.5远程连接mysql
操作系统:Centos6.4 64位
工具:Xftp5、Xshell5
安装软件:mysql5.7
说明:使用官方编译好的二进制文件进行安装
[root@localhost local]# rpm -qa|grep mysql
mysql-libs-5.1.73-8.el6_8.x86_64
比如我这里自带了一个mysql。
使用yum命令,因为yum命令可以自动删除与mysql相关的依赖;如果使用rpm命令,则还需要手动去删除和mysql相关的文件
[root@localhost local]# yum -y remove mysql-libs-5.1.73-8.el6_8.x86_64
检查删除是否成功。如果没有提示则删除成功。
[root@localhost local]# rpm -qa|grep mysql
mysql官网 https://dev.mysql.com/downloads/mysql/5.7.html#downloads
将下载的安装包"mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz"利用xftp上传至/usr/local下
解压
[root@localhost local]# tar -zxvf mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz
顺手删除mysql5.7的源码包
[root@localhost local]# rm -f mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz
修改文件名
[root@localhost local]# mv mysql-5.7.21-linux-glibc2.12-x86_64/ mysql57
进入/mysql57创建data文件夹
[root@localhost local]# cd mysql57/
[root@localhost mysql57]# mkdir data
#查看有没有mysql组
[root@localhost mysql57]# cat /etc/group | grep mysql
查看有没有mysql用户
[root@localhost mysql57]# cat /etc/passwd |grep mysql
若没有则创建mysql组
#创建mysql用户组
[root@localhost mysql57]# groupadd mysql
#创建mysql用户并添加到mysql用户组中(//useradd -r参数表示mysql用户是系统用户,不可用于登录系统;第一个mysql是用户组,第二个mysql是用户)
[root@localhost mysql57]# useradd -r -g mysql mysql
#检查用户组是否创建成功
[root@localhost mysql57]# groups mysql
#将mysql目录访问权限赋为myql用户
[root@localhost mysql57]# chown -R mysql /usr/local/mysql57
#改变mysql目录的用户组属于mysql组
[root@localhost mysql57]# chgrp -R mysql /usr/local/mysql57
#查看mysql目录下所有的目录及文件夹所属组合用户
[root@localhost mysql57]# cd /usr/local/mysql57
[root@localhost mysql57]# ll
total 56
drwxr-xr-x 2 mysql mysql 4096 Aug 11 21:24 bin
-rw-r--r-- 1 mysql mysql 17987 Dec 28 2017 COPYING
drwxr-xr-x 2 mysql mysql 4096 Aug 11 21:40 data
drwxr-xr-x 2 mysql mysql 4096 Aug 11 21:24 docs
drwxr-xr-x 3 mysql mysql 4096 Aug 11 21:23 include
drwxr-xr-x 5 mysql mysql 4096 Aug 11 21:24 lib
drwxr-xr-x 4 mysql mysql 4096 Aug 11 21:23 man
-rw-r--r-- 1 mysql mysql 2478 Dec 28 2017 README
drwxr-xr-x 28 mysql mysql 4096 Aug 11 21:24 share
drwxr-xr-x 2 mysql mysql 4096 Aug 11 21:24 support-files
权限被修改
创建以下文件,设置访问权限,用于mysql配置中
第一步:创建文件/tmp/mysql.sock。并设置权限
创建文件
[root@localhost mysql57]# mkdir tmp
[root@localhost mysql57]# cd tmp
[root@localhost tmp]# ll
total 0
[root@localhost tmp]# touch mysql.sock
[root@localhost tmp]# ll
total 0
-rw-r--r-- 1 root root 0 Aug 11 21:59 mysql.sock
设置权限
[root@localhost tmp]# chown -R mysql:mysql /usr/local/mysql57/tmp/mysql.sock
[root@localhost tmp]# chmod 755 /usr/local/mysql57/tmp/mysql.sock
第二步:创建/log/mysqld.log。并设置权限
[root@localhost mysql57]# mkdir log
[root@localhost mysql57]# cd log
[root@localhost log]# ll
total 0
[root@localhost log]# touch mysqld.log
[root@localhost log]# chown -R mysql:mysql /usr/local/mysql57/log/mysqld.log
[root@localhost log]# chmod 755 /usr/local/mysql57/log/mysqld.log
如果出错,说明路径没有写全,要写绝对路径
第三步:创建/tmp/mysqld.pid。并设置权限
[root@localhost log]# cd ../tmp
[root@localhost tmp]# touch mysqld.pid
[root@localhost tmp]# chown -R mysql:mysql /usr/local/mysql57/tmp/mysqld.pid
[root@localhost tmp]# chmod 755 /usr/local/mysql57/tmp/mysqld.pid
[root@localhost mysql57]# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql57/ --datadir=/usr/local/mysql57/data/
可能会报错。报错信息
bin/mysqld: error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory
解决方法:
原因:
yum安装的是libnuma.so.1,但安装时默认安装的是32的,而db2需要的是64位的
1.如果已经安装了libnuma.so.1,先yum remove libnuma.so.1
[root@localhost mysql57]# yum remove libnuma.so.1
2.安装依赖包 yum -y install numactl.x86_64
[root@localhost mysql57]# yum -y install numactl.x86_64
安装完成后重新,执行
[root@localhost mysql57]# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql57/ --datadir=/usr/local/mysql57/data/
安装成功
配置SSL参数(在mysql目录下)
[root@localhost mysql57]# bin/mysql_ssl_rsa_setup --datadir=/usr/local/mysql57/data/
由于mysql-5.7.23版本my.cnf不在/support-files下,故我们创建my.cnf文件。
[root@localhost mysql57]# touch /etc/my.cnf
[root@localhost mysql57]# vim /etc/my.cnf
复制如下内容(每个人安装路径可能不一样,需要修改):
[mysqld]
character_set_server=utf8
init_connect='SET NAMES utf8'
basedir=/usr/local/mysql57
datadir=/usr/local/mysql57/data
port = 3306
socket=/tmp/mysql.sock
log-error=/usr/local/mysql57/log/mysqld.log
pid-file=/usr/local/mysql57/tmp/mysqld.pid
#表名不区分大小写
lower_case_table_names = 1
max_connections=5000
sql_mode=STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
配置完成
[root@localhost mysql57]# bin/mysqld_safe --user=mysql &
查看mysql的运行情况
[root@localhost mysql57]# ps -ef |grep mysql
第一我没有成功,因为我有个地方出问题了。我在方式二配置成功。
配置mysql自动启动(可根据需要配置)
[root@localhost mysql57]# cp support-files/mysql.server /etc/init.d/mysql
[root@localhost mysql57]# vim /etc/init.d/mysql
添加配置(i 进入编辑;esc--> :wq保存退出)
若配置了mysql自启动方式则可以使用服务方式启动mysql
#查看mysql状态
/etc/init.d/mysql status 或者 service mysql status
#启动mysql
/etc/init.d/mysql start 或者 service mysql start
#停止mysql
/etc/init.d/mysql stop 或者 service mysql stop
#重新启动mysql
/etc/init.d/mysql restart 或者 service mysql restart
查看mysql服务说明启动成功
ps -ef|grep mysql
启动mysql
[root@localhost tmp]# service mysql start
Starting MySQL. ERROR! The server quit without updating PID file (/usr/local/mysql57/tmp/mysqld.pid).
报错了,上面说没有/usr/local/mysql57/tmp/mysqld.pid。
解决方案:
a)创建文件/usr/local/mysql57/tmp/mysqld.pid
b)修改权限
修改存放mysqld.pid文件目录的权限
chown -R mysql /usr/local/mysql57/tmp
chgrp -R mysql /usr/local/mysql57/tmp
chmod 777 /usr/local/mysql57/tmp
重新启动成功(如果还不行,就是中间某个步骤写错了。或者直接把mysql目录权限赋为777)
[root@localhost tmp]# /etc/init.d/mysql start
Starting MySQL. SUCCESS!
[root@localhost tmp]# vim /etc/profile
export MYSQL57_HOME=/usr/local/mysql57/bin #mysql5.7/bin路径
export PATH=$PATH:${MYSQ57_HOME}
这个地方是冒号 ( :)
设置环境变量立即生效
[root@localhost tmp]# source /etc/profile
切换到mysql下的bin目录
[root@localhost bin]# ./mysql -u root -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
密码错误。。而且之前安装的密码也忘记了
解决方案:
(注:windows下修改的是my.ini)
[root@localhost bin]# #vim /etc/my.cnf
在[mysqld]后面任意一行添加“skip-grant-tables”用来跳过密码验证的过程,如下图所示:
保存并退出(esc--> :wq)
[root@localhost bin]# /etc/init.d/mysql restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
进入mysql/bin目录,启动mysql
[root@localhost bin]# ./mysql
启动成功
mysql> use mysql;
mysql> update user set authentication_string=password("你的新密码") where user="root";
mysql> flush privileges;
mysql> quit
注意:mysql5.7没有password字段
ERROR 1054 (42S22): Unknown column 'password' in 'field list'
去掉[mysqld]后面的“skip-grant-tables”
[root@localhost bin]# vim /etc/my.cnf
重启mysql
[root@localhost bin]# /etc/init.d/mysql restart
先本地登录mysql
[root@localhost bin]# ./mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
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.
突然报错
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> use mysql
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
解决方案:需要重新修改一下密码
mysql> alter user 'root'@'localhost' identified by '修改的密码';
mysql> flush privileges;
mysql> quit;
mysql> use mysql;
mysql> update user set host='%' where user='root';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' IDENTIFIED BY 'mypwd' WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES;
mysql> quit;
[root@localhost bin]# service mysql restart;
a)配置防火墙开启3306端口
[root@localhost bin]# /sbin/iptables -I INPUT -p tcp --dport 3306-j ACCEPT
[root@localhost bin]# /etc/rc.d/init.d/iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables: [ OK ]
[root@localhost bin]# /etc/rc.d/init.d/iptables restart
iptables: Setting chains to policy ACCEPT: filter [ OK ]
iptables: Flushing firewall rules: [ OK ]
iptables: Unloading modules: [ OK ]
iptables: Applying firewall rules: [ OK ]
b)临时关闭防火墙
[root@localhost bin]# service iptables stop
c)永久关闭防火墙
重启后永久生效
[root@localhost bin]# chkconfig iptables off
连接成功
参考博客:
https://blog.csdn.net/u014079773/article/details/79701909
https://www.cnblogs.com/vickygu2007/p/5258526.html
https://www.cnblogs.com/gumuzi/p/5711495.html
https://blog.csdn.net/u010603691/article/details/50379282