安装并配置单实例mysql数据库

怎甘沉沦 提交于 2020-04-18 17:50:23

一、下载介质
http://mirrors.sohu.com/mysql/
http://mirrors.163.com/mysql/Downloads/MySQL-5.6/mysql-5.6.40.tar.gz
http://dev.mysql.com/downloads/mysql/
http://ftp.ntu.edu.tw/MySQL/Downloads/



二、安装mysql需要的依赖包和编译软件

[root@monitor01 ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
[root@monitor01 ~]# uname -r
3.10.0-957.el7.x86_64
[root@monitor01 ~]# uname -m
x86_64




安装依赖包
[root@monitor01 yum.repos.d]# yum install ncurses-devel libaio-devel cmake gcc gcc-c++ glibc openssl-devel bison-devel autoconf -y
[root@monitor01 yum.repos.d]# rpm -qa curses-devel libaio-devel

安装编译mysql需要的软件
[root@monitor01 ~]# yum install cmake -y
[root@monitor01 ~]# rpm -qa cmake

三、安装mysql

1、建立mysql用户账号
[root@monitor01 ~]# useradd -s /sbin/nologin -M mysql
[root@monitor01 ~]# id mysql
uid=1004(mysql) gid=1007(mysql) 组=1007(mysql)


2、获取mysql软件包
http://mirrors.sohu.com/mysql/

mysql-5.6.40.tar.gz
mysql-5.6.40-linux-glibc2.12-x86_64.tar.gz

3、采用编译方式安装mysql
第一步,下载mysql软件包,命令及操作如下:
[root@monitor01 ~]# mkdir -p /home/oldboy/tools
[root@monitor01 ~]# mkdir /application
[root@monitor01 ~]# cd /home/oldboy/tools
[root@monitor01 ~]# wget -q http://mirrors.163.com/mysql/Downloads/MySQL-5.6/mysql-5.6.40.tar.gz




[root@monitor01 tools]# ll -h
总用量 344M
-rw-r--r--. 1 root root 314M 4月 13 14:52 mysql-5.6.40-linux-glibc2.12-x86_64.tar.gz
-rw-r--r--. 1 root root 31M 4月 13 14:41 mysql-5.6.40.tar.gz


第二步,解压并配置mysql,命令及操作如下:
[root@monitor01 tools]# tar xf mysql-5.6.40.tar.gz
[root@monitor01 tools]# cd mysql-5.6.40/
[root@monitor01 mysql-5.6.40]# cmake . -DCMAKE_INSTALL_PREFIX=/application/mysql-5.6.40 \
-DMYSQL_DATADIR=/application/mysql-5.6.40/data \
-DMYSQL_UNIX_ADDR=/application/mysql-5.6.40/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \
-DWITH_ZLIB=bundled \
-DWITH_SSL=bundled \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_EMBEDDED_SERVER=1 \
-DENABLE_DOWNLOADS=1 \
-DWITH_DEBUG=0

















参数说明:
cmake . -DCMAKE_INSTALL_PREFIX=/application/mysql-5.6.40 \ #指定安装目录
-DMYSQL_DATADIR=/application/mysql-5.6.40/data \ #指定数据data目录
-DMYSQL_UNIX_ADDR=/application/mysql-5.6.40/mysql.sock \ #指定sock路径
-DDEFAULT_CHARSET=utf8 \ #指定默认字符集
-DDEFAULT_COLLATION=utf8_general_ci \ #指定默认校验规则
-DWITH_EXTRA_CHARSETS=all \ #指定扩展支持的字符集
-DWITH_INNOBASE_STORAGE_ENGINE=1 \ #启用innodb存储引擎
-DWITH_FEDERATED_STORAGE_ENGINE=1 \ #启用Federated存储引擎
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \ #启用BLACKHOLE引擎
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \ #禁用EXAMPLE_STORAGE_ENGINE
-DWITH_ZLIB=bundled \ #启用zlib支持
-DWITH_SSL=bundled \ #启用ssl库支持
-DENABLED_LOCAL_INFILE=1 \ #启用加载本地数据
-DWITH_EMBEDDED_SERVER=1 \ #嵌入式服务器
-DENABLE_DOWNLOADS=1 \ #编译时允许自主下载相关文件
-DWITH_DEBUG=0 #禁用调试模式
(参考:https://www.cnblogs.com/ssgeek/p/10489078.html)
















第三步,编译并安装mysql,命令及操作如下:
[root@monitor01 mysql-5.6.40]# make && make install

第四步,为mysql安装路径设置不带版本号的软链接/application/mysql,命令及操作如下:
[root@monitor01 mysql-5.6.40]# ln -s /application/mysql-5.6.40/ /application/mysql

四、创建mysql数据库配置文件并对数据库目录授权

[root@monitor01 mysql-5.6.40]# ll support-files/*.cnf
-rw-r--r--. 1 root root 1126 4月 13 15:14 support-files/my-default.cnf
[root@monitor01 mysql-5.6.40]# mv /etc/my.cnf /etc/my.cnf.bak
[root@monitor01 mysql-5.6.40]# cp support-files/my-default.cnf /etc/my.cnf
[root@monitor01 mysql-5.6.40]# chown -R mysql.mysql /application/mysql



五、初始化mysql数据库文件

1、初始化mysql数据库
[root@monitor01 mysql-5.6.40]# /application/mysql/scripts/mysql_install_db --basedir=/application/mysql/ --datadir=/application/mysql/data --user=mysql

六、配置并启动mysql数据库
1、设置mysql启动脚本
[root@monitor01 mysql-5.6.40]# pwd
/home/oldboy/tools/mysql-5.6.40
[root@monitor01 mysql-5.6.40]# cp support-files/mysql.server /etc/init.d/mysqld
[root@monitor01 mysql-5.6.40]# chmod 700 /etc/init.d/mysqld
[root@monitor01 mysql-5.6.40]# ll /etc/init.d/mysqld
-rwx------. 1 root root 10619 4月 13 15:59 /etc/init.d/mysqld






2、启动mysql数据库
[root@monitor01 mysql-5.6.40]# /application/mysql/bin/mysqld_safe --user=mysql &
或者
[root@monitor01 mysql-5.6.40]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/application/mysql-5.6.40/data/monitor01.err'.
200413 16:09:56 mysqld_safe Directory '/application/mysql-5.6.40/tmp' for UNIX socket file don't exists.
ERROR! The server quit without updating PID file (/application/mysql-5.6.40/data/monitor01.pid).
[root@monitor01 mysql-5.6.40]#
[root@monitor01 mysql-5.6.40]# mkdir -p /application/mysql-5.6.40/tmp
[root@monitor01 mysql-5.6.40]# chown -R mysql.mysql /application/mysql/
[root@monitor01 mysql-5.6.40]# /etc/init.d/mysqld start









3、检查mysql数据库是否启动
[root@monitor01 mysql-5.6.40]# netstat -lntup| grep mysql
tcp6 0 0 :::3306 :::* LISTEN 21750/mysqld

4、查看mysql数据库启动结果日志:
[root@monitor01 mysql-5.6.40]# tail -n 1000 -f /application/mysql-5.6.40/data/monitor01.err

5、设置mysql开机自动启动:
[root@monitor01 mysql-5.6.40]# chkconfig --add mysqld
[root@monitor01 mysql-5.6.40]# chkconfig --list mysqld

七、将mysql相关命令加入全局路径
1、确认mysql命令所在路径:
[root@monitor01 mysql-5.6.40]# ll /application/mysql/bin/mysql
-rwxr-xr-x. 1 mysql mysql 10250744 4月 13 15:27 /application/mysql/bin/mysql


2、在path变量前增加/application/mysql/bin路径,并追加到/etc/profile文件中
[root@monitor01 mysql-5.6.40]# echo 'export PATH=/application/mysql/bin/:$PATH' >>/etc/profile
[root@monitor01 mysql-5.6.40]# tail -l /etc/profile
[root@monitor01 mysql-5.6.40]# source /etc/profile
[root@monitor01 mysql-5.6.40]# echo $PATH
/application/mysql/bin/:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin




八、登录mysql测试
mysql -uroot -p
或者
mysql -uroot
或者
mysql -uroot -p '密码'
或者
[root@monitor01 mysql-5.6.40]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.40 Source distribution









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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)








mysql> select user();
+----------------+
| user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)





mysql>

九、基本mysql安全配置
1、为root用户设置密码
[root@monitor01 mysql-5.6.40]# mysqladmin -u root password 'oldboy123'
[root@monitor01 mysql-5.6.40]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@monitor01 mysql-5.6.40]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.40 Source distribution








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>

2、清理mysql服务器内无用的用户
mysql> select user,host from mysql.user;
+------+-----------+
| user | host |
+------+-----------+
| root | 127.0.0.1 |
| root | ::1 |
| | localhost |
| root | localhost |
| | monitor01 |
| root | monitor01 |
+------+-----------+
6 rows in set (0.00 sec)











mysql> drop user root@'::1';
Query OK, 0 rows affected (0.00 sec)

mysql> drop user root@'monitor01';
Query OK, 0 rows affected (0.00 sec)

mysql> drop user ''@'monitor01';
Query OK, 0 rows affected (0.00 sec)

mysql> drop user ''@'localhost';
Query OK, 0 rows affected (0.00 sec)

mysql> select user,host from mysql.user;
+------+-----------+
| user | host |
+------+-----------+
| root | 127.0.0.1 |
| root | localhost |
+------+-----------+
2 rows in set (0.00 sec)






mysql>
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
mysql> drop database test;
Query OK, 0 rows affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.01 sec)




















mysql>

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!