ubuntu16.04系统彻底卸载mysql,并源码免编译重装MySQL的步骤

今天的总结

ubuntu上彻底卸载MySQL或重新安装

https://www.jianshu.com/p/974b33873bca


#查看是否存在mysql服务
service mysql status

#查看安装了MySQL的相关软件包
dpkg --get-selections | grep mysql

#卸载MySQL
apt-get remove --purge mysql-*

#清除本地残留文件
apt autoclean

#清除无用依赖包(可选)
apt autoremove

#删除MySQL残留文件
rm -rf /etc/mysql /var/lib/mysql


免编译安装MySQL的方法


安装必要的依赖包
apt-get install libaio1

添加mysql用户 解压MySQL安装包
[root@localhost ~]# groupadd mysql
[root@localhost ~]# useradd -r -g mysql -s /bin/false mysql
[root@localhost ~]# mkdir -p /data
[root@localhost ~]# cd /data
[root@localhost data]# tar zxf mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz
[root@localhost data]# ln -s mysql-5.7.23-linux-glibc2.12-x86_64 mysql
[root@localhost data]# 

建立存放日志等目录
cd /data/mysql-5.7.23-linux-glibc2.12-x86_64/
mkdir logs binlogs run


删除旧配置文件
rm -rf /etc/my.cnf  /etc/mysql/my.cnf  /usr/local/mysql/etc/my.cnf  ~/.my.cnf


新增MySQL的配置文件/etc/my.cnf

cat > /etc/my.cnf << EOF
[client]
default-character-set = utf8mb4
port= 3306
socket= /data/mysql/run/mysql.sock


[mysql]
default-character-set = utf8mb4
prompt = " \u@\h \R:\m:\s [\d]> "
no-auto-rehash
socket= /data/mysql/run/mysql.sock

[mysqld]
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
character-set-client-handshake = FALSE

server-id = 3306
user= mysql
port= 3306
basedir= /data/mysql
datadir= /data/mysql/data
socket= /data/mysql/run/mysql.sock
pid-file = /data/mysql/run/mysqld.pid

skip_name_resolve = 1
open_files_limit = 65535
back_log = 1024
max_connections = 1024
log-error = /data/mysql/logs/mysql-error.log
EOF



初始化MySQL

[root@localhost data]# cd /data/mysql
[root@localhost mysql]# mkdir mysql-files
[root@localhost mysql]# chown mysql:mysql mysql-files
[root@localhost mysql]# chmod 750 mysql-files
[root@localhost mysql]# chown -R mysql:mysql .
[root@localhost mysql]# bin/mysqld --initialize --user=mysql  --basedir=/data/mysql --datadir=/data/mysql/data
[root@localhost mysql]# bin/mysql_ssl_rsa_setup --datadir=/data/mysql/data




ubuntu的unit file路径/lib/systemd/system

cat > /lib/systemd/system/mysqld.service << EOF
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target

[Install]
WantedBy=multi-user.target

[Service]
User=mysql
Group=mysql

Type=forking

PIDFile=/data/mysql/run/mysqld.pid

# Disable service start and stop timeout logic of systemd for mysqld service.
TimeoutSec=0

# Execute pre and post scripts as root
PermissionsStartOnly=true

# Needed to create system tables
#ExecStartPre=/usr/bin/mysqld_pre_systemd

# Start main service
ExecStart=/data/mysql/bin/mysqld --daemonize --pid-file=/data/mysql/run/mysqld.pid $MYSQLD_OPTS

# Use this to switch malloc implementation
EnvironmentFile=-/etc/sysconfig/mysql

# Sets open_files_limit
LimitNOFILE = 65535

Restart=on-failure

RestartPreventExitStatus=1

PrivateTmp=false
EOF


配置MySQL环境变量

echo "export PATH=$PATH:/data/mysql/bin" >> /etc/profile
source /etc/profile



启动与开机启动MySQL
systemctl daemon-reload
systemctl start mysqld.service
systemctl enable mysqld.service
systemctl is-enabled mysqld



用临时密码登录并修改临时密码
[root@localhost ~]# grep 'temporary password' /data/mysql/logs/mysql-error.log
2018-09-24T07:54:08.055154Z 1 [Note] A temporary password is generated for root@localhost: p=lXlKKrg4GJ
[root@localhost ~]# mysql -urrott -p "p=lXlKKrg4GJ"
mysql shell > alter user root@'localhost' identified by 'root12#$';
mysql shell > \q

 

posted @ 2019-02-19 20:03  峰哥ge  阅读(534)  评论(0编辑  收藏  举报