linux下安装mysql有三种方式:①yum安装 ②rpm安装 ③源码包安装
这里介绍第三种安装方式
操作系统:centos7.4
mysql版本:5.7.26,安装时注意版本区别,不同版本安装配置稍有差异。
若之前装过mysql请先删除再进行安装
[root@VM_0_14_centos ~]# rpm -qa|grep mariadb
mariadb-libs-5.5.56-2.el7.x86_64
[root@VM_0_14_centos ~]# rpm -e --nodeps mariadb-libs-5.5.64-1.el7.x86_64
[root@VM_0_14_centos ~]#
#删除/etc下my.cnf配置文件(如果存在的话)、检查mysql是否存在
[root@VM_0_14_centos ~]# rm -rf /etc/my.cnf
[root@VM_0_14_centos ~]# rpm -qa | grep mysql
下载页面:https://dev.mysql.com/downloads/mysql/
下载完成后上传服务器
解压安装包
[root@VM_0_14_centos download]# tar -zxvf mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
如果压缩包名是以 …tar 结尾,则解压方式为
[root@VM_0_14_centos download]# ta -xvf mysql-5.7.26-linux-glibc2.12-x86_64.tar
修改解压出来的文件夹移动到 /usr/local/mysql
[root@VM_0_14_centos download]# mv /usr/local/download/mysql-5.7.26-linux-glibc2.12-x86_64 /usr/local/mysql
进入mysql目录,新建data目录
[root@VM_0_14_centos download]# cd /usr/local/mysql/
[root@VM_0_14_centos mysql]# mkdir data
创建系统用户组和用户
[root@VM_0_14_centos mysql]# cat /etc/group --查看所有用户组信息是否存在mysql组,不存在则创建,存在则直接新建用户
[root@VM_0_14_centos mysql]# groupadd mysql ---新建一个msyql组
[root@VM_0_14_centos mysql]# cat /etc/passwd|grep mysql ---查看是否存在mysql用户
[root@VM_0_14_centos mysql]# useradd -g mysql mysql ---新建msyql用户
修改当前目录权限
[root@VM_0_14_centos mysql]# chown -R mysql:mysql /usr/local/mysql/
初始化数据库
[root@VM_0_14_centos bin]# ./mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
./mysqld: error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory
若报如下错
./mysqld: error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory
yun安装numactl
[root@VM_0_14_centos bin]# yum remove libnuma.so.1;
[root@VM_0_14_centos bin]# yum -y install numactl.x86_64
重新初始化成功,:最后是初始化 root 登录密码
[root@VM_0_14_centos bin]# ./mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
2019-06-29T05:24:18.538969Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-06-29T05:24:19.722625Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-06-29T05:24:19.876417Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-06-29T05:24:19.979084Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 257f6850-9a2e-11e9-9c9e-525400dc2e19.
2019-06-29T05:24:19.988044Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2019-06-29T05:24:19.988863Z 1 [Note] A temporary password is generated for root@localhost: 8w.9!6U4%m3a
将 mysql 加入服务
[root@VM_0_14_centos bin]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
配置my.cnf
搜索my.cnf
[root@VM_0_14_centos bin]# find / -name my.cnf
找到后复制到 /etc/my.cnf
并进行如下编辑,若找不到则在/etc下手动编辑一个
[root@VM_0_14_centos bin]# vi /etc/my.cnf
[mysqld]
bind-address = 0.0.0.0 # 表示允许任何主机登陆MySQL
port=3306 # 表示MySQL运行端口为3306
character_set_server=utf8
init_connect='SET NAMES utf8'
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
#不区分大小写
lower_case_table_names = 1
#不开启sql严格模式
sql_mode = "STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
log-error=/var/log/mysqld.log
pid-file=/usr/local/mysql/data/mysqld.pid
[root@VM_0_14_centos bin]# vi /etc/init.d/mysql
编辑其中两个属性如下
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
开机自启
[root@VM_0_14_centos bin]# chkconfig --add mysql
[root@VM_0_14_centos bin]# chkconfig mysql on
启动mysql:
[root@VM_0_14_centos bin]# service mysql stop
ERROR! MySQL server PID file could not be found!
[root@VM_0_14_centos bin]# service mysql start
Starting MySQL. SUCCESS!
查询列表:
[root@VM_0_14_centos bin]# chkconfig | grep -i mysql
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
mysql 0:off 1:off 2:on 3:on 4:on 5:on 6:off
如果看到mysql的服务,并且3,4,5都是on的话则成功,如果是off,则执行
[root@VM_0_14_centos bin]# chkconfig --level 345 mysql on
重启电脑 reboot
验证
[root@VM_0_14_centos ~]# netstat -na | grep 3306
tcp6 0 0 :::3306 :::* LISTEN
连接Mysql,使用root账号登录
[root@VM_0_14_centos ~]# cd /usr/local/mysql/bin/
[root@VM_0_14_centos bin]# ./mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.26
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
修改root密码
Server version: 5.7.26
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> set PASSWORD = PASSWORD('1234567');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql>
允许远程访问
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select host,user from user;
+-----------+---------------+
| host | user |
+-----------+---------------+
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+-----------+---------------+
3 rows in set (0.00 sec)
mysql> create user 'root'@'%' identified by '1234567'
-> ;
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql>
授权数据库操作
mysql> grant all on finance.* to 'root'@'%' identified by '1234567' with grant option;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)