删除MySQL相关文件
find / -name mysql|xargs rm -rf
此操作存在风险,会删除所有名称包含mysql的文件,最好查找后手动删除
下载
可以在https://downloads.mysql.com/archives/community下载文件
选择操作系统时,最好选择Linux – Generic
我下载的是mysql-5.5.62-linux-glibc2.12-x86_64.tar.gz
也可以从我的阿里云oss下载
wget https://file-wsg.oss-cn-shanghai.aliyuncs.com/Linux/mysql-5.5.62-linux-glibc2.12-x86_64.tar.gz
解压
tar -zxvf mysql-5.5.62-linux-glibc2.12-x86_64.tar.gz
解压完成后可以移动文件夹:
mv mysql-5.5.62-linux-glibc2.12-x86_64 /usr/local/mysql
rm -rf mysql-5.5.62-linux-glibc2.12-x86_64.tar.gz
添加用户组和用户
先检查是否有mysql用户组和mysql用户
groups mysql
若有,则跳过这一步;若无,则执行:
groupadd mysql
useradd -r -g mysql mysql
进入mysql目录更改权限
cd /usr/local/mysql
chown -R mysql:mysql ./
执行安装脚本
已安装一个依赖,如果已经安装,可以跳过:
yum install -y libaio
执行安装脚本:
./scripts/mysql_install_db --user=mysql
安装完之后修改当前目录拥有者为root用户,修改data目录拥有者为mysql
chown -R root:root ./
chown -R mysql:mysql data
启动mysql
先处理一个权限问题:
mkdir /var/log/mariadb
touch /var/log/mariadb/mariadb.log
chown -R mysql:mysql /var/log/mariadb/
启动:
./support-files/mysql.server start
如果失败,可能需要杀进程:
ps aux|grep mysql|awk '{print$2}'|xargs kill -9
更改mysql密码
./bin/mysqladmin -u root -h localhost.localdomain password '123456'
密码更改后即可登录MySQL:
./bin/mysql -h127.0.0.1 -uroot -p123456
登录之后将其他用户的密码也可改为root:
update mysql.user set password=password('123456') where user='root';
flush privileges;
增加远程登录权限
grant all privileges on *.* to root@'%' identified by '123456';
flush privileges;
执行之后即可远程登录
将MySQL加入Service系统服务
cp support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on
service mysqld restart
配置到环境变量
vim ~/.bash_profile
export PATH=$PATH:/usr/local/mysql/bin
source ~/.bash_profile
启动、停止、重启的命令
service mysqld start
service mysqld stop
service mysqld restart
解决Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’
ln /var/lib/mysql/mysql.sock /tmp/mysql.sock
Reference
Linux下安装MySQL
Linux安装MySQL时候出现log-error set to ‘/var/log/mariadb/mariadb.log’, however file don’t exists.
libaio.so.1: cannot open shared object file解决方法
亲测有效,解决Can ‘t connect to local MySQL server through socket ‘/tmp/mysql.sock ‘(2) “;
如需安装高版本的MySQL,可参考 Centos下安装mysql8.0详细步骤
发表评论