6

Centos7.6下源码安装MySql8.0

 2 years ago
source link: https://blog.51cto.com/rabbit2013/5329203
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Centos7.6下源码安装MySql8.0

原创

fengfaling2013 2022-05-25 10:42:17 ©著作权

文章标签 mysql 数据库 初始化 文章分类 Linux 系统/运维 阅读数171

wget https://mirrors.aliyun.com/mysql/MySQL-8.0/mysql-8.0.27-el7-x86_64.tar.gz 下载源码

然后可以检查一下以前是否安装过MySQL:(如果有的话自行卸载)

rpm -qa | grep mysql marid

下一步安装相关依赖包:

yum install libaio

下一步在opt目录下解压安装包:

tar -zxvf mysql-8.0.27-el7-x86_64.tar.gz

为了方便安装将解压后的安装包更名为mysql

mv mysql-8.0.27-el7-x86_64 mysql 或软连接 ln -s mysql-8.0.27-el7-x86_6 /usr/local/mysql

这时的文件目录结构:

Centos7.6下源码安装MySql8.0_数据库

按照习惯,我们将文件移动到/usr/local目录下

mv mysql /usr/local

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

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

修改安装目录权限: 

chmod -R 777 /usr/local/mysql/
groupadd mysql

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

useradd -r -g mysql -s /bin/false mysql

将用户添加进组:

chown -R mysql:mysql ./

修改mysql配置文件

vi /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

进入安装目录进行安装:

cd /usr/local/mysql/bin/ 初始化数据库: ./mysqld --initialize --console

初始化数据库记住随机初始化的密码:

Centos7.6下源码安装MySql8.0_mysql_02

启动MySQL服务:

[root@localhost bin]# cd /usr/local/mysql/support-files/ 进入support-files
[root@localhost support-files]# ./mysql.server start 启动mysql

Starting MySQL.Logging to '/usr/local/mysql/mysqldb/localhost.localdomain.err'.  ERROR! The server quit without updating PID file (/usr/local/mysql/mysqldb/localhost.localdomain.pid).  如果启动失败

cd /usr/local       chown -R mysql.mysql mysql  修改一下目录属主权限

启动成功了。

[root@localhost mysql]# ./mysql.server start

Starting MySQL............ SUCCESS!

Centos7.6下源码安装MySql8.0_初始化_03

输入初始化的密码

修改密码:设置新密码为123456

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

设置允许远程登录:

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

#配置环境变量在使用目录都可以使用

[root@locallocal]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile

[root@locallocal]# source /etc/profile

在centos7中所有对服务的管理都集中到了systemctl当中,所以服务的启动、关闭、重启、开机启动等等的操作都可以用systemctl。

systemctl对服务的管理都是通过配置文件,配置文件的目录在/usr/lib/systemd/system/,默认情况下没有mysql的配置文件,那就手动添加一个名为mysql.service的配置文件。

在此之前先看一下mysql的安装目录:

whereis mysql

mysql: /usr/lib64/mysql /usr/local/mysql

/usr/local/mysql 这个就是我的安装路径

新建systemctl配置文件

vim /usr/lib/systemd/system/mysql.service

文件内容如下,记着ExecStart里的路径要跟你的mysql安装路径一致(mysqld路径)

[Unit]

1 Description=MySQL Server

2 Documentation=man:mysqld(8)

3 Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html

4 After=network.target

5 After=syslog.target

6 [Install]

7 WantedBy=multi-user.target

8 [Service]

9 User=mysql

10 Group=mysql

11 ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf

12 LimitNOFILE = 5000

13 #Restart=on-failure

14 #RestartPreventExitStatus=1

15 #PrivateTmp=false

最后设置开机启动:

systemctl enable mysql

查看防火墙

firewall-cmd --list-all

如果没有打开先打开:

   [root@locallocal]# systemctl start firewalld

   [root@locallocal]# firewall-cmd --list-all

在防火墙中将端口打开:

   [root@locallocal]# firewall-cmd --zone=public --add-port=3306/tcp --permanent

   [root@locallocal]# firewall-cmd --reload


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK