linux环境下安装mysql
linux环境下安装mysql压缩包,MySQL8
创建 /usr/local/mysql
mkdir -p /usr/local/mysql
将下载好的mysql压缩包放到/usr/local/mysql
cp /mysql/mysql-8.0.33-linux-glibc2.12-x86_64.tar.xz /usr/local/mysql
解压压缩包
#解压.gz文件tar xvf firenametar zxvf firename#解压.zip文件unzip firenameunzip -d /nginx/ firename#解压tar.xz文件tar xvJf ***.tar.xz
解压至/usr/local/mysql中
tar -xvJf/usr/local/mysql/mysql-8.0.33-linux-glibc2.12-x86_64.tar.xz -C /usr/local/mysql
修改文件夹名称
mv /usr/local/mysql/mysql-8.0.33-linux-glibc2.12-x86_64/usr/local/mysql/
创建mysql用户组
groupadd mysql
添加mysql用户
useradd -g mysql mysql
授权用户
chown -R mysql.mysql /usr/local/mysql
新建 /etc/my.cnf
vim /etc/my.cnf
[client]default-character-set=utf8mb4#password = your_passwordport= 3306 socket= /usr/local/mysql/data/mysql.sock# The MySQL server [mysqld]#配置mysql的文件夹 和 mysql data目录basedir=/usr/local/mysqldatadir=/usr/local/mysql/datadefault-storage-engine=INNODBcharacter-set-server=utf8mb4collation-server=utf8mb4_general_ciport= 3306 socket= /usr/local/mysql/data/mysql.sockskip-external-lockingkey_buffer_size = 16Kmax_allowed_packet = 64Mtable_open_cache = 4 sort_buffer_size = 64Kread_buffer_size = 256Kread_rnd_buffer_size = 256Knet_buffer_length = 2Kthread_stack = 128Kbind-address = 0.0.0.0server-id = 1 # Uncomment the following if you want to log updates#log-bin=mysql-bin# binary logging format - mixed recommended#binlog_format=mixed[mysqldump]quickmax_allowed_packet = 64M[mysql]default-character-set = utf8mb4no-auto-rehash# Remove the next comment character if you are not familiar with SQL#safe-updates[myisamchk]key_buffer_size = 8Msort_buffer_size = 8M[mysqlhotcopy]interactive-timeout
初始化基础信息,最后一行后面会有个随机的初始密码保存下来一会登录要用(如果忘记了就删掉data重新初始化)
cd /usr/local/mysql/bin/./mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
这里如果报错
./mysqld –initialize –user=mysql –basedir=/usr/local/mysql –datadir=/usr/local/mysql/data#是因为没有安装链接库,出现该问题首先检查该链接库文件有没有安装使用
rpm -qa|grep libaio
命令进行核查运行该命令后发现系统中无该链接库文件使用命令,安装—-》
#执行如下命令后重新初始化即可解决
yum install -y libaio
#初始化之后将出现如下信息#记住这个默认密码 Pt6e(#znv!id2023-05-12T03:50:36.802236Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.2023-05-12T03:50:38.958183Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.2023-05-12T03:50:40.554667Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: Pt6e(#znv!id
将mysql命令添加到服务
#将mysql命令添加到服务ln -s /usr/local/mysql/bin/mysql /usr/bin#配置mysql service启动cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
启动数据库
ps -ef |grep mysql #检查数据库是否启动service mysql start #启动MySQL
连接数据库
#bin目录下/usr/local/mysql/bin./mysql -uroot -p**密码就是刚才记得默认密码**
修改数据库密码
#登录进入数据库后执行如下命令ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';#然后再执行刷新flush privileges;
开放外网可以访问(如果不设置 用可视化工具连接数据库会报 1103错误)
#use mysqlupdate user set host='%' where user='root';#刷新flush privileges;
到此数据库安装步骤完成,下面是扩展
*********========================================
*********========================================
*********========================================
*********========================================
*********========================================
添加数据库用户
CREATE USER 'admin'@'host' IDENTIFIED BY 'admin';
授权
mysql>grant all privileges on . to '用户'@'%' identified by '密码';mysql>flush privileges;
查看mysql端口
show global variables like 'port';
开放端口限制(防火墙)
firewall-cmd --add-port=3306/tcp --permanentfirewall-cmd --reload
常用命令
service mysql start #启动MySQLservice mysql stop#停止MySQLservice mysql restart #重启MySQLservice mysql status#查看MySQL状态
查看mysql是否已安装
rpm -qa | grep mysql
查看mysql服务是否启动
ps -ef |grep mysql
安装mysql 可能会用到的命令
cat /etc/passwd #查看所有的用户信息cat /etc/passwd|grep mysql #用户名,用于查找某个用户cat /etc/group #查看所有组信息cat /etc/group|grep mysql #组名,用于查找某个用户组groups #查看当前登录用户的组内成员groups test #查看test用户所在的组,以及组内成员whoami #查看当前登录用户名userdel -r mysql #删除用户id mysql #查看用户mysql相关信息#修改mysql 环境变量:vi/etc/profileexport PATH=$PATH:/usr/local/mysql/bin