#卸载系统自带的Mariadb
[root@localhost ~]# rpm -qa|grep mariadb
mariadb-libs-5.5.52-1.el7.x86_64
[root@localhost ~]# rpm -e --nodeps mariadb-libs-5.5.52-1.el7.x86_64
#删除etc目录下的my.cnf文件
#检查mysql是否存在
rpm -qa|grep -i mysql
#检查mysql组和用户是否存在
[root@localhost local]# cat /etc/group | grep mysql
[root@localhost local]# cat /etc/passwd | grep mysql
删除mysql用户及用户组
[root@localhost ~]# more /etc/passwd | grep mysql
mysql:x:1001:1001::/home/mysql:/bin/bash
[root@localhost ~]# more /etc/shadow | grep mysql
mysql:!!:17731:0:99999:7:::
[root@localhost ~]# more /etc/shadow | grep mysql
mysql:!!:17731:0:99999:7:::
[root@localhost ~]# userdel mysql
[root@localhost ~]# groupdel mysql
groupdel: group 'mysql' does not exist
#检查mysql的所有文件
find / -name mysql
#创建mysql用户组
[root@localhost local]# groupadd mysql
#创建一个用户名为mysql的用户并加入mysql用户组
[root@localhost local]# useradd -g mysql mysql
#创建 mysql 文件
#更改所属的组和用户
[root@localhost local]# chown -R mysql mysql/
[root@localhost local]# chgrp -R mysql mysql/
#在mysql文件夹下创建 数据库数据存储目录 data
[root@localhost local]# cd mysql/
[root@localhost mysql]# mkdir data
[root@localhost mysql]# chown -R mysql:mysql data
#在etc下新建配置文件my.cnf,并在该文件内添加以下配置
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8
[mysqld]
skip-name-resolve
#设置3306端口
port = 3306
# 设置mysql的安装目录
basedir=/usr/local/mysql
# 设置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
#安装和初始化
[root@localhost mysql]# bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
2018-07-21 16:00:04 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize
2018-07-21 16:00:07 [WARNING] The bootstrap log isn't empty:
2018-07-21 16:00:07 [WARNING] 2018-07-21T08:00:04.940039Z 0 [Warning] --bootstrap is deprecated. Please consider using --initialize instead
2018-07-21T08:00:04.941048Z 0 [Warning] Changed limits: max_open_files: 1024 (requested 5000)
2018-07-21T08:00:04.941059Z 0 [Warning] Changed limits: table_open_cache: 407 (requested 2000)
[root@localhost mysql]# cp ./support-files/mysql.server /etc/init.d/mysqld
cp: overwrite ‘/etc/init.d/mysqld/mysql.server’? y
#将脚本文件设置成可执行文件
[root@localhost mysql]# chown 777 /etc/my.cnf
[root@localhost mysql]# chmod a+x /etc/init.d/mysqld
#设置开机启动
如何增加一个服务:
1.服务脚本必须存放在/etc/ini.d/目录下;
2.chkconfig --add servicename
在chkconfig工具服务列表中增加此服务,此时服务会被在/etc/rc.d/rcN.d中赋予K/S入口了;
3.chkconfig --level 35 mysqld on
修改服务的默认启动等级。
使用范例:
chkconfig --list #列出所有的系统服务
chkconfig --add httpd #增加httpd服务
chkconfig --del httpd #删除httpd服务
chkconfig --level httpd 2345 on #设置httpd在运行级别为2、3、4、5的情况下都是on(开启)的状态
chkconfig --list #列出系统所有的服务启动情况
chkconfig --list mysqld #列出mysqld服务设置情况
chkconfig --level 35 mysqld on #设定mysqld在等级3和5为开机运行服务,--level 35表示操作只在等级3和5执行,on表示启动,off表示关闭
chkconfig mysqld on #设定mysqld在各等级为on,“各等级”包括2、3、4、5等级
[root@localhost mysql]# chkconfig --level 35 mysqld on
查看信息
[root@localhost init.d]# chkconfig --list mysqld
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]'.
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@localhost init.d]# service mysqld status
SUCCESS! MySQL running (3535)
#编辑 vi /etc/profile 配置环境变量
export PATH=$PATH:/usr/local/mysql/bin
[root@localhost mysql]# source /etc/profile
#获得初始密码
[root@localhost init.d]# cat /root/.mysql_secret
# Password set for user 'root@localhost' at 2018-07-21 16:00:04
m!xd:xJZlN>S
update mysql.user set authentication_string=password('你想输入的密码') where user='root' and Host ='localhost';
update mysql.user set authentication_string=password('***') where user='root' and Host ='localhost';
修改密码
[root@localhost init.d]# 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.21
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 mysql.user set authentication_string=password('***') where user='root' and Host ='localhost';
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql>
先停止mysql
[root@localhost init.d]# ./mysqld stop
Shutting down MySQL.. SUCCESS!
#使用安全模式登陆,跳过密码验证 在msyql的bin包下面
mysqld_safe --user=mysql --skip-grant-tables --skip-networking&
[1] 3666
[root@localhost bin]# 2018-07-21T08:54:22.046004Z mysqld_safe Logging to '/usr/local/mysql/data/localhost.localdomain.err'.
2018-07-21T08:54:22.089957Z mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data
^C
[root@localhost bin]# mysql -uroot mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.21 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 mysql.user set authentication_string=password('***') where user='root' and Host ='localhost';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> exit;
Bye
重新登陆后查看数据库
mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> set password=password('***');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.01 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> update user set host='%' where user='root';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select host,user from user;
+-----------+---------------+
| host | user |
+-----------+---------------+
| % | root |
| localhost | mysql.session |
| localhost | mysql.sys |
+-----------+---------------+
3 rows in set (0.01 sec)
#这里 @‘%’ 表示在任何主机都可以登录
GRANT ALL ON *.* TO root@'%' IDENTIFIED BY 'root' WITH GRANT OPTION;
mysql> GRANT ALL ON *.* TO root@'%' IDENTIFIED BY '***' WITH GRANT OPTION;
Query OK, 0 rows affected, 1 warning (0.00 sec)
#重启mysql
[root@localhost init.d]# ./mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
#为了在任何目录下可以登录mysql 做一个软连接
[root@localhost init.d]# ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql