MySQL 是最流行的关系型数据库管理系统,在 WEB 应用方面 MySQL 是最好的 RDBMS(Relational Database Management System:关系数据库管理系统)应用软件之一。
软件项目 | 版本 | 备注 |
---|---|---|
操作系统 | CentOS Linux release 7.6.1810 (Core) | 服务器操作系统 |
MySQL | MySQL Community Server 5.7.39 | 数据库软件 |
安装之前查看端口,mysql默认使用的3306端口,如果3306端口已经占用启动过程中可能会报错,针对端口占用情况,可以停止占用端口的服务或者将mysql改用其他端口启动,停用和更改端口方法不再本文介绍。参考《Centos7防火墙设置》
查看端口
netstat -nltp | grep 3306
开具端口
firewall-cmd --zone=public --add-port=3306/tcp --permanent
查看是否已安装mysql或者mariadb,如果安装卸载
rpm -qa| grep mysql
rpm -qa| grep mariadb
mariadb-libs-5.5.68-1.el7.x86_64
Red Hat Enterprise/CentOS/Fedora/Debian 发行版默认会安装 MariaDB,MariaDB和MySQL数据库不兼容,如果需要安装MySQL数据库需要卸载MariaDB数据库。
卸载已安装得数据库
rpm -e --nodeps mariadb-libs-5.5.68-1.el7.x86_64
groupadd mysql
useradd -r -g mysql mysql
linux 系统安装MySQL数据库,有四种方式其中分别为:
cd /tmp
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.39-1.el7.x86_64.rpm-bundle.tar
解压下载的安装包
tar -xvf mysql-5.7.40-1.el7.x86_64.rpm-bundle.tar
注意安装顺序,安装顺应异常可能会出现依赖关系报错
rpm -ivh mysql-community-common-5.7.40-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-5.7.40-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-compat-5.7.40-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-5.7.40-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-5.7.40-1.el7.x86_64.rpm
使用mysql用户初始化MySQL数据库
mysqld --initialize --user=mysql
启动MySQL服务
systemctl start mysqld.service
查看MySQL服务
systemctl status mysqld.service
grep "password" /var/log/mysqld.log
2023-02-22T16:35:45.656262Z 1 [Note] A temporary password is generated for root@localhost: s%gkpu3yU7wY
1、登录账号
mysql -u root -p s%gkpu3yU7wY
2、重置密码
mysql>ALTER USER 'root'@'localhost' IDENTIFIED BY 'toor';
3、设置远程登录权限
mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'toor' WITH GRANT OPTION;
mysql>FLUSH PRIVILEGES;
服务器上新装的MySQL默认编码格式对中文支持不太友好,导致无法正常显示中文,因此需要重设默认编码。
1、查看数据库编码
登录数据库查看编码
mysql> status;
--------------
mysql Ver 14.14 Distrib 5.7.39, for Linux (x86_64) using EditLine wrapper
Connection id: 2
Current database:
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.7.40
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset:latin1
Db characterset:latin1
Client characterset:utf8
Conn. characterset:utf8
UNIX socket: /var/lib/mysql/mysql.sock
Uptime: 5 min 5 sec
Threads: 1 Questions: 8 Slow queries: 0 Opens: 113 Flush tables: 1 Open tables: 106 Queries per second avg: 0.026
--------------
2、修改编码
vi /etc/my.cnf
[client]
default-character-set=utf8
[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci
3、重启数据库
systemctl restart mysqld.service
4、验证编码
mysql> status;
--------------
mysql Ver 14.14 Distrib 5.7.39, for Linux (x86_64) using EditLine wrapper
Connection id: 2
Current database:
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.7.40
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset:utf8
Db characterset:utf8
Client characterset:utf8
Conn. characterset:utf8
UNIX socket: /var/lib/mysql/mysql.sock
Uptime: 5 min 5 sec
Threads: 1 Questions: 8 Slow queries: 0 Opens: 113 Flush tables: 1 Open tables: 106 Queries per second avg: 0.026
--------------
1、命令行登录数据库
mysql -u root -p
2、第三方工具登录