数据库-Linux中MySQL的安装

1、安装MySQL

1、准备

一台CentOS-7的虚拟机
下载MySQL3.7.41的压缩包
上传到虚拟机

2、安装依赖、解压、移动

[root@mysql_server ~]# yum  install cmake ncurses-devel gcc gcc-c++ vim lsof bzip2 openssl-devel ncurses-compat-libs -y
[root@mysql_server ~]# tar xf mysql-5.7.41-linux-glibc2.12-x86_64.tar.gz 
[root@mysql_server ~]# ls
anaconda-ks.cfg  mysql-5.7.41-linux-glibc2.12-x86_64  mysql-5.7.41-linux-glibc2.12-x86_64.tar.gz
[root@mysql_server ~]# mv mysql-5.7.41-linux-glibc2.12-x86_64 /usr/local/mysql
[root@mysql_server ~]# cd /usr/local
[root@mysql_server local]# ls
bin  etc  games  include  lib  lib64  libexec  mysql  sbin  share  src
[root@mysql_server local]# cd mysql/
[root@mysql_server mysql]# ls
bin  docs  include  lib  LICENSE  man  README  share  support-files

MySQL中的文件:

bin :mysql服务器、客户端和实用程序
docs :信息格式的mysql手册
man :Unix手册
include :包含(头)文件
lib :图书馆(库)
share :用于数据库安装的错误信息、字典、SQL(Error messages, dictionary, and SQL for database installation)
support-files :其他支持文件

3、建立MySQL的组和用户、关闭防火墙与selinux

[root@mysql_server mysql]# groupadd mysql
[root@mysql_server mysql]# useradd -r -g mysql -s /bin/false mysql
[root@mysql_server mysql]# service firewalld stop 
Redirecting to /bin/systemctl stop firewalld.service
永久:
[root@mysql_server mysql]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@mysql_server mysql]# setenforce 0
永久:
[root@mysql_server mysql]# sed -i '/^SELINUX=/ s/enforcing/disabled/' /etc/selinux/config 

4、新建存放数据的目录、设置权限

[root@mysql_server mysql]# mkdir /data/mysql -p
[root@mysql_server mysql]# chown mysql:mysql /data/mysql/
[root@mysql_server mysql]# chmod 750 /data/mysql

5、初始化mysql、设置PATH

[root@mysql_server mysql]# cd /usr/local/mysql/bin
[root@mysql_server bin]# ls
innochecksum    my_print_defaults           mysql_config         mysqldump         mysqlpump                  mysql_tzinfo_to_sql  resolve_stack_dump
lz4_decompress  mysql                       mysql_config_editor  mysqldumpslow     mysql_secure_installation  mysql_upgrade        zlib_decompress
myisamchk       mysqladmin                  mysqld               mysql_embedded    mysqlshow                  mysqlxtest
myisam_ftdump   mysqlbinlog                 mysqld-debug         mysqlimport       mysqlslap                  perror
myisamlog       mysqlcheck                  mysqld_multi         mysql_install_db  mysql_ssl_rsa_setup        replace
myisampack      mysql_client_test_embedded  mysqld_safe          mysql_plugin      mysqltest_embedded         resolveip

执行mysqld并记录密码:
[root@mysql_server bin]# ./mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql &>passwd.txt
[root@mysql_server bin]# cat passwd.txt 
2023-07-06T12:31:03.040618Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2023-07-06T12:31:03.281735Z 0 [Warning] InnoDB: New log files created, LSN=45790
2023-07-06T12:31:03.327110Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2023-07-06T12:31:03.428998Z 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: f8c0d2c9-1bf8-11ee-bddd-000c29ab69a4.
2023-07-06T12:31:03.429464Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2023-07-06T12:31:03.574816Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2023-07-06T12:31:03.574827Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2023-07-06T12:31:03.575625Z 0 [Warning] CA certificate ca.pem is self signed.
2023-07-06T12:31:03.602765Z 1 [Note] A temporary password is generated for root@localhost: uY1OaVCSqx&4

允许ssl远程登录
[root@mysql_server bin]# ./mysql_ssl_rsa_setup --datadir=/data/mysql/
配置PATH变量,Server服务:
[root@mysql_server bin]# export PATH=/usr/local/mysql/bin/:$PATH
[root@mysql_server bin]# echo 'PATH=/usr/local/mysql/bin/:$PATH' >>/root/.bashrc 
[root@mysql_server bin]# cp ../support-files/mysql.server /etc/init.d/mysqld

6、复制mysql.server并修改

