mysql 安装部署
2022-07-13 本文已影响0人
JepWei
yum方法
mysql5版本用以下配置:
wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
yum localinstall mysql57-community-release-el7-8.noarch.rpm
vim /etc/yum.repos.d/mysql-community.repo #可以在这里面安装指定版本,也可跳过
mysql8版本用以下配置:
vi /etc/yum.repos.d/mysql-community.repo
[mysql-connectors-community]
name=MySQL Connectors Community
baseurl=https://opentuna.cn/mysql/yum/mysql-connectors-community-el7-$basearch/
enabled=1
gpgcheck=1
gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql
[mysql-tools-community]
name=MySQL Tools Community
baseurl=https://opentuna.cn/mysql/yum/mysql-tools-community-el7-$basearch/
enabled=1
gpgcheck=1
gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql
[mysql-5.6-community]
name=MySQL 5.6 Community Server
baseurl=https://opentuna.cn/mysql/yum/mysql-5.6-community-el7-$basearch/
enabled=0
gpgcheck=1
gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql
[mysql-5.7-community]
name=MySQL 5.7 Community Server
baseurl=https://opentuna.cn/mysql/yum/mysql-5.7-community-el7-$basearch/
enabled=1
gpgcheck=1
gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql
[mysql-8.0-community]
name=MySQL 8.0 Community Server
baseurl=https://opentuna.cn/mysql/yum/mysql-8.0-community-el7-$basearch/
enabled=1
gpgcheck=1
gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql
如有gpg报错则:
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
yum install mysql-community-server
systemctl start mysqld
systemctl enable mysqld
systemctl daemon-reload
firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --reload
mysql5版本初始化
#查看默认密码
grep 'temporary password' /var/log/mysqld.log
mysql -uroot -p
#降低密码强度的方法(mysql5.7命令)
ALTER USER 'root'@'localhost' IDENTIFIED BY 'Aa#0539';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'Aa#0539' WITH GRANT OPTION;
FLUSH PRIVILEGES;
mysql8初始化
grep 'temporary password' /var/log/mysqld.log
use mysql;
update user set host='%' where user='root';
ALTER USER 'root'@'%' IDENTIFIED BY 'Root@123' PASSWORD EXPIRE NEVER;
FLUSH PRIVILEGES;
#创建新用户(可选)
CREATE USER 'app'@'%' IDENTIFIED BY 'App@123456' PASSWORD EXPIRE NEVER;
GRANT INSERT,UPDATE,DELETE,SELECT,CREATE TEMPORARY TABLES,LOCK TABLES,SHOW DATABASES ON *.* TO 'app'@'%';
FLUSH PRIVILEGES;
#开端口
firewall-cmd --zone=public --add-port=3306/tcp --permanent && firewall-cmd --reload
#最后要卸载这个防止自动更新:
yum -y remove mysql57-community-release-el7-8.noarch
RPM方法:
1. 将以下依赖和mysql的rpm上传安装即可
2. 如上传慢,在有网络的情况下可以用wget下载mysql的rpm
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.36-1.el7.x86_64.rpm-bundle.tar
源码安装
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz
tar xf mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
ln -s /usr/local/mysql-5.7.35-linux-glibc2.12-x86_64 /usr/local/mysql
groupadd mysql;
useradd -r -g mysql mysql;
#数据目录根据实际情况修改
mkdir -p /data/mysql;
chown mysql:mysql -R /data/mysql;
vi /etc/profile
export MYSQL_HOME=/usr/local/mysql
export PATH=.:${MYSQL_HOME}/bin:$PATH
source /etc/profile
vi /etc/my.cnf
[mysqld]
bind-address=0.0.0.0 #绑定地址运行远程连接
port=3306 #Mysql开放的端口
user=mysql #数据库登录用户
basedir=/usr/local/mysql #Mysql安装的绝对路径
datadir=/data/mysql #Mysql数据存放的绝对路径
socket=/tmp/mysql.sock #套接字文件
log-error=/data/mysql/mysql.err #mysql生成的错误日志存放的路径
pid-file=/data/mysql/mysql.pid #为mysqld程序指定一个存放进程ID的文件
character_set_server=utf8mb4 #数据库字符编码
symbolic-links=0 #是否开启链接符号
explicit_defaults_for_timestamp=true #数据库timestamp类型的列自动更新
#配置导出免密
[mysqldump]
user=root
password="123456"
cd /usr/local/mysql/bin/
./mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql/ --datadir=/data/mysql/ --user=mysql --initialize
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
service mysql start
service mysql status
cat /data/mysql/mysql.err | grep password
mysql -uroot -p
SET PASSWORD = PASSWORD('LeShan#1456');
ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER;
FLUSH PRIVILEGES;
#设置任何主机都可以访问mysql,主要是为了远程Navicat连接
USE mysql;
UPDATE user SET host = '%' WHERE user = 'root';
#权限刷新
FLUSH PRIVILEGES;
#退出数据库
exit;
service mysql restart;
service mysql status;
cp /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld;
chmod +x /etc/init.d/mysqld;
chkconfig --add mysqld;
chkconfig --list;
firewall-cmd --zone=public --add-port=3306/tcp --permanent;
firewall-cmd --reload;
firewall-cmd --list-all;
Docker方法:
docker pull mysql:5.7
mkdir -p ~/mydata/mysql/data
mkdir -p ~/mydata/mysql/conf
mkdir -p ~/mydata/mysql/log
docker run --name mysql -d -p 3306:3306 -v ~/mydata/mysql/data:/var/lib/mysql -v ~/mydata/mysql/conf:/etc/mysql/conf.d -v ~/mydata/mysql/log:/var/log/mysql -e MYSQL_ROOT_PASSWORD=123456 --privileged=true mysql:5.7
mysql卸载方法如下:
rpm包安装方式卸载
1.查包名:rpm -qa|grep -i mysql
2.删除命令:rpm -e –nodeps 包名
yum安装方式卸载
查看已安装的mysql
命令:rpm -qa | grep -i mysql
卸载mysql
命令:yum remove mysql-community-server-5.6.36-2.el7.x86_64
查看mysql的其它依赖:rpm -qa | grep -i mysql
卸载依赖
yum remove mysql-libs
yum remove mysql-server
yum remove perl-DBD-MySQL
yum remove mysql
删除残留文件(重要)
rm -f /var/log/mysqld.log
rm -rf /var/lib/mysql
docker方式卸载
docker rm -f [docker id]