Linux 安装 MySQL 5.7.27 教程

1. 检查当前系统是否安装过mysql

[root@yum ~]# rpm -qa|grep mariadb
mariadb-libs-5.5.68-1.el7.x86_64 #已经存在
# 存在则先卸载
[root@yum ~]# rpm -e --nodeps  mariadb-libs

2. 检查当前mysql依赖环境

[root@yum ~]# rpm -qa|grep libaio
libaio-0.3.109-13.el7.x86_64 #已经存在

[root@yum ~]# rpm -qa|grep net-tools
#不存在 net-tools 则安装
[root@yum ~]# yum -y install net-tools
[root@yum ~]# rpm -qa|grep net-tools
net-tools-2.0-0.25.20131004git.el7.x86_64 #已经存在

3. 上传 mysql 安装包到 Linux 的 /root/ apps/mysql目录下并解压

[root@yum ~]# mkdir /root/apps/mysql
[root@yum ~]# tar -xvf mysql-5.7.27-1.el7.x86_64.rpm-bundle.tar -C /root/apps/mysql

4. 在mysql 的安装目录下执行:(必须按照顺序执行)

[root@yum ~]# cd /root/apps/mysql/
[root@yum mysql]# rpm -ivh mysql-community-common-5.7.27-1.el7.x86_64.rpm
[root@yum mysql]# rpm -ivh mysql-community-libs-5.7.27-1.el7.x86_64.rpm
[root@yum mysql]# rpm -ivh mysql-community-client-5.7.27-1.el7.x86_64.rpm
[root@yum mysql]# rpm -ivh mysql-community-server-5.7.27-1.el7.x86_64.rpm

5. 查看mysql 安装版本

[root@yum mysql]# mysqladmin --version
mysqladmin  Ver 8.42 Distrib 5.7.27, for Linux on x86_64

6. mysql 服务初始化

为了保证数据库目录为与文件的所有者为 mysql 登陆用户,如果你是以 root 身份运行 mysql 服务,需要执行下面的命令初始化

[root@yum ~]# mysqld --initialize --user=mysql
#查看初始化密码
[root@yum ~]# cat /var/log/mysqld.log
2021-06-06T14:08:09.011209Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-06-06T14:08:09.223359Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-06-06T14:08:09.251594Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-06-06T14:08:09.314410Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 9f4e6310-c6d0-11eb-b791-000c29e525a3.
2021-06-06T14:08:09.321278Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
#root@localhost后面就是初始化密码:oyiNZ7(9q<q*
2021-06-06T14:08:09.321927Z 1 [Note] A temporary password is generated for root@localhost: oyiNZ7(9q<q* 

注意:另外 --initialize 选项默认以“安全”模式来初始化,则会为 root 用户生成一个密码并将该密码标记为过期,登陆后你需要设置一个新的密码。

7. 启动 mysql 服务

#启动mysql服务
[root@yum ~]# systemctl start mysqld.service
#设置开机启动mysql服务
[root@yum ~]# systemctl enable mysqld.service

停止mysql服务器:#systemctl stop mysqld.service

8. 首次登录 mysql 和修改初始密码

[root@yum ~]# mysql -uroot -p
Enter password: #这里输入初始化密码  
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 2
Server version: 5.7.27
mysql>

#这里的初始化密码默认是过期的,查看数据库会报错如下:
mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

#修改密码(这里密码改为了 root)
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'root';
Query OK, 0 rows affected (0.00 sec)

设置完密码就可以用新密码登陆,正常使用数据库了

9. 修改字符集问题

mysql5.7 默认不支持中文,所以直接插入中文数据报错

[root@yum ~]# vim  /etc/my.cnf
#行尾添加
character_set_server=utf8

#重新启动mysql服务使修改配置生效
[root@yum ~]# systemctl restart mysqld

扩展:已生成的库表字符集如何变更

#修改数据库的字符集
mysql> alter database mydb character set 'utf8';

#修改数据表的字符集
mysql> alter table tableName convert to  character set 'utf8';

10. 远程工具连接 MySQL 数据库

mysql 数据库默认只能本地访问,远程工具连接 mysql 需要授予远程访问权限

[root@yum ~]# mysql -uroot -p
Enter password: #这是输入修改后的密码root
mysql>
#授予通过网络方式登录的的root用户 ,对所有库所有表的全部权限,密码设为 root
mysql> grant all privileges on *.* to root@'%'  identified by 'root';
Query OK, 0 rows affected, 1 warning (0.00 sec)

接着在 windows 系统下使用 Navicat 工具远程连接 Linux 的 MySQL 即可。

作者:Binge
本文版权归作者和博客园共有,转载必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/binbingg/p/14856752.html