数据库-MySQL安装

yum在线安装

[root@localhost ~]#wget -i -c http://dev.mysql.com/get/mysql57-community- release-el7-10.noarch.rpm
[root@localhost ~]# yum install -y mysql57-community-release-el7-10.noarch.rpm 
[root@localhost ~]# yum -y install mysql-community-server

rpm包安装

事先从官网下载RPM安装包,上传至服务器:

[root@localhost ~]# ls -l mysql-5.7.14-1.el7.x86_64.rpm-bundle.tar 
-rw-r--r-- 1 root root 569344000 10月 24 2017 mysql-5.7.14-1.el7.x86_64.rpm- bundle.tar

解压查看:

[root@localhost ~]# tar xf mysql-5.7.14-1.el7.x86_64.rpm-bundle.tar 
[root@localhost ~]# ls -l mysql-community*
其中:
 mysql-community-client 客户端 
 mysql-community-devel 开发库 
 mysql-community-common 服务端和客户端的公共文件 
 mysql-community-embedded 嵌入式 
 mysql-community-serve 服务端 
 mysql-community-test 测试组件 
 mysql-community-libs 共享库 
 mysql-community-libs-compat MySQL之前版本的共享兼容库

手动安装:

[root@localhost ~]# yum remove mariadb-libs

 [root@localhost ~]# rpm -ivh  mysql-community-client-5.7.14-1.el7.x86_64.rpm mysql-community-common-5.7.14-1.el7.x86_64.rpm mysql-community-libs-5.7.14- 1.el7.x86_64.rpm mysql-community-server-5.7.14-1.el7.x86_64.rpm

启动服务:

[root@mysql01 ~]# systemctl enable --now mysqld   开机自启动

查看服务是否启动

[root@mysql01 ~]# systemctl is-active mysqld
active
[root@mysql01 ~]# systemctl is-enabled mysqld
enabled
[root@mysql01 ~]# netstat -lnpt | grep 3306    查看端口
tcp6       0      0 :::3306                 :::*                    LISTEN      12318/mysqld        
[root@mysql01 ~]# ps -ef | grep mysqld   查看进程
mysql     12318      1  6 14:30 ?        00:00:50 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
root      12402   1564  0 14:43 pts/0    00:00:00 grep --color=auto mysqld

[root@mysql01 ~]# systemctl status  mysqld   查看状态
[root@localhost ~]# systemctl stop firewalld.service   关闭防火墙

修改root密码

[root@localhost ~]# grep  password /var/log/mysqld.log     查询数据库生成的一次性密码root@localhost之后
2021-01-13T03:23:06.792634Z 1 [Note] A temporary password is generated for root@localhost: hh/u<a#PT4H/
[root@localhost ~]# mysql -u root -p'hh/u<a#PT4H/'          复制查询出来的密码进入数据库
mysql> alter user root@localhost identified by 'ABCabc123.';      修改密码(由于密码策略的问题大小写,特殊字符必须都有)  
Query OK, 0 rows affected (0.00 sec)

修改密码策略:
在 /etc/my.cnf 配置文件中[mysqld] 下增加

validate_password=off

修改完毕,重启服务。可以重新进入数据库修改密码

通用二进制格式安装

1、下载glibc版本的Mysql

mysql-5.7.14-linux-glibc2.5-x86_64.tar

2、新建用户以安全方式运行进程

[root@serverb local]# groupadd -r -g  27 mysql
[root@serverb local]# useradd -r -u 27 -g 27 -c "MySQL Server" -s /bin/false mysql

3、安装并初始化mysql

[root@localhost ~]# tar xf mysql-5.7.14-linux-glibc2.5-x86_64.tar -C /usr/local/
[root@localhost ~]# cd /usr/local/
[root@localhost local]# tar xf mysql-5.7.14-linux-glibc2.5-x86_64.tar.gz
[root@localhost local]# ln -sv mysql-5.7.14-linux-glibc2.5-x86_64 mysql        创建软连接
"mysql" -> "mysql-5.7.14-linux-glibc2.5-x86_64"
[root@localhost local]# chown -R mysql.mysql mysql/*

4、初始化

mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
 --initialize    代表初始化
 --user          指定用户
 --basedir       指定基本目录
 --datadir       指定数据库的数据文件
 注意记录临时密码:
2019-09-30T07:54:09.437644Z 1 [Note] A temporary password is generated for root@localhost: Di23xhiKVt-W 
临时密码只允许第一次登录用
注意:将临时密码记住

&RpsMMXNi7;Z
4、为mysql提供主配置文件

[root@localhost local]# cd /usr/local/mysql
[root@localhost mysql]# \cp support-files/my-default.cnf /etc/my.cnf

vim /etc/my.cnf
添加如下行指定mysql数据文件的存放位置:
datadir = /usr/local/mysql/data

5、为mysql提供sysv服务脚本

