Linux环境中安装Mysql5.7,系统版本centos7.6。
Mysql5.7安装
本文总共包含4种安装方式,均通过验证,推荐使用yum安装(最新版本),rpm可安装指定版本,安装包可安装指定版本。
安装方式如下:
YUM安装
Mysql5.7 YUM下载地址
卸载之前版本
1 2 3 4 5 6 7
|
[root@localhost ~] mariadb-libs-5.5.68-1.el7.x86_64 [root@localhost ~] [root@localhost ~]
|
下载rpm文件
1
| wget https://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql57-community-release-el7-10.noarch.rpm
|
安装Mysql
1 2 3 4 5 6 7 8 9 10 11 12 13
| yum -y localinstall mysql57-community-release-el7-10.noarch.rpm 或 rpm -ivh mysql57-community-release-el7-10.noarch.rpm
yum whatprovides mysql-community-server
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
yum -y install mysql-community-server
|
启动Mysql
1 2
| systemctl start mysqld systemctl enable mysqld
|
重置密码
1 2 3 4 5 6 7 8 9 10
| [root@localhost ~] 2022-08-25T08:25:32.680447Z 1 [Note] A temporary password is generated for root@localhost: XcrEJlRho0_b
mysql -uroot -p set global validate_password_policy=0; set global validate_password_length=1;
set password for root@localhost = password('root');
|
开放远程连接
1 2 3
| use mysql; update user set user.Host='%' where user.User='root'; flush privileges;
|
RPM安装
Mysql5.7 RPM下载地址
卸载之前版本
1 2 3 4 5 6 7
|
[root@localhost ~] mariadb-libs-5.5.60-1.el7_5.x86_64 [root@localhost ~] [root@localhost ~]
|
下载rpm文件
mysql-community-libs-compat 能解决缺失libmysqlclient.so.18问题,处理参考
1 2 3 4 5
| wget https://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql-community-common-5.7.31-1.el7.x86_64.rpm wget https://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql-community-libs-5.7.31-1.el7.x86_64.rpm wget https://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql-community-client-5.7.31-1.el7.x86_64.rpm wget https://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql-community-server-5.7.31-1.el7.x86_64.rpm wget https://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql-community-libs-compat-5.7.31-1.el7.x86_64.rpm
|
安装Mysql
1 2 3 4 5 6 7
| yum install -y perl numactl
rpm -ivh mysql-community-common-5.7.31-1.el7.x86_64.rpm rpm -ivh mysql-community-libs-5.7.31-1.el7.x86_64.rpm rpm -ivh mysql-community-client-5.7.31-1.el7.x86_64.rpm rpm -ivh mysql-community-server-5.7.31-1.el7.x86_64.rpm rpm -ivh mysql-community-libs-compat-5.7.31-1.el7.x86_64.rpm
|
通过在命令行中指定可选的–prefix或–relocate参数来指定安装路径或目录。
(1)指定安装路径:如果我们要将软件包安装到路径/usr/local/newdir/下,我们可以通过执行以下命令来实现:rpm -ivh package.rpm –prefix /usr/local/newdir/。
(2)指定安装目录:如果我们要将RPM软件包中的某一组件安装到路径/usr/lib/newlib/下,我们可以通过执行以下命令来实现:rpm -ivh package.rpm -–relocate ‘/usr/lib=/usr/lib/newlib/’。
在安装RPM时,指定安装路径或目录可以更好地对软件进行定制化安装,提高软件的应用效率。在使用时,我们需要根据具体需要来指定正确的安装目录和路径,同时,为了避免软件安装过程中出现意外情况,我们也需要进行备份等工作。
RPM其他命令-卸载rpm包
卸载包命令:rpm -e yum-utils-1.1.31-54.el7_8
卸载包命令:rpm -e –nodeps yum-utils-1.1.31-54.el7_8
功能:卸载rpm包,-e是erase简写,就是清除卸载包;–nodeps,是代表不确认包的依赖。
RPM其他命令-查看已安装的包
查询已安装的rpm包列表:rpm -qa
已安装包中的查询包含关键字的包:rpm -qa | grep yum-utils
RPM其他命令-查看已安装包的信息
命令:rpm -qi yum-utils
启动Mysql
1 2
| systemctl start mysqld systemctl enable mysqld
|
重置密码
1 2 3 4 5 6 7 8 9 10
| [root@localhost ~] 2022-08-25T08:25:32.680447Z 1 [Note] A temporary password is generated for root@localhost: IqYx0nr/VEk7
mysql -uroot -p set global validate_password_policy=0; set global validate_password_length=1;
set password for root@localhost = password('root');
|
开放远程连接
1 2 3
| use mysql; update user set user.Host='%' where user.User='root'; flush privileges;
|
开启日志
编辑my.cnf文件,添加如下代码,并重启mysqld
1 2 3 4 5 6 7
| [root@localhost ~]
log-bin=mysql-bin binlog_format=Mixed server_id=1
[root@localhost ~]
|
安装包安装
Mysql5.7安装包下载地址
卸载之前版本
卸载mariadb 和 mysql
1 2 3 4 5 6 7
|
[root@localhost ~] mariadb-libs-5.5.68-1.el7.x86_64 [root@localhost ~] [root@localhost ~]
|
创建MySQL用户组
1 2 3 4 5 6 7
| cat /etc/group | grep mysql cat /etc/passwd |grep mysql
groupadd mysql useradd -r -g mysql mysql
|
获取安装包
1
| wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
|
解压mysql
1 2
| tar -zxvf mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz mv mysql-5.7.34-linux-glibc2.12-x86_64 /usr/local/mysql/
|
创建数据和⽇志⽬录
1 2
| chown -R mysql:mysql /usr/local/mysql chmod -R 755 /usr/local/mysql
|
初始化mysqld
1 2
| /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/usr/local/mysql/data --basedir=/usr/local/mysql
|
编写配置文件my.cnf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| [root@localhost ~]
[mysqld] datadir=/usr/local/mysql/data port = 3306 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES symbolic-links=0 max_connections=400 innodb_file_per_table=1
lower_case_table_names=1
log-bin=mysql-bin binlog_format=Mixed server_id=1
binlog 有三种格式: Statement(Statement-Based Replication,SBR):每一条会修改数据的 SQL 都会记录在 binlog 中。 Row(Row-Based Replication,RBR):不记录 SQL 语句上下文信息,仅保存哪条记录被修改。 Mixed(Mixed-Based Replication,MBR):Statement 和 Row 的混合体。
|
启动mysql
1 2 3 4 5 6 7
| ln -s /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql
systemctl daemon-reload systemctl start mysqld.service
|
登录mysql
1 2 3 4 5
| ln -s /home/mysql /usr/local/mysql
mysql -u root -p
|
修改密码
1
| set password for root@localhost = password('root');
|
开放远程连接
1 2 3
| use mysql; update user set user.Host='%' where user.User='root'; flush privileges;
|
设置开机自启
第一种启动
1 2 3 4 5 6
| chmod +x /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig --list
|
第二种启动
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| [root@sx-db init.d]
echo "----启动mysql----"$(date +%y年%m月%d日%H:%M:%s); export LANG=zh-cn.UTF8
systemctl daemon-reload systemctl start mysqld.service
[root@sx-db init.d]
[root@sx-db init.d] [root@sx-db init.d] /etc/init.d/mysql.sh
|
第三种启动
使用systemd,自己写个服务