因为我们的数据文件存放目录是自己建立的,所以我们要修改一下/etc/init.d/mysqld

[root@mysql_server bin]# cp ../support-files/mysql.server /etc/init.d/mysqld
[root@mysql_server bin]# 
[root@mysql_server bin]# vim /etc/init.d/mysqld 
 67   bindir=/usr/local/mysql/bin
 68   if test -z "$datadir"
 69   then
 70 datadir=/data/mysql
替换第70行:
[root@mysql_server bin]# sed -i '70c datadir=/data/mysql' /etc/init.d/mysqld 

7、生成etc/my.cnf配置文件

[root@mysql_server bin]# cat >/etc/my.cnf  <<EOF
> [mysqld_safe]
> 
> [client]
> socket=/data/mysql/mysql.sock
> 
> [mysqld]
> socket=/data/mysql/mysql.sock
> port = 3306
> open_files_limit = 8192
> innodb_buffer_pool_size = 512M
> character-set-server=utf8
> 
> [mysql]
> auto-rehash
> prompt=\\u@\\d \\R:\\m  mysql>
> EOF
[root@mysql_server bin]# cat /etc/my.cnf
[mysqld_safe]

[client]
socket=/data/mysql/mysql.sock

[mysqld]
socket=/data/mysql/mysql.sock
port = 3306
open_files_limit = 8192
innodb_buffer_pool_size = 512M
character-set-server=utf8

[mysql]
auto-rehash
prompt=\u@\d \R:\m  mysql>

8、设置mysql服务并启动

设置内核参数,将mysqld添加到linux服务管理中,并设置开机自启

[root@mysql_server bin]# ulimit -n 1000000
[root@mysql_server bin]# echo "ulimit -n 1000000" >>/etc/rc.local
[root@mysql_server bin]# chmod +x /etc/rc.d/rc.local
[root@mysql_server bin]# /sbin/chkconfig --add mysqld
[root@mysql_server bin]# /sbin/chkconfig mysqld on
[root@mysql_server bin]# service mysqld start
Starting MySQL.Logging to '/data/mysql/mysql_server.err'.
. SUCCESS! 

9、登录mysql修改密码

密码是前面的临时密码

[root@mysql_server bin]# mysql -uroot -p"uY1OaVCSqx&4"
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.41

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

root@(none) 20:02  mysql>set password = 'Sc@123456';
Query OK, 0 rows affected (0.00 sec)

root@(none) 20:04  mysql>exit
Bye

验证一下:
[root@mysql_server bin]# mysql -uroot -p"Sc@123456"
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.41 MySQL Community Server (GPL)

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

root@(none) 20:11  mysql>show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

root@(none) 20:11  mysql>

在Linux-CentOS7中二进制安装完成。

2、直接脚本安装

因为下载太慢,这里直接解压没有下载压缩包,直接用xftp上传的
你可以在脚本中使用wget命令从MySQL官网下载MySQL 5.7.41的安装包。
添加:

wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.41-linux-glibc2.12-x86_64.tar.gz

#!/bin/bash

#解决软件的依赖关系
yum  install cmake ncurses-devel gcc  gcc-c++  vim  lsof bzip2 openssl-devel ncurses-compat-libs -y
#解压mysql二进制安装包
tar  xf  mysql-5.7.41-linux-glibc2.12-x86_64.tar.gz
#将解压后的文件夹移动到/usr/local下改名叫mysql
mv mysql-5.7.41-linux-glibc2.12-x86_64 /usr/local/mysql

#新建组和用户 mysql
groupadd mysql
#mysql这个用户的shell 是/bin/false 属于mysql组 是系统用户只用于启动
useradd -r -g mysql -s /bin/false mysql

#关闭firewalld防火墙服务,并且设置开机不要启动
service firewalld stop
systemctl  disable  firewalld
#临时关闭selinux
setenforce 0
#永久关闭selinux
sed -i '/^SELINUX=/ s/enforcing/disabled/'  /etc/selinux/config

#新建存放数据的目录
mkdir  /data/mysql -p
#修改/data/mysql目录的权限归mysql用户和mysql组所有,这样mysql用户可以对这个文件夹进行读写了
chown mysql:mysql /data/mysql/
#只是允许mysql这个用户和mysql组可以访问,其他人都不能访问
chmod 750 /data/mysql/
#进入/usr/local/mysql/bin目录
cd /usr/local/mysql/bin/

