时间:2024-11-11 来源:525游
Mysql是我们常用的一种开源关系性数据库,据说,目前排名第二,仅次于Oracle,有各种版本,比如Windows版的、Linux版的,不过一般还是Linux版用的比较多。
可能平时没人关注它的安装方式,这里,我们就来看看Linux下的Mysql有多少种安装方式。
1、下载安装yum仓库
yum -y install https://repo.mysql.com/mysql80-community-release-el7-1.noarch.rpm
2、安装
yum -y install mysql-community-server
3、启动
systemctl start mysqld
4、查看状态
systemctl status mysqld
5、查看初始密码
grep 'temporary password' /var/log/mysqld.log
6、登录测试(用初始化密码)
mysql -uroot -p
7、修改root登录密码
ALTER USER 'root'@'localhost' IDENTIFIED BY '密码';
flush privileges;
8、允许root远程访问
update mysql.user set host = '%' where user ='root';
1、下载安装包
官网
[https://downloads.mysql.com/archives/community/](https://downloads.mysql.com/archives/community/)
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-8.0.20-1.el7.x86_64.rpm-bundle.tar
国内镜像站(中国科学技术大学镜像站)
[http://mirrors.ustc.edu.cn/mysql-ftp/Downloads/MySQL-8.0/](http://mirrors.ustc.edu.cn/mysql-ftp/Downloads/MySQL-8.0/)
wget http://mirrors.ustc.edu.cn/mysql-ftp/Downloads/MySQL-8.0/mysql-8.0.20-1.el7.x86_64.rpm-bundle.tar
2、解压
tar -xvf mysql-8.0.20-1.el7.x86_64.rpm-bundle.tar
3、安装(各个rpm是有依赖的,所以要按顺序安装)
(1)rpm -ivh mysql-community-common-8.0.20-1.el7.x86_64.rpm
(2)rpm -ivh mysql-community-libs-8.0.20-1.el7.x86_64.rpm
如果报错:
error: Failed dependencies:
mariadb-libs is obsoleted by mysql-community-libs-8.0.20-1.el7.x86_64
执行:yum -y remove mysql-libs再继续安装
(3)rpm -ivh mysql-community-libs-8.0.20-1.el7.x86_64.rpm
(4)rpm -ivh mysql-community-server-8.0.20-1.el7.x86_64.rpm
如果报错:
error: Failed dependencies:
libaio.so.1()(64bit) is needed by mysql-community-server-8.0.20-1.el7.x86_64
libaio.so.1(LIBAIO_0.1)(64bit) is needed by mysql-community-server-8.0.20-1.el7.x86_64
libaio.so.1(LIBAIO_0.4)(64bit) is needed by mysql-community-server-8.0.20-1.el7.x86_64
执行:yum -y install libaio再继续安装
4、启动
systemctl start mysqld
5、查看状态
systemctl status mysqld
6、查看初始密码
grep 'temporary password' /var/log/mysqld.log
7、登录测试(用初始化密码)
mysql -uroot -p
8、修改root登录密码
ALTER USER 'root'@'localhost' IDENTIFIED BY '密码';
flush privileges;
9、允许root远程访问
update mysql.user set host = '%' where user ='root';
1、下载二进制glibc包
官网
[https://dev.mysql.com/downloads/mysql/](https://dev.mysql.com/downloads/mysql/)(选择对应版本mysql-8.0.21-linux-glibc2.12-x86_64.tar.xz)
wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.21-linux-glibc2.12-x86_64.tar.xz
国内镜像站(中国科学技术大学镜像站)
wget http://mirrors.ustc.edu.cn/mysql-ftp/Downloads/MySQL-8.0/mysql-8.0.21-linux-glibc2.12-x86_64.tar.xz
2、解压
tar -xvf mysql-8.0.21-linux-glibc2.12-x86_64.tar.xz
3、将解压目录移动到自定义目录,并改名
mv mysql-8.0.21-linux-glibc2.12-x86_64 /usr/local/mysql
4、创建用户组和用户
groupadd mysql
useradd -r -g mysql mysql
5、创建配置文件
vim /etc/my.cnf
[mysqld]
basedir=/usr/local/mysql # 基本路径
datadir=/usr/local/mysql/data #数据路径 (先在/usr/local/mysql建data目录)
socket=/usr/local/mysql/data/mysql.sock # socket文件
symbolic-links=0
log-error=/usr/local/mysql/data/mysqld.log #错误日志
pid-file=/usr/local/mysql/data/mysqld.pid #pid文件
[mysql]
socket=/usr/local/mysql/data/mysql.sock
6、配置环境变量
vim /etc/profile
export MYSQL_HOME=/usr/local/mysql
export MYSQL_PATH=$MYSQL_HOME/bin
export PATH=$PATH:$MYSQL_PATH
source /etc/profile
7、初始化
/usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
报错:
error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
原因:缺少libaio库文件
解决:执行yum install libaio* -y 再重新初始化
8、修改属主属组
chown -R mysql:mysql /usr/local/mysql
9、配置服务管理
cd /usr/local/mysql/support-files/
mv mysql.server /etc/init.d/mysqld
10、启动,设置密码
因为没有初始密码,用跳过授权表的方式来启动mysql
mysqld_safe --defaults-file=/etc/my.cnf --skip-grant-tables &
免密登录
mysql -uroot -p 不需要输入密码直接回车
flush privileges;
ALTER USER 'root'@'localhost' IDENTIFIED BY '密码';
11、重启验证密码
service mysqld restart
service mysqld status
12、允许root远程访问
mysql -uroot -p
update mysql.user set host = '%' where user ='root';
rpm -qa | grep -i mysql
rpm -e MySQL-client-5.6.38-1.el7.x86_64
whereis mysql
rm -rf /usr/lib/mysql
yum -y install autoconf
rpm -ivh MySQL-server-5.6.27-1.linux_glibc2.5.x86_64.rpm
copy mysql.server启动脚本至/etc/init.d/mysqld
# /etc/init.d/mysql stop
# mysqld_safe --user=mysql --skip-grant-tables --skip-networking &
# mysql -u root mysql
mysql> UPDATE user SET Password=PASSWORD('openstack') where USER='root';
mysql> FLUSH PRIVILEGES;
mysql> quit
使用以下命令,查询mysql 镜像
docker search mysql
通过以下命令拉取mysql最新镜像
docker pull mysql:latest
通过以下命令查看已下载的镜像
docker images
通过以下命令创建并运行容器
docker run -d
--name mysql
--restart=always
-p 3306:3306
-e MYSQL_ROOT_PASSWORD=xxxx
mysql:latest
通过以下命令查看正在运行的容器
docker ps
通过以下命令进入容器docker exec -it 68a63f41845e bash
登录mysql命令MySQL:mysql -u root -p