在Linux上安装MySQL

实验环境

  • CentOS/Ubuntu系统;
  • 系统架构:64位
  • MySQL版本:5.7.17

相关网址

安装MySQL服务

CentOS系统

  • 下载系统对应版本的rpm-bundle包;

CentOS-6

1
$ tar -xf mysql-5.7.17-1.el6.x86_64.rpm-bundle.tar
  • 安装依赖包:
1
$ yum install -y libaio numactl
  • 卸载旧版本的mysql包:
1
$ rpm -e --nodeps $(rpm -qa | grep mysql)
  • 安装MySQL服务:
1
2
3
4
5
$ rpm -ivh mysql-community-common-5.7.17-1.el6.x86_64.rpm
$ rpm -ivh mysql-community-libs-5.7.17-1.el6.x86_64.rpm
$ rpm -ivh mysql-community-libs-compat-5.7.17-1.el6.x86_64.rpm
$ rpm -ivh mysql-community-client-5.7.17-1.el6.x86_64.rpm
$ rpm -ivh mysql-community-server-5.7.17-1.el6.x86_64.rpm

CentOS-7

1
$ tar -xf mysql-5.7.17-1.el7.x86_64.rpm-bundle.tar
  • 安装依赖包:
1
$ yum install -y libaio numactl
  • 卸载旧版本的mariadb包:
1
$ rpm -e --nodeps $(rpm -qa | grep mariadb)
  • 安装MySQL服务:
1
2
3
4
5
$ rpm -ivh mysql-community-common-5.7.17-1.el7.x86_64.rpm
$ rpm -ivh mysql-community-libs-5.7.17-1.el7.x86_64.rpm
$ rpm -ivh mysql-community-libs-compat-5.7.17-1.el7.x86_64.rpm
$ rpm -ivh mysql-community-client-5.7.17-1.el7.x86_64.rpm
$ rpm -ivh mysql-community-server-5.7.17-1.el7.x86_64.rpm

CentOS-All

  • 创建数据目录:
1
2
$ mkdir -p /data/mysql/{data,tmp,log}
$ chown -R mysql:mysql /data/mysql/{data,tmp,log}

Ubuntu系统

  • 下载系统对应版本的deb-bundle包;

Ubuntu-14.04

1
$ tar -xf mysql-server_5.7.17-1ubuntu14.04_amd64.deb-bundle.tar
  • 安装依赖包:
1
$ apt install -y libaio1 libaio-dev libmecab2 libmecab-dev
  • 安装MySQL服务(弹窗设置密码):
1
2
3
4
5
6
7
8
$ dpkg -i mysql-common_5.7.17-1ubuntu14.04_amd64.deb
$ dpkg -i libmysqlclient20_5.7.17-1ubuntu14.04_amd64.deb
$ dpkg -i libmysqlclient-dev_5.7.17-1ubuntu14.04_amd64.deb
$ dpkg -i libmysqld-dev_5.7.17-1ubuntu14.04_amd64.deb
$ dpkg -i mysql-community-client_5.7.17-1ubuntu14.04_amd64.deb
$ dpkg -i mysql-client_5.7.17-1ubuntu14.04_amd64.deb
$ dpkg -i mysql-community-server_5.7.17-1ubuntu14.04_amd64.deb
$ dpkg -i mysql-server_5.7.17-1ubuntu14.04_amd64.deb
  • 停止MySQL服务:
1
$ service mysql stop

Ubuntu-16.04

1
$ tar -xf mysql-server_5.7.17-1ubuntu16.04_amd64.deb-bundle.tar
  • 安装依赖包:
1
$ apt install -y libaio1 libaio-dev libmecab2 libmecab-dev
  • 安装MySQL服务(弹窗设置密码):
1
2
3
4
5
6
7
8
$ dpkg -i mysql-common_5.7.17-1ubuntu16.04_amd64.deb
$ dpkg -i libmysqlclient20_5.7.17-1ubuntu16.04_amd64.deb
$ dpkg -i libmysqlclient-dev_5.7.17-1ubuntu16.04_amd64.deb
$ dpkg -i libmysqld-dev_5.7.17-1ubuntu16.04_amd64.deb
$ dpkg -i mysql-community-client_5.7.17-1ubuntu16.04_amd64.deb
$ dpkg -i mysql-client_5.7.17-1ubuntu16.04_amd64.deb
$ dpkg -i mysql-community-server_5.7.17-1ubuntu16.04_amd64.deb
$ dpkg -i mysql-server_5.7.17-1ubuntu16.04_amd64.deb
  • 停止MySQL服务:
1
$ systemctl stop mysql.service

Ubuntu-All

  • 迁移数据存储目录:
