Linux下MySql的安装

1.下载

可以官网下载也可以通过wget下载
官网地址:https://dev.mysql.com/downloads/mysql/5.6.html#downloads

cd /usr/local/
wget https://cdn.mysql.com//Downloads/MySQL-5.6/mysql-5.6.41-linux-glibc2.12-x86_64.tar.gz

下载可能失败,请登录官网查看最新版本号,然后将以上的版本号5.6.41修改为最新的版本号就可以下载了

2.解压并重命名文件夹

tar -xvf mysql-5.6.41-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.6.41-linux-glibc2.12-x86_64 mysql

3.添加系统mysql组和mysql用户

groupadd mysql
useradd -r -g mysql -s /bin/false mysql

4.进入mysql目录更改权限

cd mysql
chown -R mysql:mysql ./

5.安装数据库,此处可能出现错误

./scripts/mysql_install_db --user=mysql
  • 可能错误1
    FATAL ERROR: please install the following Perl modules before executing ./scripts/mysql_install_db:Data::Dumper
    解决方法:
yum install -y perl-Data-Dumper
  • 可能错误2
    linux libaio.so.1: cannot open shared object file: No such file or directory
    解决方法:
yum install libaio*

6.修改当前目录拥有者为root用户,修改data目录拥有者为mysql

chown -R root:root ./
chown -R mysql:mysql data

MySql安装结束


7.添加mysql服务开机自启动

cp support-files/mysql.server /etc/init.d/mysql
# 赋予可执行权限
chmod +x /etc/init.d/mysql
# 添加服务
chkconfig --add mysql 
# 显示服务列表
chkconfig --list

如果看到mysql的服务,并且3,4,5都是on的话则成功,如果是off,则执行

chkconfig --level 345 mysql on

8.启动、关闭、重启MySql

 service mysql start
 service mysql stop
 service mysql restart

执行开启mysql

service mysql start
  • 可能错误1
    Starting MySQL.180822 21:33:57 mysqld_safe error: log-error set to '/var/log/mariadb/mariadb.log', however file don't exists.Create writable for user 'mysql'.The server quit without updating PID file (/var/lib/mysql/i[FAILED]c2bbcfdqcpcrcz.pid).
    解决方法:
mkdir /var/log/mariadb 
touch /var/log/mariadb/mariadb.log 
chown -R mysql:mysql  /var/log/mariadb/

以上命令不行可以使用以下命令启动

sudo -u mysql service mysql start

9.本地连接数据库

由于系统默认会查找/usr/bin下的命令,如果这个命令不在这个目录下,当然会找不到命令,我们需要做的就是映射一个链接到/usr/bin目录下,相当于建立一个链接文件

ln -s /usr/local/mysql/bin/mysql /usr/bin
ln -s /usr/local/mysql/bin/mysqladmin /usr/bin

设置密码

mysqladmin -u root password "newpwd"

连接数据库

mysql -u root -p  

输入密码报错
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
解决办法

ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock 

10.远程连接数据库

开启远程访问权限,需要在防火墙中开端口,具体操作查看【13.】

#先登录mysql
mysql -u root -p 
#设置所有ip可以登录mysql
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;   
#grant all privileges on 库名.表名 to '用户名'@'IP地址' identified by '密码' with grant option;
#刷新权限 
FLUSH PRIVILEGES;

修改默认端口号

11.先登录mysql ,查看当前端口

mysql> show global variables like 'port';  

12.编辑mysql配置文件

/etc/my.cnf

在第二行新增端口配置

[mysqld]  
datadir=/var/lib/mysql  

新增后如下

[mysqld]  
port=3506  
datadir=/var/lib/mysql  

修改后重启mysql生效

 service mysql restart

13.开放端口

  • 防火墙命令
# 关闭防火墙
systemctl stop firewalld.service
# 开启防火墙
systemctl start firewalld
# 查看防火墙状态
systemctl status firewalld
  • 开启3506端口
firewall-cmd --zone=public --add-port=3506/tcp --permanent   # 开放3506端口
firewall-cmd --zone=public --remove-port=3506/tcp --permanent  #关闭3506端口
firewall-cmd --reload   # 配置立即生效

查看防火墙所有开放的端口

firewall-cmd --zone=public --list-ports
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容