【Mysql】Linux环境下安装Mysql

安装

groupadd mysql

useradd -r -g mysql -s /bin/false mysql

mkdir -p /var/mysql/data
mkdir -p /var/mysql/logs
touch /var/mysql/logs/error.log
chown -R mysql:mysql /var/mysql/data
chown -R mysql:mysql /var/mysql/logs
chmod -R 750 /var/mysql/data
chmod -R 750 /var/mysql/logs


cd /usr/local/src

wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz

tar -xzvf mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz

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

cd /usr/local/mysql

# 修改配置文件
vi /usr/local/mysql/etc/my.cnf 

bin/mysqld --initialize --user=mysql

bin/mysql_ssl_rsa_setup

bin/mysqld_safe --user=mysql &

验证

[root@VM-4-6-centos mysql]# netstat -alnp | grep mysql
tcp        0      0 0.0.0.0:36306           0.0.0.0:*               LISTEN      94844/mysqld    
tcp        1      0 127.0.0.1:43248         127.0.0.1:36306         CLOSE_WAIT  45721/./bin/mysql
unix  2      [ ACC ]     STREAM     LISTENING     442984   94844/mysqld         /tmp/mysql.sock
unix  2      [ ]         STREAM     CONNECTED     442969   94844/mysqld
unix  2      [ ]         STREAM     CONNECTED     239009   45721/./bin/mysql

初始化密码查看

[root@VM-4-6-centos mysql]# grep password /var/mysql/logs/error.log
2021-12-14T15:24:11.796964Z 1 [Note] A temporary password is generated for root@localhost: S1iqa89UIm&P

登录

# 登录
./bin/mysql -h 127.0.0.1 -P 36306 -u root -p

# 修改初始密码
ALTER USER 'root'@'localhost' IDENTIFIED BY 'S1iqa89UIm&P';

创建子用户

# 创建用户
use mysql;
create user 'dev'@'%' identified by 'dev_#29@f18fgw3';
# 授权
grant all privileges on dev to 'dev'@'%' identified by 'dev_#29@f18fgw3';

# 修改密码
update user set authentication_string = password('dev_#29@f18fgw3') where user = 'dev' limit 1;

# 允许外网访问
update user set host = "%" where `user` = "dev";

# 刷新权限
flush privileges;

配置文件修改 /usr/local/mysql/etc/my.cnf

[mysqld]
#mysql启动时使用的用户
user    = mysql
#默认连接端口
port    = 36306
#为MySQL客户端程序和服务器之间的本地通讯指定一个套接字文件
# socket    = /data/mysql/data/mysql.sock
#数据库服务器id,这个id用来在主从服务器中标记唯一mysql服务器
server-id = 1
#端口绑定的ip地址,0.0.0.0表示允许所有远程访问,127.0.0.1表示只能本机访问,默认值为*
bind-address = 0.0.0.0
#默认名为 主机名.pid,在数据库/mysql/data/主机名.pid,记录mysql运行的process id
#如果存在,再次start时会报已经启动
# pid-file = /data/mysql/data/mysql.pid

#安装目录
basedir    = /usr/local/mysql
#数据库存放目录
datadir    = /data/mysql/data/


##错误日志:记录启动,运行,停止mysql时出现的信息
log-error = /data/mysql/logs/error.log

你可能感兴趣的:(【Mysql】Linux环境下安装Mysql)