CentOS-MySql环境搭建

笔记-现学现用-MySql环境搭建

Posted by Replay on October 16, 2018

前言

首次在Linux系统上使用MySql,笔记还是要记录一下的。

进入正题

本次笔者使用YUM安装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

安装完毕后,运行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

7、登录到 MySQL 服务端并更新用户 root 的密码

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

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

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

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

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

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

grant all privileges on *.* to root@"localhost" identified by 'Root123456..';

然后刷新权限后生效

flush privileges;

这样环境就搭建完成了。

注意:如果用远程工具还是连接不上,试试用 iptables -F 命令来清除防火墙中链中的规则

8.修改配置表

打开配置表,文件路径:

/etc/my.cnf

设置 MySQL 的字符集为 UTF-8

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

# 设置 MySQL 的字符集为 UTF-8
[client]
default-character-set=utf8

[mysqld]
# 设置 MySQL 的字符集为 UTF-8
character_set_server=utf8

#
# 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后登陆,查看字符集

show variables like '%character%';

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

netstat -anp

常用Sql命令

--执行 sql 脚本
\. <sql文件绝对路径>

--查看MySql数据库物理文件存放位置
mysql> show global variables like "%datadir%";

常用的相关管理Shell命令

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

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

Thanks

CentOS7 通过YUM安装MySQL5.7

在mysql命令行下执行sql文件