linux-mysql在centos7安装和基础配置
1.安装mysql数据库
1.使用官网安装
1.检查是否存在mysql的分支mariadb
[root@localhost ~]# rpm -qa |grep mariadb
mariadb-libs-5.5.64-1.el7.x86_64
[root@localhost ~]#
2.卸载这个分支包
[root@localhost ~]# rpm -qa | grep mariadb
mariadb-libs-5.5.64-1.el7.x86_64
[root@localhost ~]# rpm -e --nodeps mariadb-libs-5.5.64-1.el7.x86_64
[root@localhost ~]# rpm -qa | grep mariadb
[root@localhost ~]#
3..进入mysql官网的这个目录
4.使用wget下载
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-8.0.30-el7-x86_64.tar
5.拆包解压安装
移动到/usr/local下
6.创建存储数据库文件的文件夹和数据库的用户
[root@localhost mysql]# pwd
/usr/local/mysql
[root@localhost mysql]# mkdir data
[root@localhost mysql]# useradd -s /sbin/nologin mysql
[root@localhost mysql]# chown mysql:mysql ./data/
[root@localhost mysql]#
7.初始化数据库
[root@localhost bin]# pwd
/usr/local/mysql/bin
[root@localhost bin]# ./mysqld --initialize --user=mysql --datadir=/usr/local/mysql/data --basedir=/usr/local/mysql
2025-01-22T06:34:16.421063Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
2025-01-22T06:34:16.421129Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.30) initializing of server in progress as process 2389
2025-01-22T06:34:16.435853Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2025-01-22T06:34:17.399675Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2025-01-22T06:34:18.255920Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: yzyn%taJ#9B?
[root@localhost bin]#
这里有一个初始化密码 yzyn%taJ#9B?
8.配置文件路径
bin data docs include lib LICENSE man README share support-files
[root@localhost mysql]# find / -name my.cnf
/etc/my.cnf
9.修改配置文件如下即可
[client]
port = 3306
socket = /usr/local/mysql/data/mysql.sock
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
[mysqld]
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_general_ci
init_connect = 'SET NAMES utf8mb4'
port = 3306
socket = /usr/local/mysql/data/mysql.sock
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
datadir = /usr/local/mysql/data
#lower_case_table_names=1
#如果要设置lower_case_table_names可以在初始化里面设置 ./mysqld --initialize --user=mysql --datadir=/usr/local/mysql/data --basedir=/usr/local/mysql --lower_case_table_names=1
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
10.然后开启数据库
添加到环境变量中,然后使用的时候就直接mysql --version即可,好使用
小心使用,容易把系统命令搞崩溃
SUCCESS!
[root@localhost support-files]# pwd
/usr/local/mysql/support-files
[root@localhost support-files]# PATH=$PAHT:/usr/local/mysql/bin/
[root@localhost support-files]# mysql --version
mysql Ver 8.0.30 for Linux on x86_64 (MySQL Community Server - GPL)
2.最简便的下载
wget https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm
cd /etc/yum.repos.d/
rpm -ivh mysql57-community-release-el7-9.noarch.rpm
yum install mysql-server
如果报错
mysql-community-libs-compat-5.7.37-1.el7.x86_64.rpm 的公钥尚未安装
失败的软件包是:mysql-community-libs-compat-5.7.37-1.el7.x86_64
GPG 密钥配置为:file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
那么就执行
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
再次安装
yum install mysql-server
然后开启使用
3.第三种安装方法,使用rpm安装
使用wget下载,不同版本只需要修改一下参数即可
[root@localhost src]# wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.41-1.el7.x86_64.rpm-bundle.tar
1.解压:
[root@localhost src]# ls
mysql-8.0.41-1.el7.x86_64.rpm-bundle.tar
[root@localhost src]# tar xvf mysql-8.0.41-1.el7.x86_64.rpm-bundle.tar
mysql-community-client-8.0.41-1.el7.x86_64.rpm
mysql-community-client-plugins-8.0.41-1.el7.x86_64.rpm
mysql-community-common-8.0.41-1.el7.x86_64.rpm
mysql-community-debuginfo-8.0.41-1.el7.x86_64.rpm
mysql-community-devel-8.0.41-1.el7.x86_64.rpm
mysql-community-embedded-compat-8.0.41-1.el7.x86_64.rpm
mysql-community-icu-data-files-8.0.41-1.el7.x86_64.rpm
mysql-community-libs-8.0.41-1.el7.x86_64.rpm
mysql-community-libs-compat-8.0.41-1.el7.x86_64.rpm
mysql-community-server-8.0.41-1.el7.x86_64.rpm
mysql-community-server-debug-8.0.41-1.el7.x86_64.rpm
mysql-community-test-8.0.41-1.el7.x86_64.rpm
[root@localhost src]#
2.安装
rpm -ivh mysql-community-common-8.0.41-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-plugins-8.0.41-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-8.0.41-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-8.0.41-1.el7.x86_64.rpm
rpm -ivh mysql-community-icu-data-files-8.0.41-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-8.0.41-1.el7.x86_64.rpm
3.初始化数据库
[root@localhost src]# mysqld --version
/usr/sbin/mysqld Ver 8.0.41 for Linux on x86_64 (MySQL Community Server - GPL)
[root@localhost src]# mysqld --initialize --console
[root@localhost src]#
4.修改文件
以下文件和/etc/passwd中的用户都是通过rpm安装的时候生成的
这里的/var/lib/mysql是数据库的数据存储目录,修改以下它的属主和数组为mysql用户
[root@localhost src]# chown -R mysql:mysql /var/lib/mysql
[root@localhost src]#
接下来我们就可以开启使用了,我们进行使用需要密码,然而我们在初始化的时候它会给我们生成一个密码,我们通过日志文件来获取密码
密码如下,使用即可
2.数据库的基本使用
1.修改密码
在mysql中修改mysql的密码
alter user 'root'@'localhost' identified by '123456';
set password for root@localhost = '123456';
2.数据库的基本操作
1.查看当前的库
2.使用mysql库,并且查表
3.查看某个表中的所有字段
or
4.查看当前用户
5.创建一个名为db1的 数据库
6.创建一个表
7.查看数据库版本
8.查看当前数据库状态
9.查看mysql参数
10.修改参数
11.查看服务器队列
这个在日常的管理工作中使用最为频繁,因为使用它可以查看当前mysql在干什么,可以发现是否有锁表: