MySQL安装操作指引

 

以下所有操作为centos6.7环境

一、下载MySQL数据库安装包

MySQL官方网站:https://dev.mysql.com/downloads/mysql/

下载安装包:mysql-5.6.41-linux-glibc2.12-x86_64.tar.gz

1.png


二、创建mysql用户组及mysql用户帐号

[root@CentOS-A-server ~]# groupadd mysql            #创建mysql用户组
[root@CentOS-A-server ~]# useradd mysql -s /sbin/nologin -g mysql -M    #建mysql用户不可登陆不建家目录
[root@CentOS-A-server ~]# 
[root@CentOS-A-server ~]# tail -1 /etc/group                   #查看是否创建成功
mysql:x:502:
[root@CentOS-A-server ~]#

 

三、mysql安装过程

[root@CentOS-A-server ~]# cd /usr/local/      #进入/usr/local/目录
[root@CentOS-A-server local]# ll
-rw-r--r--. 1 root root 328530942 7月  31 04:28 mysql-5.6.41-linux-glibc2.12-x86_64.tar.gz


#解压mysql安装包

[root@CentOS-A-server local]# tar zxf mysql-5.6.41-linux-glibc2.12-x86_64.tar.gz    #解压mysql安装包
[root@CentOS-A-server local]# ll
drwxr-xr-x. 13 root root      4096 7月  31 04:29 mysql-5.6.41-linux-glibc2.12-x86_64
-rw-r--r--.  1 root root 328530942 7月  31 04:28 mysql-5.6.41-linux-glibc2.12-x86_64.tar.gz


#更改解压后的目录名为mysql

[root@CentOS-A-server local]# mv mysql-5.6.41-linux-glibc2.12-x86_64 mysql   #更改解压后的目录名为mysql
[root@CentOS-A-server local]# ll
drwxr-xr-x. 13 root root      4096 7月  31 04:29 mysql
-rw-r--r--.  1 root root 328530942 7月  31 04:28 mysql-5.6.41-linux-glibc2.12-x86_64.tar.gz


#授权/usr/local/属组给mysql帐号

[root@CentOS-A-server mysql]# chown -R mysql:mysql /usr/local/   #授权/usr/local/属组给mysql帐号
[root@CentOS-A-server mysql]# 
[root@CentOS-A-server mysql]# cd /usr/local/mysql    #进入/usr/local/mysql目录


#创建my.cnf文件及编辑,重点

[root@CentOS-A-server mysql]# vim my.cnf             #创建my.cnf文件及编辑
[client]
default-character-set=utf8
socket=/tmp/mysql.sock
[mysql]
default-character-set=utf8
[mysqld]
socket=/tmp/mysql.sock
tmpdir=/usr/local/mysql/tmp/
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
symbolic-links=0
character_set_server=utf8
[mysqld_safe]
default-character-set=utf8
log-error=/usr/local/mysql/logs/mysqld.log
pid-file=/usr/local/mysql/run/mysqld/mysqld.pid
[root@CentOS-A-server mysql]#


[root@CentOS-A-server mysql]# cat my.cnf       #查看my.cnf编辑后的内容,重点
[client]
default-character-set=utf8
socket=/tmp/mysql.sock
[mysql]
default-character-set=utf8
[mysqld]
socket=/tmp/mysql.sock
tmpdir=/usr/local/mysql/tmp/
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
symbolic-links=0
character_set_server=utf8
[mysqld_safe]
default-character-set=utf8
log-error=/usr/local/mysql/logs/mysqld.log
pid-file=/usr/local/mysql/run/mysqld/mysqld.pid
[root@CentOS-A-server mysql]#


#mysql目录下创建相关目录

[root@CentOS-A-server mysql]# cd /usr/local/mysql    #进入/usr/local/mysql目录
[root@CentOS-A-server mysql]# mkdir tmp              #在mysql目录下创建tmp目录
[root@CentOS-A-server mysql]# mkdir logs             #在mysql目录下创建logs目录
[root@CentOS-A-server mysql]# mkdir -p run/mysqld    #在mysql目录下创建run/mysqld目录
[root@CentOS-A-server mysql]# chown -R mysql:mysql /usr/local/    #授权/usr/local/属组给mysql帐号


#安装mysql数据库

[root@CentOS-A-server mysql]# scripts/mysql_install_db --user=mysql --defaults-file=/usr/local/mysql/my.cnf --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data/  #安装mysql数据库
WARNING: The host 'CentOS-A-server' could not be looked up with /usr/local/mysql/bin/resolveip.
This probably means that your libc libraries are not 100 % compatible
with this binary MySQL version. The MySQL daemon, mysqld, should work
normally with the exception that host name resolving will not work.
This means that you should use IP addresses instead of hostnames
when specifying MySQL privileges !
 
Installing MySQL system tables...2018-07-31 04:41:20 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-07-31 04:41:20 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
2018-07-31 04:41:20 0 [Note] /usr/local/mysql/bin/mysqld (mysqld 5.6.41) starting as process 6836 ...
2018-07-31 04:41:20 6836 [Note] InnoDB: Using atomics to ref count buffer pool pages
2018-07-31 04:41:20 6836 [Note] InnoDB: The InnoDB memory heap is disabled
2018-07-31 04:41:20 6836 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2018-07-31 04:41:20 6836 [Note] InnoDB: Memory barrier is not used
2018-07-31 04:41:20 6836 [Note] InnoDB: Compressed tables use zlib 1.2.3
2018-07-31 04:41:20 6836 [Note] InnoDB: Using Linux native AIO
2018-07-31 04:41:20 6836 [Note] InnoDB: Using CPU crc32 instructions
2018-07-31 04:41:20 6836 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2018-07-31 04:41:20 6836 [Note] InnoDB: Completed initialization of buffer pool
2018-07-31 04:41:20 6836 [Note] InnoDB: The first specified data file ./ibdata1 did not exist: a new database to be created!
2018-07-31 04:41:20 6836 [Note] InnoDB: Setting file ./ibdata1 size to 12 MB
2018-07-31 04:41:20 6836 [Note] InnoDB: Database physically writes the file full: wait...
2018-07-31 04:41:21 6836 [Note] InnoDB: Setting log file ./ib_logfile101 size to 48 MB
2018-07-31 04:41:22 6836 [Note] InnoDB: Setting log file ./ib_logfile1 size to 48 MB
2018-07-31 04:41:22 6836 [Note] InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0
2018-07-31 04:41:22 6836 [Warning] InnoDB: New log files created, LSN=45781
2018-07-31 04:41:22 6836 [Note] InnoDB: Doublewrite buffer not found: creating new
2018-07-31 04:41:22 6836 [Note] InnoDB: Doublewrite buffer created
2018-07-31 04:41:22 6836 [Note] InnoDB: 128 rollback segment(s) are active.
2018-07-31 04:41:22 6836 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-07-31 04:41:23 6836 [Note] InnoDB: Foreign key constraint system tables created
2018-07-31 04:41:23 6836 [Note] InnoDB: Creating tablespace and datafile system tables.
2018-07-31 04:41:23 6836 [Note] InnoDB: Tablespace and datafile system tables created.
2018-07-31 04:41:23 6836 [Note] InnoDB: Waiting for purge to start
2018-07-31 04:41:23 6836 [Note] InnoDB: 5.6.41 started; log sequence number 0
2018-07-31 04:41:24 6836 [Note] Binlog end
2018-07-31 04:41:24 6836 [Note] InnoDB: FTS optimize thread exiting.
2018-07-31 04:41:24 6836 [Note] InnoDB: Starting shutdown...
2018-07-31 04:41:26 6836 [Note] InnoDB: Shutdown completed; log sequence number 1625977
OK
 
Filling help tables...2018-07-31 04:41:26 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-07-31 04:41:26 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
2018-07-31 04:41:26 0 [Note] /usr/local/mysql/bin/mysqld (mysqld 5.6.41) starting as process 6858 ...
2018-07-31 04:41:26 6858 [Note] InnoDB: Using atomics to ref count buffer pool pages
2018-07-31 04:41:26 6858 [Note] InnoDB: The InnoDB memory heap is disabled
2018-07-31 04:41:26 6858 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2018-07-31 04:41:26 6858 [Note] InnoDB: Memory barrier is not used
2018-07-31 04:41:26 6858 [Note] InnoDB: Compressed tables use zlib 1.2.3
2018-07-31 04:41:26 6858 [Note] InnoDB: Using Linux native AIO
2018-07-31 04:41:26 6858 [Note] InnoDB: Using CPU crc32 instructions
2018-07-31 04:41:26 6858 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2018-07-31 04:41:26 6858 [Note] InnoDB: Completed initialization of buffer pool
2018-07-31 04:41:26 6858 [Note] InnoDB: Highest supported file format is Barracuda.
2018-07-31 04:41:26 6858 [Note] InnoDB: 128 rollback segment(s) are active.
2018-07-31 04:41:26 6858 [Note] InnoDB: Waiting for purge to start
2018-07-31 04:41:26 6858 [Note] InnoDB: 5.6.41 started; log sequence number 1625977
2018-07-31 04:41:27 6858 [Note] Binlog end
2018-07-31 04:41:27 6858 [Note] InnoDB: FTS optimize thread exiting.
2018-07-31 04:41:27 6858 [Note] InnoDB: Starting shutdown...
2018-07-31 04:41:28 6858 [Note] InnoDB: Shutdown completed; log sequence number 1625987
OK
 
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
 
PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
 
  /usr/local/mysql/bin/mysqladmin -u root password 'new-password'
  /usr/local/mysql/bin/mysqladmin -u root -h CentOS-A-server password 'new-password'
 
Alternatively you can run:
 
  /usr/local/mysql/bin/mysql_secure_installation
 
which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.
 
See the manual for more instructions.
 
You can start the MySQL daemon with:
 
  cd . ; /usr/local/mysql/bin/mysqld_safe &
 
You can test the MySQL daemon with mysql-test-run.pl
 
  cd mysql-test ; perl mysql-test-run.pl
 
Please report any problems at http://bugs.mysql.com/
 
The latest information about MySQL is available on the web at
 
  http://www.mysql.com
 
Support MySQL by buying support/licenses at http://shop.mysql.com
 
WARNING: Found existing config file /usr/local/mysql/my.cnf on the system.
Because this file might be in use, it was not replaced,
but was used in bootstrap (unless you used --defaults-file)
and when you later start the server.
The new default config file was created as /usr/local/mysql/my-new.cnf,
please compare it with your file and take the changes you need.


#查找mysql服务

[root@CentOS-A-server mysql]# find / -name mysql.server       #查找mysql服务
/usr/local/mysql/support-files/mysql.server
[root@CentOS-A-server mysql]#


#启动mysql数据库

[root@CentOS-A-server mysql]# /usr/local/mysql/support-files/mysql.server start    #启动mysql数据库
Starting MySQL.180731 04:43:03 mysqld_safe error: log-error set to '/usr/local/mysql/logs/mysqld.log', however file don't exists. Create writable for user 'mysql'.
The server quit without updating PID file (/usr/local/mysql[失败]CentOS-A-server.pid).    #报错没有以上文件
[root@CentOS-A-server mysql]#


#创建以上报错没有的文件

[root@CentOS-A-server mysql]# touch /usr/local/mysql/logs/mysqld.log   #创建以上报错没有的文件
[root@CentOS-A-server mysql]# ll /usr/local/mysql/logs/mysqld.log                   #查看是否已创建成功
-rw-r--r--. 1 root root 0 7月  31 04:51 /usr/local/mysql/logs/mysqld.log
[root@CentOS-A-server mysql]#


#授权属组给mysql帐号

[root@CentOS-A-server mysql]# chown -R mysql:mysql /usr/local/mysql/logs/mysqld.log   #授权属组给mysql帐号
[root@CentOS-A-server mysql]# 
[root@CentOS-A-server mysql]# ll /usr/local/mysql/logs/mysqld.log                   #查看授权属组是否成功
-rw-r--r--. 1 mysql mysql 0 7月  31 04:51 /usr/local/mysql/logs/mysqld.log
[root@CentOS-A-server mysql]#


#启动mysql数据库

[root@CentOS-A-server mysql]# find / -name mysql.server       #查找mysql服务
/usr/local/mysql/support-files/mysql.server
[root@CentOS-A-server mysql]# 
[root@CentOS-A-server mysql]# /usr/local/mysql/support-files/mysql.server start    #启动mysql数据库
Starting MySQL.............................................[确定]....................................     #启动成功
[root@CentOS-A-server mysql]#


#查找确认mysql进程是否启动成功

