系统版本:CentOS Linux release 7.9.2009 (Core)
自带的mariadb在/etc目录下也有一个my.cnf
卸载掉系统默认数据库mariadb
[root@yfm03 mysql]# rpm -qa | grep mariadb
mariadb-libs-5.5.68-1.el7.x86_64
[root@yfm03 mysql]# rpm -e --nodeps mariadb-libs-5.5.68-1.el7.x86_64
源码安装mysql
mkdir -p /data/mysql/
cd /data/mysql/ && wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.21.tar.gz
tar -zxvf mysql-5.7.21.tar.gz
cmake . \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/usr/local/mysql/mysqldata \
-DWITH_BOOST=/data/boost/boost_1_59_0 \ #缺少boost才添加
-DSYSCONFDIR=/etc \
-DDEFAULT_CHARSET=utf8mb4 \
-DDEFAULT_COLLATION=utf8mb4_general_ci \
-DENABLED_LOCAL_INFILE=1 \
-DEXTRA_CHARSETS=all
如果出现类似的报错,说明系统缺少需要的boost依赖,mysql5.7依赖于boost1.59
CMake Error at cmake/boost.cmake:81 (MESSAGE):
You can download it with -DDOWNLOAD_BOOST=1 -DWITH_BOOST=<directory>
This CMake script will look for boost in <directory>. If it is not there,
it will download and unpack it (in that directory) for you.
使用yum install boost安装
rpm -qa|grep boost 或者 yum list installed | grep boost发现系统默认下载的版本为1.5.3
使用yum remove boost卸载
mkdir -p /data/boost
cd /data/boost && wget http://www.sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz
tar -xvzf boost_1_59_0.tar.gz
cd /data/mysql/mysql-5.7.21
make && make install #时间较久
groupadd mysql
useradd -g mysql mysql -M -s /sbin/nologin #创建mysql用户,不建立mysql用户家目录(加-M参数),也就是禁用mysql账号登陆系统
mkdir -p /usr/local/mysql/mysqldata
chown -R mysql:mysql /usr/local/mysql
chown -R mysql:mysql /usr/local/mysql/mysqldata
之前mysql使用mysqld_db_install命令进行初始化的,但在5.7之后,数据库初始化发生了改变,需要mysqld命令进行初始化
1)不生成root密码
/usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/mysqldata
2)生成root密码
/usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/mysqldata
查看生成的密码
cat /usr/local/mysql/mysqldata/mysql-error.log | grep password
配置系统服务
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
开机启动
chkconfig mysql on
在启动MySQL服务时,会按照一定次序搜索my.cnf,先在/etc目录下找,找不到则会搜索"$basedir/my.cnf",在本例中就是/usr/local/mysql/my.cnf,这是新版MySQL的配置文件的默认位置!
在CentOS 6.4版操作系统最小安装完成后,默认会在/etc目录下存在一个my.cnf,需要将此文件更名为其他的名字,如:/etc/my.cnf.bak,否则,该文件会干扰源码安装的MySQL的正确配置,造成无法启动。
在使用"yum update"更新系统后,需要检查下/etc目录下是否会多出一个my.cnf,如果多出,将它重命名成别的。否则,MySQL将使用这个配置文件启动,可能造成无法正常启动等问题。
新建/etc/my.cnf、/etc/my.cnf.d/mysql-clients.cnf
修改/etc/my.cnf文件
### /etc/my.cnf
[mysqld]
socket=/var/lib/mysql/mysql.sock
#指定server端字符集
character-set-server = utf8
collation-server = utf8_general_ci
# 解决问题:TIMESTAMP with implicit DEFAULT value is deprecated
explicit_defaults_for_timestamp=true
# mysql程序安装目录
basedir=/usr/local/mysql
# mysql数据文件目录
datadir=/usr/local/mysql/mysqldata
pid-file = /usr/local/mysql/mysqldata/mysql.pid
log_error = /usr/local/mysql/mysqldata/mysql-error.log
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
#更改字符集为utf-8
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
修改/etc/my.cnf.d/mysql-clients.cnf文件
### /etc/my.cnf.d/mysql-clients.cnf
#
# These groups are read by MariaDB command-line tools
# Use it for options that affect only one utility
#
# 在配置文件中添加“[client]”选项和“[mysql]”选项
# 并使用这两个选项下的“socket”参数值,与“[mysqld]”选项下的“socket”参数值,指向的socket文件路径完全一致
# 解决问题: connect to local MySQL server through socket /var/lib/mysql/mysql.sock
[mysql]
default-character-set=utf8
socket=/var/lib/mysql/mysql.sock
[client]
default-character-set=utf8
socket=/var/lib/mysql/mysql.sock
[mysql_upgrade]
[mysqladmin]
[mysqlbinlog]
[mysqlcheck]
[mysqldump]
[mysqlimport]
[mysqlshow]
[mysqlslap]
启动mysql
service mysql start
[root@yfm01 mysqldata]# service mysql start
Starting MySQL.2021-01-21T07:18:27.858957Z mysqld_safe Directory '/var/lib/mysql' for UNIX socket file don't exists.
ERROR! The server quit without updating PID file (/usr/local/mysql/mysqldata/mysql.pid).
mkdir /var/lib/mysql
chown -R mysql:mysql /var/lib/mysql/
设置环境变量
echo "PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:\$PATH" >> /etc/profile
echo "export PATH " >> /etc/profile
source /etc/profile
mysql忘记密码
需要注意mysql版本
service mysql stop
#以无密码方式进入MySQL安全模式。
mysqld_safe --skip-grant-tables &
mysql -u root,按回车键即可
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> update mysql.user set authentication_string=password('123456') where host='localhost' and user='root';
Query OK, 1 row affected, 1 warning (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 1
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
设置远程连接
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> grant all privileges on *.* to 'root' @'%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql卸载
service mysql stop
find / -name mysql
rm -rf 删掉所有的文件
删掉配置文件
/etc/my.cnf、/etc/my.cnf.d、/etc/init.d/mysql
删除MySQL用户以及用户组
[root@yfm01 mysql]# id mysql
uid=27(mysql) gid=27(mysql) groups=27(mysql)
userdel mysql
删除配置的环境变量
yum方式安装
wget http://repo.mysql.com//mysql57-community-release-el7-8.noarch.rpm
安装yum repository
yum -y install mysql57-community-release-el7-8.noarch.rpm
查看mysql server
yum search mysql-com
安装
yum -y install mysql-community-server.x86_64 #时间久,和网络有关
启动
systemctl start mysqld.service
配置文件路径
/etc/my.cnf
我这里启动失败了,可能是之前没有卸载mariadb
[root@yfm02 packages]# systemctl start mysqld.service
Job for mysqld.service failed because the control process exited with error code. See "systemctl status mysqld.service" and "journalctl -xe" for details.
使用journalctl -xe命令
发现无法创建目录/usr/local/mysql/mysqldata/
mkdir -p /usr/local/mysql/mysqldata
接着继续启动systemctl start mysqld.service
查看生成的历史密码
cat /var/log/mysqld.log | grep password | grep password
连接
mysql -uroot -p
回车输入刚刚查到的密码即可
修改初始密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
如果出现密码设置太简单提示
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
set global validate_password_policy=0;
set global validate_password_length=1;
然后继续修改密码
切换数据库
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
设置远程连接
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
忘记密码
vi /etc/my.cnf
在[mysqld]下添加skip-grant-tables
重启mysql
systemctl restart mysqld.service
mysql -uroot -p
回车即可
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '12345678';
ERROR 1290 (HY000): Unknown error 1290
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
然后去掉刚刚在/etc/my.cnf添加的skip-grant-tables,重启mysql服务
卸载
[root@yfm02 yum.repos.d]# yum list installed | grep mysql
mysql-community-client.x86_64 5.7.33-1.el7 @mysql57-community
mysql-community-common.x86_64 5.7.33-1.el7 @mysql57-community
mysql-community-libs.x86_64 5.7.33-1.el7 @mysql57-community
mysql-community-libs-compat.x86_64 5.7.33-1.el7 @mysql57-community
mysql-community-server.x86_64 5.7.33-1.el7 @mysql57-community
mysql57-community-release.noarch el7-8 @/mysql57-community-release-el7-8.noarch
yum remove -y mysql-community-client.x86_64
yum remove -y mysql-community-common.x86_64
yum remove -y mysql-community-libs.x86_64
yum remove -y mysql-community-libs-compat.x86_64
yum remove -y mysql-community-server.x86_64
yum remove -y mysql57-community-release.noarch
或者
rpm -qa | grep -i mysql
rpm -e --nodeps xxx
然后继续参考上文mysql卸载
关闭开机自启
systemctl disable mysqld
rpm方式安装
直接去官网下载https://www.mysql.com/downloads/
或者
wget http://repo.mysql.com//mysql57-community-release-el7-8.noarch.rpm
安装yum repository
yum -y install mysql57-community-release-el7-8.noarch.rpm
查看mysql server
yum search mysql-com
下载rpm包,不安装
yum -y install --downloadonly --downloaddir=/data/mysql mysql-community-server.x86_64
顺序安装
rpm -ivh mysql-community-common-5.7.33-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-5.7.33-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-compat-5.7.33-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-5.7.33-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-5.7.33-1.el7.x86_64.rpm
启动msql
service mysqld start
查看生成的临时密码
[root@yfm03 mysql]# cat /var/log/mysqld.log | grep password
2021-01-21T02:15:35.835318Z 1 [Note] A temporary password is generated for root@localhost: zqpc0gxPBE-h
连接
mysql -uroot -p
回车输入刚才查到的密码即可
其他的改密码、卸载与yum 方式安装的mysql类似,不再赘述
安装mysql clinet
有时候不希望安装mysql server,只是想使用mysql命令连接数据库
rpm -ivh https://repo.mysql.com//mysql57-community-release-el7-11.noarch.rpm
yum search mysql-community
yum install mysql-community-client.x86_64 -y