------------------------------------------ A. 卸载 ------------------------------------------------------------------------------
第一步 查找并卸载相关软件
rpm -qa |grep -i mysql
rpm -qa |grep -i mariadb
第二步 依次执行下面的命令开始卸载mysql及其依赖:
yum remove mysql-community-common-xxxx.x86_64
yum remove mysql-community-clientxxxx.x86_64
yum remove mysql57-community-xxxxx.noarch
yum remove mysql-community-libs-xxxxx.x86_64
yum removemysql-community-server-xxxx.x86_64
第三步,继续使用第一步中的命令来查看mysql是否卸载完成:
rpm -qa |grep -i mysql
第四步,使用下面的命令来查找mysql相关目录信息:
find / -name mysql
第五步,使用下面的命令来删除mysql相关目录信息:
rm -rf 目录信息
第六步,使用下面的命令来删除/etc/my.cnf配置文件:
rm -rf /etc/my.cnf
第七步,删除mysql日志文件
使用下面的命令来删除/var/log/mysqld.log日志文件,注意如果不删除这个文件,会导致新安装的mysql无法生成新密码,进而导致无法登陆:
rm -rf /var/log/mysqld.log
如果你是选择上传安装包安装,直接停止mysql服务,然后删除相关文件及service
------------------------------------------ B. Repository安装方式 -------------------------------------------------------
MySQL :: Download MySQL Yum Repository
https://dev.mysql.com/downloads/repo/yum/
wget https://repo.mysql.com//mysql80-community-release-el9-1.noarch.rpm --no-check-certificate
sudo rpm -i mysql80-community-release-el9-1.noarch.rpm
安装 rpmlib
[liwu@lw-centos soft]$ sudo yum search rpmlib
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* epel: mirrors.tuna.tsinghua.edu.cn
* extras: mirrors.tuna.tsinghua.edu.cn
* updates: mirrors.aliyun.com
======== N/S matched: rpmlib=================================
kobo-rpmlib.noarch : Functions to manipulate with RPM files
# sudo yum install kobo-rpmlib.noarch
sudo yum update
sudo yum install mysql-server
启动
sudo systemctl start mysqld.service
查看密码(必须启动后才有):
sudo grep "password" /var/log/mysql/mysqld.log
2023-04-08T16:26:50.585520Z 6 [Note] [MY-010454] [Server]
A temporary password is generated for root@localhost: Luw!6WX+#Gri
2023-04-12T18:13:16.322927Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option
安装完成后,MySQL服务将自动启动,您可以通过键入以下内容进行验证:
sudo apt policy mysql-server
sudo systemctl status mysqld
sudo systemctl enable mysql # 开机开启mysql
sudo systemctl disable mysql # 禁用mysql
sudo systemctl restart mysql # 重启mysql
sudo systemctl stop mysql # 关闭mysql
sudo systemctl status mysql # 查看mysql运行状态
9.防火墙
sudo ufw allow 3306/tcp
sudo ufw status
Status: active
To Action From
-- ------ ----
22/tcp ALLOW Anywhere
Nginx Full ALLOW Anywhere
3306/tcp ALLOW Anywhere
22/tcp (v6) ALLOW Anywhere (v6)
Nginx Full (v6) ALLOW Anywhere (v6)
3306/tcp (v6) ALLOW Anywhere (v6)
如果仍然出现远程访问 Ubuntu上的Myql时,报10061错误
# sudo vim /etc/my.cnf
修改如下
bind-address = 127.0.0.1
mysqlx-bind-address = 127.0.0.1
为
bind-address = 0.0.0.0
mysqlx-bind-address = 0.0.0.0
重启 sudo systemctl restart mysqld.service
为了提高MySQL安装的安全性,执行 sudo mysql_secure_installation[可选]
liwu@liwu:~$ sudo mysql_secure_installation
Securing the MySQL server deployment.
Connecting to MySQL using a blank password.
VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?
Press y|Y for Yes, any other key for No: y
There are three levels of password validation policy:
LOW Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2
Please set the password for root here.
#这里设置root密码为 LIWU19842018@outlook.com
New password:
Re-enter new password:
Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
- Dropping test database...
Success.
- Removing privileges on test database...
Success.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.
All done!
以root用户登录MySQL。
在终端中,键入以下命令:sudo mysql -u root -p,输入 root用户的密码,然后按Enter 。 会得到以下信息
8.默认情况下, root用户将使用auth_socket插件身份验证方法,而不是密码。
要设置root用户使用密码连接到MySQL实例,需要将身份验证方法从auth_socket更改为mysql_native_password方法。
首先,在MySQL用户前面使用sudo命令连接到MySQL实例:sudo mysql -u root -p
第一个就是将验证方法从auth_socket修改成mysql_native_password。你可以通过运行下面的命令实现:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'LWlw184....';
FLUSH PRIVILEGES;
第二个选项,推荐的选项,就是创建一个新的独立管理用户,拥有所有数据库的访问权限:
CREATE USER 'liwu'@'%' IDENTIFIED BY 'LWlw184....';
GRANT ALL PRIVILEGES ON *.* TO 'liwu'@'%';
FLUSH PRIVILEGES;
------------------------------------------------------ C 二进制包安装方式(推荐) ------------------------------------
1.下载Mysql
首先去Mysql官网下载安装包,网址 MySQL :: Download MySQL Community Server
推荐大家下载Linux通用版本的,便于管理安装位置,也方便一台服务器安装多个版本的mysql。按照自己的要选择32位还是64位。
#wget https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.33-linux-glibc2.28-x86_64.tar.gz --no-check-certificate
# tar -xvf mysql-8.0.33-linux-glibc2.28-x86_64.tar.gz
我们将其安装再mysql目录
#sudo mv mysql-8.0.33-linux-glibc2.28-x86_64 /usr/local/mysql
# cd /usr/local/mysql
1. 添加mysql用户组和用户
sudo groupadd mysql
sudo useradd -r -g mysql mysql -s /sbin/nologin
2. 在mysql下新增文件夹data,var,tmp,binlogs,logs备用
sudo mkdir data var tmp binlogs logs etc pid
sudo chmod 777 ./tmp
sudo touch logs/error.log
sudo chmod 777 ./logs
sudo touch pid/mysql.pid
3. 创建数据库配置文件 sudo vim etc/my.cnf
[mysqld]
port=3306
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/tmp/mysql.sock
#默认存储引擎
default-storage-engine=INNODB
[client]
socket=/usr/local/mysql/tmp/mysql.sock
default-character-set = utf8
[mysqld_safe]
log-error=/usr/local/mysql/logs/error.log
pid-file=/usr/local/mysql/pid/mysql.pid
这几项配置是启动所必须的,其他的可以按照自己的需求配置
port=3306
datadir =/usr/local/mysql/data
socket =/usr/local/mysql/tmp/mysql.sock
log-error =/usr/local/mysql/logs/error.log
pid-file =/usr/local/mysql/pid/mysql.pid
#将/usr/local/mysql/的所有者及所属组改为mysql
sudo chown -R mysql:mysql /usr/local/mysql/
sudo chmod -R 777 /usr/local/mysql/data
5. 初始化
# cd /usr/local/mysql/bin
# sudo ./mysqld --defaults-file=/usr/local/mysql/etc/my.cnf --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
结果:
2023-07-03T04:17:21.305200Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.33) initializing of server in progress as process 49457
2023-07-03T04:17:21.317773Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2023-07-03T04:17:23.009307Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2023-07-03T04:17:26.030773Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: I*EwKy=/g0Lm
一定要记录最后生成的初始密码 I*EwKy=/g0Lm
6. 启动新的数据库服务
sudo /usr/local/mysql/bin/mysqld_safe --defaults-file=/usr/local/mysql/etc/my.cnf &
7.查询服务是否启动成功并且登陆自己的ps -ef|grep mysql
[liwu@lw-centos bin]$ ps -ef|grep mysql
root 11707 8795 0 20:28 pts/0 00:00:00 sudo /usr/local/mysql/bin/mysqld_safe --defaults-file=/usr/local/mysql/etc/my.cnf
root 11708 11707 0 20:28 pts/0 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --defaults-file=/usr/local/mysql/etc/my.cnf
mysql 11858 11708 10 20:28 pts/0 00:00:01 /usr/local/mysql/bin/mysqld --defaults-file=/usr/local/mysql/etc/my.cnf --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/usr/local/mysql/logs/error.log --pid-file=/usr/local/mysql/pid/mysql.pid --socket=/usr/local/mysql/tmp/mysql.sock --port=3306
8. 连接数据库和修改密码
cd /usr/local/mysql/bin
./mysql -h127.0.0.1 -uroot -P3306 -p
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'LWlw184....';
FLUSH PRIVILEGES;
第二个选项,推荐的选项,就是创建一个新的独立管理用户,拥有所有数据库的访问权限:
CREATE USER 'liwu'@'%' IDENTIFIED WITH mysql_native_password BY 'LWlw184....';
GRANT ALL PRIVILEGES ON *.* TO 'liwu'@'%';
FLUSH PRIVILEGES;
9.开放端口(防火墙 虚拟主机端口也要开放)
sudo ufw allow 3306/tcp
sudo ufw status
10. 将support-files下的mysql.server文件内容修改成如下内容:
cd /usr/local/mysql/support-files
sudo vim mysql.server
#主要修改里面的目录路径
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
conf=/usr/local/mysql/etc/my.cnf
以及/usr/local/mysql/pid/mysql.pid
mysqld_pid_file_path=$basedir/pid/mysql.pid
mysqld_pid_file_path=$basedir/pid/mysql.pid if test -z "$mysqld_pid_file_path" then ##mysqld_pid_file_path=$datadir/`hostname`.pid ###这里##### ###mysqld_pid_file_path=$basedir/mysql.pid else case "$mysqld_pid_file_path" in /* ) ;; * ) mysqld_pid_file_path="$datadir/$mysqld_pid_file_path" ;; esac fi
开机启动
cd /usr/local/mysql/support-files
sudo ./mysql.server stop
sudo cp mysql.server /etc/init.d/mysql
sudo systemctl daemon-reload
启动
sudo service mysql start
加入软连
sudo ln -s /usr/local/mysql/bin/mysql /usr/bin/