[root@CentOS-A-server mysql]# ps -ef | grep mysql    #查找确认mysql进程是否启动成功
root       7260      1  0 04:52 pts/0    00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-file=/usr/local/mysql/data/CentOS-A-server.pid
mysql      7458   7260 77 04:52 pts/0    00:01:52 /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=/usr/local/mysql/logs/mysqld.log --pid-file=/usr/local/mysql/data/CentOS-A-server.pid --socket=/tmp/mysql.sock
root       7663   6692  0 04:55 pts/0    00:00:00 grep mysql
[root@CentOS-A-server mysql]#
[root@CentOS-A-server mysql]# cd /usr/local/mysql/bin/    #进入/usr/local/mysql/bin/目录
[root@CentOS-A-server bin]#


登陆MYSQL格式

---------------------------------------------------------------------------------------------------------------------------

连接MYSQL 格式: mysql -h主机地址 -u用户名 p用户密码 1、例1:连接到本机上的MYSQL 找到mysql的安装目录,一般可以直接键入命令./mysql -h127.0.0.1 -uroot -p,回车后提示你输密码,如果刚安装好MYSQL,超级用户root是没有密码的

---------------------------------------------------------------------------------------------------------------------------


#登陆mysql数据库,新建帐号没设置密码,可直接回车键登陆

[root@CentOS-A-server bin]# ./mysql -h127.0.0.1 -uroot -p       #登陆mysql数据库
Enter password:                                                 #直接回车键进入
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.41 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> use mysql;       #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用户

mysql> select host,user from mysql.user;        #查看mysql用户
+-----------------+------+
| host            | user |
+-----------------+------+
| 127.0.0.1       | root |
| ::1             | root |
| centos-a-server |      |
| centos-a-server | root |
| localhost       |      |
| localhost       | root |
+-----------------+------+
6 rows in set (0.06 sec

2.png


 #退出mysql数据库

mysql> quit         #退出mysql数据库
Bye
[root@CentOS-A-server bin]#


#找不到mysql命名

[root@CentOS-A-server ~]#mysql -h127.0.0.1 -uroot –p   #若不是在/usr/local/mysql/bin/执行会报错
-bash: mysql: command not found                                               #
找不到mysql命名

原因:这是由于系统默认会查找/usr/bin下的命令,如果这个命令不在这个目录下,当然会找不到命令,我们需要做的就是映射一个链接到/usr/bin目录下,相当于建立一个链接文件。
首先得知道mysql命令或mysqladmin命令的完整路径,比如mysql的路径是:/usr/local/mysql/bin/mysql,我们则可以这样执行命令:

# ln -s /usr/local/mysql/bin/mysql /usr/bin



#做软链接

[root@CentOS-A-server bin]# ln -s /usr/local/mysql/bin/mysql /usr/bin     #做软链接
[root@CentOS-A-server bin]# 
[root@CentOS-A-server bin]# mysql -h127.0.0.1 -uroot –p         #登陆mysql数据库
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.41 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> 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 mysql.user;
+-----------------+------+
| host            | user |
+-----------------+------+
| 127.0.0.1       | root |
| ::1             | root |
| centos-a-server |      |
| centos-a-server | root |
| localhost       |      |
| localhost       | root |
+-----------------+------+
6 rows in set (0.00 sec)
 
mysql> 
mysql> quit
Bye
[root@CentOS-A-server bin]#


 

 

四、创建mysql密码

[root@CentOS-A-server extra]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.41 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> set password =password('ouxuetong');    #创建mysql密码
Query OK, 0 rows affected (0.00 sec)
 
mysql> flush privileges;    #使其生效
Query OK, 0 rows affected (0.00 sec)
 
mysql>

 

 

五、查看基本数据库列表信息

创建用户:

mysql> create user 'usrabc'@'%' identified by 'usrabc';    #创建用户:
Query OK, 0 rows affected (0.00 sec)
mysql> select host,user from mysql.user;
+------------+--------+
| host       | user   |
+------------+--------+
| %          | usrabc |
| 127.0.0.1  | root   |
| ::1        | root   |
| localhost  |        |
| localhost  | root   |
| server.gao |        |
| server.gao | root   |
+------------+--------+
7 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>
mysql> quit
Bye
[root@CentOS-A-server bin]#

 

 

 安装整个过程已完成!