Linux 配置MySQL

1、检查是否安装过sql

[root@localhost ~]# rpm -qa | grep mysql

[root@localhost ~]# rpm -qa | grep mariadb

如果有则卸载:

[root@localhost ~]# sudo yum remove mariadb-libs-5.5.68-1.el7.x86_64
已加载插件:fastestmirror, langpacks
正在解决依赖关系
--> 正在检查事务
---> 软件包 mariadb-libs.x86_64.1.5.5.68-1.el7 将被 删除
--> 正在处理依赖关系 libmysqlclient.so.18()(64bit),它被软件包 2:postfix-2.10.1-9.el7.x86_64 需要
--> 正在处理依赖关系 libmysqlclient.so.18(libmysqlclient_18)(64bit),它被软件包 2:postfix-2.10.1-9.el7.x86_64 需要
--> 正在检查事务
---> 软件包 postfix.x86_64.2.2.10.1-9.el7 将被 删除
--> 解决依赖关系完成

依赖关系解决

=======================================================================================
 Package              架构           版本                      源                 大小
=======================================================================================
正在删除:
 mariadb-libs         x86_64         1:5.5.68-1.el7            @anaconda         4.4 M
为依赖而移除:
 postfix              x86_64         2:2.10.1-9.el7            @anaconda          12 M

事务概要
=======================================================================================
移除  1 软件包 (+1 依赖软件包)

安装大小:17 M
是否继续?[y/N]:y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在删除    : 2:postfix-2.10.1-9.el7.x86_64                                      1/2
  正在删除    : 1:mariadb-libs-5.5.68-1.el7.x86_64                                 2/2
  验证中      : 1:mariadb-libs-5.5.68-1.el7.x86_64                                 1/2
  验证中      : 2:postfix-2.10.1-9.el7.x86_64                                      2/2

删除:
  mariadb-libs.x86_64 1:5.5.68-1.el7                                                   

作为依赖被删除:
  postfix.x86_64 2:2.10.1-9.el7                                                        

完毕!

检查是否卸载干净:

[root@localhost ~]# rpm -qa | grep mysql

[root@localhost ~]# rpm -qa | grep mariadb

2、下载并上传MySQL压缩包

官网地址:MySQL :: Download MySQL Community Server

Linux 配置MySQL_第1张图片

 上传至 /usr/local 目录下,解压并重命名:

Linux 配置MySQL_第2张图片

Linux 配置MySQL_第3张图片

创建数据库文件存放的文件夹。这个文件夹将来存放每个数据库的库文件 :

[root@localhost local]# cd mysql
[root@localhost mysql]# mkdir mysqldb 

3、赋予myql文件权限

执行 chmod -R 777 /usr/local/mysql/ 命令:

[root@localhost ~ ]# chmod -R 777 /usr/local/mysql/

Linux 配置MySQL_第4张图片

 4、创建mysql组和用户

# 创建组

[root@centos7 mysql]# groupadd mysql

# 创建用户(-s /bin/false参数指定mysql用户仅拥有所有权,而没有登录权限)

[root@centos7 mysql]# useradd -r -g mysql -s /bin/false mysql

# 将用户添加到组中

[root@centos7 mysql]# chown -R mysql:mysql ./

Linux 配置MySQL_第5张图片

5、创建mysql配置文件

[root@localhost ~]# vi usr/local/mysql/etc/my.cnf

添加以下内容:

[mysqld]
# 设置3306端口
port=3306
# 设置mysql的安装目录
basedir=/usr/local/mysql
# 设置mysql数据库的数据的存放目录
datadir=/usr/local/mysql/mysqldb
# 允许最大连接数
max_connections=10000
# 允许连接失败的次数。这是为了防止有人从该主机试图攻击数据库系统
max_connect_errors=10
# 服务端使用的字符集默认为UTF8
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
# 默认使用“mysql_native_password”插件认证
default_authentication_plugin=mysql_native_password
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8
[client]
# 设置mysql客户端连接服务端时默认使用的端口
port=3306
default-character-set=utf8

6、安装mysql

进入mysql 安装目录下:

[root@localhost ~]# cd /usr/local/mysql/bin/

安装mysql,并记住初始化随机密码:

[root@localhost bin]# ./mysqld --initialize --console
2022-08-08T17:36:53.599341Z 0 [Warning] [MY-010918] [Server] 'default_authentication_plugin' is deprecated and will be removed in a future release. Please use authentication_policy instead.
2022-08-08T17:36:53.599352Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.30) initializing of server in progress as process 5176
2022-08-08T17:36:53.600411Z 0 [Warning] [MY-013242] [Server] --character-set-server: 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous.
2022-08-08T17:36:53.605819Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2022-08-08T17:36:53.849746Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2022-08-08T17:36:54.639630Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: tvrK&0h*Ip<:

7、启动mysql

更改配置文件 my.cnf 权限:

[root@localhost ~]# chmod -R 644 /usr/local/mysql/etc/my.cnf

进入mysql.server服务目录下并启动服务:

[root@localhost ~]# cd /usr/local/mysql/support-files
[root@localhost support-files]# ./mysql.server start

第一次启动会报错:

重新给mysql安装目录赋予一下权限后,再次执行即可:

[root@localhost support-files]# chmod -R 777 /usr/local/mysql

启动成功:

[root@localhost support-files]# ./mysql.server start
my_print_defaults: [Warning] World-writable config file '/usr/local/mysql/etc/my.cnf' is ignored.
Starting MySQL.my_print_defaults: [Warning] World-writable config file '/usr/local/mysql/etc/my.cnf' is ignored.
my_print_defaults: [Warning] World-writable config file '/usr/local/mysql/etc/my.cnf' is ignored.
Logging to '/usr/local/mysql/data/localhost.localdomain.err'.
 ERROR! The server quit without updating PID file (/usr/local/mysql/data/localhost.localdomain.pid).
[root@localhost support-files]# chmod -R 644 /usr/local/mysql/etc/my.cnf
[root@localhost support-files]# ./mysql.server start
Starting MySQL. SUCCESS!

[root@localhost support-files]#

8、将mysql添加到系统进程中

 [root@localhost bin]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

9、设置mysql自启动

[root@localhost ~]# chmod +x /etc/init.d/mysqld
[root@localhost ~]# systemctl enable mysqld

Linux 配置MySQL_第6张图片

设置成功。

10、登录mysql

[root@localhost ~]# cd /usr/local/mysql/bin/
[root@localhost bin]# ./mysql -u root -p

输入一开始的随机密码进入mysql:

Linux 配置MySQL_第7张图片

11、修改root用户登录密码

mysql> alter user 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'mypassword';

12、设置允许远程登录

mysql> use mysql
mysql> update user set user.Host='%'where user.User='root';
mysql> flush privileges;
mysql> quit

Linux 配置MySQL_第8张图片

13、重启服务

[root@localhost ~]# systemctl restart mysql    
[root@localhost ~]# service mysql restart

查看mysql运行状态:

[root@localhost ~]# systemctl status mysql

Linux 配置MySQL_第9张图片

14、开放端口3306

15、用navicat测试连接

Linux 配置MySQL_第10张图片

你可能感兴趣的:(高级框架,mysql,linux,数据库)