---
title: "centos 安装mysql "
author: "java12345678"
date: "2019-08-08T10:38:33Z"
tags: []
categories: ["开源软件"]
slug: "centos-mysql"
draft: false
lastmod: "2019-08-08T12:02:02Z"
id: 2443359
url: "https://java12345678.iteye.com/blog/2443359"
---

1\. 下载mysql  tar.gz 

登录 [https://www.mysql.com/downloads/](https://www.mysql.com/downloads/) ，选 择相应的版本，下载

mysql-5.6.44-linux-glibc2.12-x86\_64.tar.gz

2\. 解压

  tar -xzvf mysql-5.6.44-linux-glibc2.12-x86\_64.tar.gz

  解压后，目录结构：

  Directory Contents of Directory `bin`, `scripts` [**mysqld**](https://dev.mysql.com/doc/refman/5.6/en/mysqld.html "4.3.1 mysqld — The MySQL Server") server, client and utility programs `data` Log files, databases `docs` MySQL manual in Info format `include` Include (header) files `lib` Libraries `mysql-test` Test suite `man` Unix manual pages `share` Error messages, dictionary, and SQL for database installation `sql-bench` Benchmarks `support-files` Miscellaneous support files, including sample configuration files

 

 

3\. 初始化数据库

   ./scripts/ mysql\_install\_db --help

 

  --basedir=path       The path to the MySQL installation directory.

  --builddir=path      If using --srcdir with out-of-directory builds, you

                       will need to set this to the location of the build

                       directory where built files reside.

  --cross-bootstrap    For internal use.  Used when building the MySQL system

                       tables on a different host than the target.

  --datadir=path       The path to the MySQL data directory.

                       If missing, the directory will be created, but its

                       parent directory must already exist and be writable.

  --defaults-extra-file=name

                       Read this file after the global files are read.

  --defaults-file=name Only read default options from the given file name.

  --force              Causes mysql\_install\_db to run even if DNS does not

                       work.  In that case, grant table entries that

                       normally use hostnames will use IP addresses.

  --help               Display this help and exit.                     

  --ldata=path         The path to the MySQL data directory. Same as --datadir.

  --no-defaults        Don't read default options from any option file.

  --keep-my-cnf        Don't try to create my.cnf based on template. 

                       Useful for systems with working, updated my.cnf.

                       Deprecated, will be removed in future version.

  --random-passwords   Create and set a random password for all root accounts

                       and set the "password expired" flag,

                       also remove the anonymous accounts.

  --rpm                For internal use.  This option is used by RPM files

                       during the MySQL installation process.

  --skip-name-resolve  Use IP addresses rather than hostnames when creating

                       grant table entries.  This option can be useful if

                       your DNS does not work.

  --srcdir=path        The path to the MySQL source directory.  This option

                       uses the compiled binaries and support files within the

                       source tree, useful for if you don't want to install

                       MySQL yet and just want to create the system tables.

  --user=user\_name     The login username to use for running mysqld.  Files

                       and directories created by mysqld will be owned by this

                       user.  You must be root to use this option.  By default

                       mysqld runs using your current login name and files and

                       directories that it creates will be owned by you.

 

  如果此命令报错：mysql\_install\_db: /usr/bin/perl: bad interpreter: No such file or directory

  则需要安装perl :yum install -y perl-Module-Install.noarch

 

安装完成后，可进行初始数据库：

mysql\_install\_db

--basedir /home/tester/mysql/mysql-5.6.44-linux-glibc2.12-x86\_64

--datadir /home/tester/mysql/data

 

```
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:

  /home/tester/mysql/mysql-5.6.44-linux-glibc2.12-x86_64/bin/mysqladmin -u root password 'new-password'
  /home/tester/mysql/mysql-5.6.44-linux-glibc2.12-x86_64/bin/mysqladmin -u root -h localhost.localdomain password 'new-password'

Alternatively you can run:

  /home/tester/mysql/mysql-5.6.44-linux-glibc2.12-x86_64/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 . ; /home/tester/mysql/mysql-5.6.44-linux-glibc2.12-x86_64/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

New default config file was created as /home/tester/mysql/mysql-5.6.44-linux-glibc2.12-x86_64/my.cnf and
will be used by default by the server when you start it.
You may edit this file to change server settings

WARNING: Default config file /etc/my.cnf exists on the system
This file will be read by default by the MySQL server
If you do not want to use this, either remove it, or use the
--defaults-file argument to mysqld_safe when starting the server
```

 

在basedir 下生成 my.cnf

修改my.conf

```
basedir=/home/tester/mysql/mysql-5.6.44-linux-glibc2.12-x86_64
datadir=/home/tester/mysql/data
log-error=/home/tester/mysql/log/error.log
pid-file=/home/tester/mysql/mysqld.pid
character_set_server=utf8
```

 

 

 

 

 

启动mysql :./mysqld\_safe --defaults-file=/home/tester/mysql/mysql-5.6.44-linux-glibc2.12-x86\_64/my.cnf&

 

创建用户： mysql -u root 连接mysql 服务

 命令：CREATE USER 'yanlei'@'%' IDENTIFIED BY '111111';

 命令： GRANT ALL ON \*.* TO 'yanlei'@'%';

 创建数据库：create database testdb character set utf8  collate  utf8\_general\_ci;

 

 

停止mysql服务：./mysqladmin -u root shutdown