[root@localhost mysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
[root@localhost mysql]# chmod +x /etc/rc.d/init.d/mysqld

6、启动mysql

# chkconfig --add mysqld --添加为系统服务
# chkconfig mysqld on --开机自启动
systemctl start mysqld

7、将mysql的bin目录加入PATH环境变量,编辑/etc/profile文件
使用mysql客户端命令

[root@localhost mysql]# mysql
-bash: mysql: 未找到命令

由于PATH环境变量中没有添加,如下:

# cd /etc/profile.d/
# vim mysql.sh
• 添加如下内容
export PATH=$PATH:/usr/local/mysql/bin
# source mysql.sh

通用二进制下的多实例安装

方法一:多实例各一个my.cnf配置文件

1、关闭服务
[root@localhost ~]# systemctl stop mysqld.service 
[root@localhost ~]# systemctl disable mysqld 
mysqld.service is not a native service, redirecting to /sbin/chkconfig.
Executing /sbin/chkconfig mysqld off

2、分别创建data目录
[root@localhost ~]# mkdir /mysql/3306/data -p
[root@localhost ~]# mkdir /mysql/3307/data -p

3、分别编辑my.cnf配置文件
[root@localhost ~]# vim /mysql/3306/my.cnf
[client] 
port = 3306 
socket = /mysql/3306/mysql.sock 

[mysqld] 
user = mysql 
port = 3306 
socket = /mysql/3306/mysql.sock 
basedir = /usr/local/mysql 
datadir = /mysql/3306/data 
server-id = 2 

[mysqldump] 
quick 
max_allowed_packet = 16M 

[mysqld_safe] 
log-error=/mysql/3306/mysql_3306.err 
pid-file=/mysql/3306/mysqld.pid

[root@localhost ~]# vim /mysql/3307/my.cnf
[client]
port = 3307 
socket = /mysql/3307/mysql.sock 

[mysqld] 
user = mysql 
port = 3307 
socket = /mysql/3307/mysql.sock 
basedir = /usr/local/mysql 
datadir = /mysql/3307/data 
server-id = 3

[mysqldump] 
quick 
max_allowed_packet = 16M 

[mysqld_safe] 
log-error=/mysql/3307/mysql_3307.err 
pid-file=/mysql/3307/mysqld.pid

4、修改创建的目录权限
[root@localhost ~]# chown -R mysql.mysql /mysql/

5、初始化数据库
[root@localhost ~]# /usr/local/mysql/bin/mysqld --initialize --datadir=/mysql/3306/data --basedir=/usr/local/mysql --user=mysql
记录临时密码:ZVlvJitpB1(f
[root@localhost ~]# /usr/local/mysql/bin/mysqld --initialize --datadir=/mysql/3306/data --basedir=/usr/local/mysql --user=mysql
记录临时密码)L(hWC8q:Mp2

6、重启服务
[root@localhost ~]# mysqld_safe --defaults-file=/mysql/3306/my.cnf &
[root@localhost ~]# mysqld_safe --defaults-file=/mysql/3307/my.cnf &

7、查看端口是否重启成功
[root@localhost ~]# netstat -lnupt | grep 330
tcp6       0      0 :::3306                 :::*                    LISTEN      2008/mysqld         
tcp6       0      0 :::3307                 :::*                    LISTEN      2209/mysqld 

8、进入数据库重置密码
[root@localhost ~]# mysql -uroot -p'ZVlvJitpB1(f' -S /mysql/3306/mysql.sock
mysql> alter user root@localhost identified by '123456';
[root@localhost ~]# mysql -uroot -p')L(hWC8q:Mp2' -S /mysql/3307/mysql.sock
mysql> alter user root@localhost identified by '123456';

9、关闭服务
[root@localhost ~]# mysqladmin -uroot -p123456  -S /mysql/3306/mysql.sock shutdow
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
[root@localhost ~]# mysqladmin -uroot -p123456  -S /mysql/3307/mysql.sock shutdow
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
启动:# mysqld_safe --defaults-file=/data/3306/my.cnf & 
关闭:# mysqladmin -uroot -p'' -S /data/3306/mysql.sock shutdow

方法二:多实例公用一个my.cnf配置文件

1、编辑my.cnf配置文件
[root@localhost ~]# cp /etc/my.cnf /etc/my.cnf.bak
[root@localhost ~]# vim /etc/my.cnf
[mysqld_multi]
mysqld = /usr/local/mysql/bin/mysqld_safe
mysqladmin = /usr/local/mysql/bin/mysqladmin
user = root
pass = 123456

[mysqld3306]
socket = /mysql/3306/mysql.sock
port = 3306
pid-file = /mysql/3306/mysql.pid
datadir = /mysql/3306/data
basedir = /usr/local/mysql

[mysqld3307]
socket = /mysql/3307/mysql.sock
port = 3307
pid-file = /mysql/3307/mysql.pid
datadir = /mysql/3307/data
basedir = /usr/local/mysql

2、启动服务
[root@localhost ~]# mysqld_multi start 3306,3307

3、进入数据库
[root@localhost ~]# mysql -uroot -p123456 -S /mysql/3306/mysql.sock
[root@localhost ~]# mysql -uroot -p123456 -S /mysql/3307/mysql.sock

4、停止多实例
[root@slave ~]# mysqld_multi stop  3306,3307
[root@slave ~]# netstat -lnupt | grep 330

猜你喜欢

转载自blog.csdn.net/m0_46289868/article/details/112556853