#初始化mysql
./mysqld  --initialize --user=mysql --basedir=/usr/local/mysql/  --datadir=/data/mysql  &>passwd.txt
#让mysql支持ssl方式登录的设置
./mysql_ssl_rsa_setup --datadir=/data/mysql/
#获得临时密码
tem_passwd=$(cat passwd.txt |grep "temporary"|awk '{print $NF}')
  #$NF表示最后一个字段
  # abc=$(命令)  优先执行命令,然后将结果赋值给abc 


#修改PATH变量,加入mysql bin目录的路径
#临时修改PATH变量的值
export PATH=/usr/local/mysql/bin/:$PATH
#重新启动linux系统后也生效,永久修改
echo  'PATH=/usr/local/mysql/bin:$PATH' >>/root/.bashrc

#复制support-files里的mysql.server文件到/etc/init.d/目录下叫mysqld
cp  ../support-files/mysql.server   /etc/init.d/mysqld
#修改/etc/init.d/mysqld脚本文件里的datadir目录的值
sed  -i '70c  datadir=/data/mysql'  /etc/init.d/mysqld

#生成/etc/my.cnf配置文件
cat  >/etc/my.cnf  <<EOF
[mysqld_safe]

[client]
socket=/data/mysql/mysql.sock

[mysqld]
socket=/data/mysql/mysql.sock
port = 3306
open_files_limit = 8192
innodb_buffer_pool_size = 512M
character-set-server=utf8

[mysql]
auto-rehash
prompt=\\u@\\d \\R:\\m  mysql>
EOF

#修改内核的open file的数量
ulimit -n 1000000
#设置开机启动的时候也配置生效
echo "ulimit -n 1000000" >>/etc/rc.local
chmod +x /etc/rc.d/rc.local
#将mysqld添加到linux系统里服务管理名单里
/sbin/chkconfig --add mysqld
#设置mysqld服务开机启动
/sbin/chkconfig mysqld on
#启动mysqld进程
service mysqld start


#初次修改密码需要使用--connect-expired-password 选项
#-e 后面接的表示是在mysql里需要执行命令  execute 执行
mysql -uroot -p$tem_passwd --connect-expired-password   -e  "set password='Sc@123456';"
#检验上一步修改密码是否成功,如果有输出能看到mysql里的数据库,说明成功。
mysql -uroot -p'Sc@123456'  -e "show databases;"

运行一下脚本验证效果:

[root@mysql_server ~]# bash install_mysql.sh 
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
epel/x86_64/metalink                                                                                                                                | 6.8 kB  00:00:00     
 * base: mirrors.ustc.edu.cn
 * epel: mirror-hnd.yuki.net.uk
 * extras: mirrors.ustc.edu.cn
 * updates: mirrors.ustc.edu.cn
base                                                                                                                                                | 3.6 kB  00:00:00     
epel                                                                                                                                                | 4.7 kB  00:00:00     
extras                                                                                                                                              | 2.9 kB  00:00:00     
updates                                                                                                                                             | 2.9 kB  00:00:00     
(1/4): extras/7/x86_64/primary_db                                                                                                                   | 250 kB  00:00:00     
(2/4): updates/7/x86_64/primary_db                                                                                                                  |  22 MB  00:00:15     
(3/4): epel/x86_64/updateinfo                                                                                                                       | 1.0 MB  00:01:06     
(4/4): epel/x86_64/primary_db                                                                                                                       | 7.0 MB  00:05:13     
软件包 cmake-2.8.12.2-2.el7.x86_64 已安装并且是最新版本
软件包 ncurses-devel-5.9-14.20130511.el7_4.x86_64 已安装并且是最新版本
软件包 gcc-4.8.5-44.el7.x86_64 已安装并且是最新版本
软件包 gcc-c++-4.8.5-44.el7.x86_64 已安装并且是最新版本
软件包 2:vim-enhanced-7.4.629-8.el7_9.x86_64 已安装并且是最新版本
软件包 lsof-4.87-6.el7.x86_64 已安装并且是最新版本
软件包 bzip2-1.0.6-13.el7.x86_64 已安装并且是最新版本
软件包 1:openssl-devel-1.0.2k-26.el7_9.x86_64 已安装并且是最新版本
没有可用软件包 ncurses-compat-libs。
无须任何处理
Redirecting to /bin/systemctl stop firewalld.service
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
Starting MySQL.Logging to '/data/mysql/mysql_server.err'.
 SUCCESS! 
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+

需要重新登录root刷新一下服务,不然可能会找不到mysql命令

[root@mysql_server ~]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@mysql_server ~]# mysql -uroot -p'Sc@123456'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.41 MySQL Community Server (GPL)

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

root@(none) 21:44  mysql>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值