linux部署mysql

2021/11/6 19:12:22

本文主要是介绍linux部署mysql,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

一 安装

1、进入存放安装包位置文件夹

cd /usr/src/

2、查看系统中是否已安装MySql服务,有两种方式

rpm -qa | grep mysql
yum list installed | grep mysql

3、如果已存在,则删除MySql及其依赖项

yum -y remove mysql-libs.x86_64

4、下载 mysql57-community-release-el7-8.noarch.rpm 的 YUM 源

wget http://repo.mysql.com/mysql57-community-release-el7-8.noarch.rpm

5、安装 mysql57-community-release-el7-8.noarch.rpm

rpm -ivh mysql57-community-release-el7-8.noarch.rpm

6、安装 MySql,一路 Y 到底

yum install mysql-server

7、启动mysql 获取随机密码

安装完毕后,运行mysql,然后在 /var/log/mysqld.log 文件中会自动生成一个随机的密码,我们需要先取得这个随机密码,以用于登录 MySQL 服务端

service mysqld start
grep "password" /var/log/mysqld.log

将会返回如下内容,末尾字符串就是密码,把它保存下来

A temporary password is generated for root@localhost: 2ru>oFqj+fHw

8、登录到 MySQL 服务端并更新用户 root 的密码,并给与ip登录权限。

mysql -u root -p
2ru>oFqj+fHw

登陆后更改密码(密码使用了强度验证,所以需要有一定强度的密码)

ALTER USER 'root'@'localhost' identified by '(新密码)';
flush privileges;

设置用户 root 可以在任意 IP 下被访问

grant all privileges on *.* to root@"%" identified by '(新密码)';

设置用户 root 可以在本地被访问

grant all privileges on *.* to root@"localhost" identified by '(新密码)';

然后刷新权限后生效

flush privileges;

这样环境就搭建完成了。
注意:如果用远程工具还是连接不上,试试用 iptables -F 命令来清除防火墙中链中的规则

9.修改配置表
打开配置表,文件路径:

vim /etc/my.cnf

设置 MySQL 的字符集为 UTF-8
去除大小写敏感 my.cnf(/etc/my.cnf)的[mysqld]区段下增加: lower_case_table_names=1

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

[client]

default-character-set=utf8

[mysqld]

default-storage-engine=INNODB

character-set-server=utf8

collation-server=utf8_general_ci


#
# 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
#
# 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
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

重启MySql后登陆,查看字符集

重启命令
service mysqld restart
查看字符集命令
show variables like '%character%';

10.MySQL 采用的 TCP/IP 协议传输数据,默认端口号为 3306,我们可以通过如下命令查看

netstat -anp | grep 3306

--查看MySql数据库物理文件存放位置

mysql> show global variables like "%datadir%";

mysql服务常用的几个命令

service mysqld start; 启动
service mysqld stop; 停止
service mysqld restart; 重启
service mysqld status; 查看状态

systemctl start mysqld
service mysqld stop
service mysqld restart
systemctl status mysqld

二 开启端口

阿里云 腾讯云 需要在安全组添加规则



这篇关于linux部署mysql的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程