1、源码包安装
在CentOS服务器上安装Mysql时,由于Centos自身的yum源中用Mysql的分支Mariadb代替了MySQL,所以选择rpm或tar.gz包的方式安装(可在线或离线)。
1.1、安装
查看是否有安装过mysql
有则删除rpm –qa |grep mysql
查询所有Mysql对应的文件夹并删除
whereis mysql
rm -rf /usr/bin/mysql /usr/lib64/mysql /usr/include/mysql /usr/share/mysql
/usr/share/man/man1/mysql.1.gz
查询mysql用户和用户组是否存在,不存在则创建
cat /etc/group | grep mysql
cat /etc/passwd |grep mysql
groupadd mysql
useradd -r -g mysql mysql
下载或导入Mysql安装包,下载地址:
https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
解压安装包tar -xzvf mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
移动目录并重命名mv mysql-5.7.24-linux-glibc2.12-x86_64 /usr/local/mysql
创建数据仓库目录mkdir -p /data/mysql
更改相关目录所属
chown -R mysql /usr/local/mysql
chgrp -R mysql /usr/local/mysql
chown -R mysql /data/mysql
初始化数据库(需要注意生成的临时密码):
`
bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql
`
启动SSLbin/mysql_ssl_rsa_setup --datadir=/data/mysql
1.2、配置
复制并修改配置文件mysql
cp mysql.server /etc/init.d/mysql
vim /etc/init.d/mysql
basedir=/usr/local/mysql mysql #安装路径
datadir=/data/mysql #数据存储路径
修改配置文件my.cnf
vim /etc/my.cnf
datadir=/data/mysql #数据存储路径
port=3306 #端口
启动MySQL/usr/local/mysql/support-files/mysql.server start
1.3、连接
连接数据库:mysql -hlocalhost -uroot -p
密码例如bko0)gfktskH
修改密码set password=password('root');
设置root账户的host地址(修改了才可以远程连接)
grant all privileges on *.* to 'root'@'%' identified by 'root';
flush privileges;
查看表
use mysql;
select host,user from user;
退出后打开3306端口/sbin/iptables -I INPUT -p tcp --dport 3306 -j ACCEPT
连接数据库
2、备注
2.1、问题处理
问题:mysqld_safe error: log-error set to '/var/log/mariadb/mariadb.log', however file don't exists
创建文件
mkdir /var/log/mariadb
touch /var/log/mariadb/mariadb.log
chown -R mysql:mysql /var/log/mariadb/
问题:mysqld_safe Directory '/var/lib/mysql' for UNIX socket file don't exists
创建目录并授权
mkdir /var/lib/mysql
chmod 777 /var/lib/mysql
问题:Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
查找mysql.sock文件位置find / -name mysql.sock
创建软连接ln -s /var/lib/mysql/mysql.sock /tmp
2.2、参考资料
https://www.cnblogs.com/fanshudada/p/9781794.html
https://www.jianshu.com/p/276d59cbc529
https://blog.csdn.net/weixin_38003389/article/details/90696337
https://www.jianshu.com/p/276d59cbc529
https://blog.csdn.net/qq_24347541/article/details/88734308
https://blog.csdn.net/colsum/article/details/89226964
https://blog.csdn.net/qq_32331073/article/details/76229420
https://blog.csdn.net/u010457406/article/details/78171441