一、CentOS7.6系统自带mariadb
# 查看系统自带的Mariadb
[root@vmtest ~]# rpm -qa|grep mariadb
mariadb-libs-5.5.44-2.el7.centos.x86_64
# 卸载系统自带的Mariadb
[root@vmtest ~]# rpm -e --nodeps mariadb-libs-5.5.44-2.el7.centos.x86_64
# 删除etc目录下的my.cnf
[root@vmtest ~]# rm /etc/my.cnf
二、检查mysql是否存在
# 检查mysql是否存在
[root@VMTest ~]# rpm -qa | grep mysql
[root@VMTest ~]#
三、查看用户和组是否存在
1)检查mysql组合用户是否存在
# 检查mysql组和用户是否存在,如无则创建
[root@VMTest ~]# cat /etc/group | grep mysql
[root@VMTest ~]# cat /etc/passwd | grep mysql
# 查询全部用户(只是做记录,没必要执行)
[root@VMTest ~]# cat /etc/passwd|grep -v nologin|grep -v halt|grep -v shutdown|awk -F ":" '{print $1 "|" $3 "1" $4}' | more
root|010
sync|510
mysql|99711001
2)若不存在,则创建mysql组和用户
# 创建mysql用户组
[root@VMTest ~]# groupadd mysql
# 创建一个用户名为mysql的用户,并加入mysql用户组
[root@VMTest ~]# useradd -g mysql mysql
# 制定password 为111111
[root@VMTest ~]# passwd mysql
Changing password for user mysql.
New password:
BAD PASSWORD: The password is a palindrome
Retype new password:
passwd: all authentication tokens updated successfully.
四、下载mysql的tar包
# 网页地址: https://downloads.mysql.com/archives/community/
https://dev.mysql.com/downloads/mysql/5.7.html#downloads
五、上传TAR包
上传第四步下载的mysql TAR包
# 进入/usr/local/src文件夹
[root@VMTest ~]# cd /usr/local/src/
# 上传mysql TAR包
[root@VMTest src]# rz
# 解压tar文件
[root@VMTest src]# tar xvf mysql-5.7.22-el7-x86_64.tar
# 解压mysql-5.7.22-el7-x86_64.tar.gz
[root@VMTest src]# tar -zxvf mysql-5.7.22-el7-x86_64.tar.gz
# 解压后的文件移动到/usr/local文件夹
[root@VMTest src]# mv mysql-5.7.22-el7-x86_64 /usr/local
# 进入/usr/local下,修改为mysql
[root@VMTest src]# cd /usr/local
[root@VMTest local]# mv mysql-5.7.22-el7-x86_64 mysql
六、更改所属的组和用户
# 更改所属的组和用户
[root@VMTest local]# chown -R mysql mysql/
[root@VMTest local]# chgrp -R mysql mysql/
[root@VMTest local]# cd mysql/
[root@VMTest mysql]# mkdir /data/mysql -p
[root@VMTest mysql]# chown -R mysql:mysql /data/mysql
七、安装mysql
进入mysql文件夹,并安装mysql
# 进入mysql
[root@VMTest etc]# cd /usr/local/mysql/
# 安装mysql
[root@VMTest mysql]# bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
or(推荐):
[root@VMTest mysql]# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
2018-07-04 15:46:02 [WARNING] 5mysql_install_db is deprecated. Please consider switching to mysqld --initialize
2018-07-04 15:46:05 [WARNING] The bootstrap log isn't empty:
2018-07-04 15:46:05 [WARNING] 2018-07-04T15:46:02.728710Z 0 [Warning] --bootstrap is deprecated. Please consider using --initialize instead
2018-07-01T15:46:02.729161Z 0 [Warning] Changed limits: max_open_files: 1024 (requested 5000)
2018-07-04 T15:46:02.729167Z 0 [Warning] Changed limits: table_open_cache: 407 (requested 2000)
1.安装数据库后,初始化报错
[root@db01 /data/app] mysqld --initialize-insecure --user=mysql --basedir=/data/app/mysql --datadir=/data/3306/data
mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
2.报错问题:加载共享库时出错:libaio.so。1:无法打开共享对象文件:没有这样的文件或目录
mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
3.解决方法:安装 libaio 包
[root@db01 /data/app] yum install libaio -y
[root@VMTest mysql]# cp ./support-files/mysql.server /etc/init.d/mysqld
[root@VMTest mysql]# chmod +x /etc/init.d/mysqld
八、配置my.cnf
在/etc下创建my.cnf文件
# 进入/etc文件夹下
[root@VMTest mysql]# cd /etc
# 创建my.cnf文件
[root@VMTest etc]# touch my.cnf
[root@VMTest mysql]# chown 744 /etc/my.cnf
# 编辑my.cnf
[root@VMTest etc]# vim my.cnf
1)my.cnf添加如下内容:
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8
[mysqld]
character-set-server=utf8
# 设置3306端口
port = 3306
skip-grant-tables
# 设置mysql的安装目录
basedir=/usr/local/mysql
explicit_defaults_for_timestamp=ON
# 设置mysql数据库的数据的存放目录
datadir=/usr/local/mysql/data
# 允许最大连接数
max_connections=200
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
lower_case_table_names=1
max_allowed_packet=16M
2)查看my.cnf内容
# 查看my.cnf文件
[root@data-vm1 mysql]# cat /etc/my.cnf
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8
character-set-server=utf8
[mysqld]
# 设置3306端口
port = 3306
skip-grant-tables
# 设置mysql的安装目录
basedir=/usr/local/mysql
explicit_defaults_for_timestamp=ON
# 设置mysql数据库的数据的存放目录
datadir=/usr/local/mysql/data
# 允许最大连接数
max_connections=200
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
lower_case_table_names=1
max_allowed_packet=16M
九、启动mysql
# 启动mysql
[root@VMTest mysql]# /etc/init.d/mysqld start
解决:
# 1、查看进程
[root@VMTest mysql]# ps aux|grep mysql
root 10031 0.0 0.1 113264 1616 pts/0 S 14:36 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-file=/usr/local/mysql/data/iZ2ze3hm3gyjyjz628l7rgZ.pid
mysql 10220 0.0 19.1 1140876 195072 pts/0 Sl 14:36 0:02 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=iZ2ze3hm3gyjyjz628l7rgZ.err --pid-file=/usr/local/mysql/data/iZ2ze3hm3gyjyjz628l7rgZ.pid --port=3306
root 10421 0.0 0.0 112660 968 pts/0 R+ 15:51 0:00 grep --color=auto mysql
# 2、杀死进程
[root@VMTest mysql]# kill -9 10031
[root@VMTest mysql]# kill -9 10220
# 3、重启mysql
[root@VMTest mysql]# /etc/init.d/mysqld restart
Shutting down MySQL..
Starting MySQL.
十、设置开机启动
#设置开机启动
[root@VMTest mysql]# systemctl enable mysqld
# old
[root@VMTest mysql]# chkconfig --level 35 mysqld on
[root@VMTest mysql]# chkconfig --list mysqld
[root@VMTest mysql]# chmod +x /etc/rc.d/init.d/mysqld
[root@VMTest mysql]# chkconfig --add mysqld
[root@VMTest mysql]# chkconfig --list mysqld
[root@VMTest mysql]# service mysqld status
SUCCESS! MySQL running (4475)
十一、修改配置文件
# 进入/etc/profile文件夹
[root@VMTest mysql]# vim /etc/profile
1)修改/etc/profile,在最后添加如下内容
# 修改/etc/profile文件
#set mysql environment
export PATH=$PATH:/usr/local/mysql/bin
# 使文件生效
[root@VMTest mysql]# source /etc/profile
十二、获得mysql初始密码
# 1、获得mysql初始密码
[root@VMTest bin]# cat /root/.mysql_secret
# Password set for user 'root@localhost' at 2017-04-17 17:40:02
_pB*3VZl5T<6
# 2、修改密码
[root@VMTest bin]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 53
Server version: 5.7.22 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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> update user set authentication_string=password('root123') where user='root';
# or
mysql> set PASSWORD = PASSWORD('root123');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
# 3、执行步骤2时如果报错:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
解决方案:
3.1 重置密码的第一步就是跳过MySQL的密码认证过程,方法如下:
#vim /etc/my.cnf(注:windows下修改的是my.ini)
在[mysqld]后面任意一行添加“skip-grant-tables”用来跳过密码验证,
保存文档并退出:
3.2 重启MySQL:
service mysqld restart
#or
/etc/init.d/mysql restart
3.3 重启之后输入#mysql即可进入mysql命令行
3.4 接下来就是用sql来修改root的密码
mysql> use mysql;
# mydql5.7 密码password 修改为了 authentication_string
mysql> update mysql.user set authentication_string=password('root123') where user='root';
# old
mysql> update user set password=password("root123") where user="root";
mysql> flush privileges;
mysql> quit
3.5 编辑my.cnf,去掉刚才添加的内容,然后重启MySQL: service mysqld restart
十三、添加远程访问权限
# 添加远程访问权限
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> alter user user() identified by "root";
mysql> use mysql
mysql> update user set host='%' where user='root';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 0
mysql> select host,user from user;
+-----------+---------------+
| host | user |
+-----------+---------------+
| % | root |
| localhost | mysql.session |
| localhost | mysql.sys |
+-----------+---------------+
3 rows in set (0.00 sec)
mysql> quit
十四、重启mysql生效
# 重启mysql
[root@VMTest bin]# service mysql restart
# or
[root@VMTest bin]# /etc/init.d/mysqld restart
Shutting down MySQL..
Starting MySQL.
备注:
由于安装在/usr/local下面的mysql,因此可以在任何文件夹启动mysql
若安装在别的文件夹,请执行以下命令:
# 为了在任何目录下可以登录mysql
ln -s /你的mysql路径/mysql /usr/local/mysql
十五、添加新用户
1. 进入mysql命令行
[root@iZ8vb7dfqoy56ogjiklg4kZ ~]# mysql -uroot -p
2. 选择mysql数据库
mysql> use mysql
3. 创建用户,比如用户名是demo,则使用
mysql> create user demo;
4. 给用户权限: https://www.cnblogs.com/felix-h/p/11072743.html
# 把操作数据库的所有权限给了demo,并且设置密码是password
mysql> grant all on *.* to 'demo'@'localhost' identified by 'password';
# 如果希望该用户可以创建新的用户,并授权,就使用添加with grant option
mysql> grant all on *.* to 'demo'@'localhost' identified by 'password' with grant option;
# 授权允许远程访问
mysql> grant all privileges on *.* to 'root'@'%' identified by '@wjb13191835106';
# 授予指定权限
mysql> grant select,create,drop,update,alter on *.* to 'fiend'@'%' identified by 'fiend123' with grant option;
# use
mysql> grant select,create,drop,update,alter on *.* to 'atlassian'@'%' identified by 'atlassian123';
# 查询授予的权限
mysql> show grants for 'fiend'@'localhost';
# 关闭授权
mysql> revoke all on *.* from fiend@localhost;
5. 刷新下, 退出
mysql> flush privileges;
mysql> quit