一、MySQL YUM 源
官方地址:https://dev.mysql.com/downloads/repo/yum/
在Linux终端机器上进行下载
wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
如果需要升级包可以用下面的命令
rpm -Uvh mysql80-community-release-el7-3.noarch.rpm
二、安装需要的版本
查看所有版本
yum repolist all | grep mysql
这里5.7版本是不可用的 8.0的能用 所以需要先禁用8.0的然后开启5.7版本的
禁用8.0命令
yum-config-manager --disable mysql80-community
#注意#:
如果运行 yum-config-manager命令提示 -bash: yum-config-manager: command not found 命令未找到,因为 yum-config-manager 在 yum-utils包里面。由于系统默认没有这个命令,需要另外进行安装
安装yum-config-manager
yum -y install yum-utils
如果报错如下:
The GPG keys listed for the “MySQL 5.7 Community Server” repository are already installed but they are not correct for this package.
Check that the correct key URLs are configured for this repository
这是由于MySQL GPG密钥已过期导致的 以2022年为例 执行以下命令:
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
启用5.7版本
yum-config-manager --enable mysql57-community
三、安装MySQL
yum install -y mysql-community-server
验证MySQL
mysql --version
四、启动MySQL
查看启动状态
systemctl status mysqld.service
启动
systemctl start mysqld.service
登录MySQL
MySql服务器初始化会创建 'root@localhost’的一个超级用户账号。设置超级用户的密码并将其存储在错误日志文件中
grep 'temporary password' /var/log/mysqld.log
使用临时密码登录Mysql
mysql -uroot -p'nYZWlu&Qr4S('
重置密码
ALTER USER 'root'@'localhost' IDENTIFIED BY 'ggxl886...';
如果密码设置的太简单可能会报错,如下:
解决方法:
set global validate_password_length=6;
set global validate_password_policy=0;
show variables like "%validate%";
五、开启远程连接
进入mysql库执行命令
use mysql
select host ,user from user;
可以看到root用户的host是localhost,localhost 表示只能进行本地访问,不能进行远程连接
修改权限
把root用户的host改为%
update user set host = "%" where user = "root";
刷新权限
flush privileges;
测试连接
六、MySQL相关服务命令
启动mysql服务:systemctl start mysqld.service
停止mysql服务:systemctl stop mysqld.service
重启mysql服务:systemctl restart mysqld.service
查看mysql服务当前状态:systemctl status mysqld.service
设置mysql服务开机自启动:systemctl enable mysqld.service
停止mysql服务开机自启动:systemctl disable mysqld.service
七、MySQL数据库及表编码
-
查看数据库编码格式
show variables like 'character_set_database';
-
查看数据表的编码格式
show create table <表名>;
-
创建数据库时指定数据库的字符集
create database <数据库名> character set utf8;
-
创建数据表时指定数据表的编码格式
create table test( name varchar(20) not null, id int not null, age int not null ) default charset = utf8;
-
修改数据库的编码格式
alter database <数据库名> character set utf8;
-
修改数据表格编码格式
alter table <表名> character set utf8;
-
修改字段编码格式
alter table <表名> change <字段名> <字段名> <类型> character set utf8; alter table test change name name2 varchar(20) character set utf8 not null;
八、MySQL开启binlog
- 登录mysql查看binlog状态
show variables like '%log_bin%';
2. 创建目录并设置权限
mkdir -p /data/mysql/log_bin
chown -R mysql:mysql /data/mysql
chmod -R 755 /data/mysql
3. 修改mysql配置文件my.cnf
server-id=1
log_bin=/data/mysql/log_bin/mysql_bin
binlog_format=ROW
- 重启mysql
systemctl restart mysqld.service
- 查看binlog日志目录
- 验证是否开启