Centos7安装mysql5.7,最新解决办法

问题描述:

服务器环境如下

  • centos7.4
  • mysql5.7  

下载并解压mysql5.7

wget http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz

#解压文件 -zxvf(至小威风) 看到某人的博客里是这样记 o(∩_∩)o 哈哈
tar -zxvf mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz

#move移动到mysql文件
mv mysql-5.7.17-linux-glibc2.5-x86_64/ mysql

2、配置文件

#编辑并查看
vi /mysql/support-files/my-default.cnf

主要配置basedir和datadir目录的路径。按照我们安装mysql的路径填写

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It‘s a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.

[mysqld]

# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M

# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin

# These are commonly set, remove the # and set as required.
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
# port = .....
# server_id = .....
# socket = .....

# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M 

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 

创建一个用户负责管理mysql

#创建一个mysqlGroup组
groupadd mysqlGroup
#创建一个mysql用户并放到mysqlgroup组内
useradd -r -g mysqlgroup mysql 
#设置mysql用户的密码
passwd mysql
#更改mysql拥有者
chown -R mysql:mysqlgroup /user/local/mysql/

3、 初始化

cd /usr/local/mysql/bin/
./mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
**#初始化后会生成一个临时密码 root@localhost::*(最好先记录这个临时密码)**
#启动mysql
./support-files/mysql.server start

#将mysql进程放入系统进程中,命令如下:
cp support-files/mysql.server /etc/init.d/mysqld

#.重新启动mysql服务,命令如下:
service mysqld restart

#检查mysql是否启动
ps -ef|grep mysql
**有进程代表成功安装了。**

4、设置开机自启动

#复制mysql/support-files/mysql.server到/etc/init.d/目录下(目的想实现开机自动执行效果)
cp mysql.server /etc/init.d/mysql

#修改mysql参数
vim /etc/init.d/mysql

#修改为以下内容
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data

#添加mysql服务
chkconfig --add mysql

#设置mysql服务为自启动
chkconfig mysql on

5、配置环境变量

vim /etc/profile
#最后一行加上
export PATH=$JAVA_HOME/bin;/usr/local/mysql/bin;$PATH
#保存退出后,修改生效
source /etc/profile

6、登陆并修改密码

#使用随机密码登录mysql数据库,命令如下:
mysql -u root -p
## 等待系统提示,输入随机密码(上面记录的密码),即可登录

#更改root用户的密码为123456
alter user 'root'@'localhost' identified by '123456';

#选择mysql数据库
use mysql;

#更改mysql可被所有host访问
update user set user.Host='%' where user.User='root';

#刷新权限
flush privileges;

6、远程访问

#将3306打开
#因为centos7防火墙改为firewall
firewall-cmd --zone=public --add-port=3306/tcp --permanent
service firewalld restart

#查看firewall开发的端口
firewall-cmd --zone=public --list-port
这里提醒下,如果远程访问不了,还可能是服务器商的安全组未打开3306端口。(如果是阿里云,到控制台实例的安全组配置规则即可)

常见问题

1.-bash: mysql: command not found

#原因:这是由于系统默认会查找/usr/bin下的命令,如果这个命令不在这个目录下。
#当然会找不到命令,我们需要做的就是映射一个链接到/usr/bin目录下,相当于建立一个链接文件。
ln -s /usr/local/mysql/bin/mysql /usr/bin

你可能感兴趣的:(Centos7安装mysql5.7,最新解决办法)