centos安装使用mysql

下载mysql5.7.x 官网下载地址

安装依赖

yum -y install numactl
yum install -y libaio

wget http://tar.matosiki.site/tar/mysql-5.7.27-linux-glibc2.12-x86_64.tar.gz

解压到指定目录

tar -zxvf mysql-5.7.27-linux-glibc2.12-x86_64.tar.gz -C /usr/local

mv /usr/local/mysql-5.7.27-linux-glibc2.12-x86_64 /usr/local/mysql

添加用户组并添加用户

sudo groupadd mysql

sudo useradd -g mysql mysql

配置mysql启动

# 新建mysql的data文件夹
mkdir -p /usr/local/mysql/data

# 给mysql 文件夹赋权限
chown -R mysql:mysql /usr/local/mysql

添加配置文件相关信息

cat << EOF > /etc/my.cnf
[client]
port=3306
socket=/usr/local/mysql/mysql.sock
default-character-set=utf8

[mysqld]
skip-name-resolve
user=mysql
basedir=/usr/local/mysql/
datadir=/usr/local/mysql/data
port=3306
server-id=10
socket=/usr/local/mysql/mysql.sock
log-bin=/usr/local/mysql/data/mysql-bin
character-set-server=utf8
#skip-grant-tables

[mysqld_safe]
pid-file=/usr/local/mysql/mysql.pid
log-error=/usr/local/mysql/data/mysql.err

EOF

注意配置中的server-id不是server_id 通过mysql命令行设置使用set global server_id=10;这是用的是下划线,一个巨大的坑.

# 复制开机配置
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

# 更改启动文件权限
chmod 755 /etc/init.d/mysqld

在myslqd启动文件中找到配置并更改(这一步可以不用配置,mysql默认地址跟一下配置一样)

basedir=/usr/local/mysql
datadir=/usr/local/mysql/data

初始化数据库 (这里最容易出错 注意这个命令的反斜杠换行)

# 5.6版本执行 5.7已废弃
/usr/local/mysql/bin/mysql_install_db \
--defaults-file=/etc/my.cnf \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data &


# 使用最新安装脚本
# 生成自定义密码
/usr/local/mysql/bin/mysqld --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data --initialize
# 不生成自定义密码

/usr/local/mysql/bin/mysqld --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data --initialize-insecure

你会看到没有报错就成功了

启动mysql

service mysqld start

配置环境变量

echo 'export PATH=/usr/local/mysql/bin:$PATH' >> /etc/profile
source /etc/profile

登录测试

注意试着先用mysqladmin 设置密码在登陆

cd /usr/local/mysql/bin

默认注释skip-grant-tables配置需要初始化登录密码

./mysqladmin -u root password ‘123456’

或者一行命令(参数-p后面是生成的密码)

./mysqladmin -uroot -pxxxx password 123456

试着用初始化的密码登录

./mysql -uroot -p123456

mysqladmin更改密码命令格式:mysqladmin -u 用户名 -p 旧密码 password 新密码

添加自启动

chkconfig --add mysqld
chkconfig mysqld on
chkconfig --list mysqld

开启用户外网访问

打开防火墙

添加特定端口

firewall-cmd –zone=public –add-port=3306/tcp –permanent

查看端口是否添加成功

firewall-cmd –zone=public –query-port=3306/tcp

重新载入规则

firewall-cmd –reload

设置该用户可以外网访问

进入mysql

./mysql -uroot -p123456

use mysql

查看mysql用户

select user,password,host from user;

赋予外网访问权限

grant all privileges on . to 用户名@”Ip地址” identified by “密码”;

grant all privileges on *.* to root@"%" identified by "123456";

刷新一下

flush privileges;

连接测试

错误解决

  1. /vmware-install.pl: /user/bin/perl: 坏的解释器:没有那个文件或目录

解决:

yum install perl gcc kernel-devel
  1. please install the following Perl modules before executing /usr/local/mysql/scripts/mysql_install_db

解决

yum -y install autoconf

本文总阅读量