1
2
3
4
$ mkdir -p /data/mysql/{data,tmp,log}
$ chown -R mysql:mysql /data/mysql/{data,tmp,log}
$ cp -a /var/lib/mysql/* /data/mysql/data
$ rm -rf /var/lib/mysql
  • 更改系统限制:
1
$ vim /etc/apparmor.d/usr.sbin.mysqld
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Allow pid, socket, socket lock file access
/data/mysql/tmp/mysqld.pid rw,
/data/mysql/tmp/mysql.sock rw,
/data/mysql/tmp/mysql.sock.lock rw,
/run/mysqld/mysqld.pid rw,
/run/mysqld/mysqld.sock rw,
/run/mysqld/mysqld.sock.lock rw,

# Allow data dir access
/data/mysql/data/ r,
/data/mysql/data/** rwk,

# Allow log file access
/data/mysql/log/ r,
/data/mysql/log/** rw,

# Allow tmp file access
/data/mysql/tmp/ r,
/data/mysql/tmp/** rw,
  • 重新加载系统配置:
1
$ service apparmor reload

配置MySQL服务

CentOS系统

  • 配置MySQL服务:
1
$ vim /etc/my.cnf

Ubuntu系统

  • 配置MySQL服务:
1
$ vim /etc/mysql/my.cnf

All

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[client]
port = 3306
socket = /data/mysql/tmp/mysql.sock
default-character-set = utf8mb4

[mysqld]
port = 3306
datadir = /data/mysql/data
pid-file = /data/mysql/tmp/mysqld.pid
socket = /data/mysql/tmp/mysql.sock
log-error = /data/mysql/log/error.log
character_set_server = utf8mb4
user = mysql
bind-address = *
server-id = 1
symbolic-links=1
connect_timeout = 3600
wait_timeout = 3600
interactive_timeout = 3600
explicit_defaults_for_timestamp = true

启动MySQL服务

CentOS系统

CentOS-6

1
$ service mysqld start

CentOS-7

1
$ systemctl start mysqld.service

CentOS-All

  • 查看MySQL服务端口:
1
$ netstat -nlput | grep "mysqld"

Ubuntu系统

Ubuntu-14

1
$ service mysql start

Ubuntu-16

1
$ systemctl start mysql.service

Ubuntu-All

  • 查看MySQL服务端口:
1
$ netstat -nlput | grep "mysql"

All

  • 获取MySQL的版本信息:
1
$ mysql -V
  • 创建软链接:
1
$ ln -s /data/mysql/tmp/mysql.sock /tmp/mysql.sock

登入MySQL服务

CentOS系统

CentOS-All

  • 获取自动生成的随机密码:
1
$ MySQL_PASS=$(cat /data/mysql/log/error.log | grep "A temporary password" | awk '{print $NF}')
  • 登录到MySQL数据库:
1
$ mysql -u root -p"${MySQL_PASS}"
  • 修改密码(密码必须满足一定的复杂度, 包含大小写字母, 数字, 长度满足8位):
1
SET PASSWORD='NEW_PASSWORD';
  • 退出登录:
1
exit

CentOS-6

  • 停止MySQL服务:
1
$ service mysqld stop
  • 重启MySQL服务:
1
$ service mysqld restart
  • 获取MySQL服务状态:
1
$ service mysqld status
  • 启用开机自启MySQL服务:
1
$ chkconfig mysqld on
  • 禁用开机自启MySQL服务:
1
$ chkconfig mysqld off
  • 查看MySQL服务开启自启的状态:
1
$ chkconfig --list mysqld

CentOS-7

  • 停止MySQL服务:
1
$ systemctl stop mysqld.service
  • 重启MySQL服务:
1
$ systemctl restart mysqld.service
  • 获取MySQL服务状态:
1
$ systemctl status mysqld.service
  • 启用开机自启MySQL服务:
1
$ systemctl enable mysqld.service
  • 禁用开机自启MySQL服务:
1
$ systemctl disable mysqld.service
  • 查看MySQL服务状态:
1
$ systemctl status mysqld.service

Ubuntu系统

Ubuntu-All

  • 登录到MySQL数据库:
1
$ mysql -u root -p'密码'
  • 修改密码(密码必须满足一定的复杂度, 包含大小写字母, 数字, 长度满足8位):
1
SET PASSWORD='NEW_PASSWORD';
  • 退出登录:
1
exit

Ubuntu-14

  • 重启MySQL服务:
1
$ service mysqld restart
  • 获取MySQL服务状态:
1
$ service mysqld status
  • 启用开机自启MySQL服务:
1
$ update-rc.d mysql defaults
  • 禁用开机自启MySQL服务:
1
$ update-rc.d -f mysql remove

Ubuntu-16

  • 重启MySQL服务:
1
$ systemctl restart mysql.service
  • 获取MySQL服务状态:
1
$ systemctl status mysql.service
  • 启用开机自启MySQL服务:
1
$ systemctl enable mysql.service
  • 禁用开机自启MySQL服务:
1
$ systemctl disable mysql.service

有你就有世界,感谢有你,昕!
0%