linux系统安装mysql数据库

mysql数据库,作为一种开源的关系型数据库,以其易学易用等特点,深受同行喜爱,流行度很高,特别是使用过DB2数据库后,就会发现,mysql数据库是多么简单易用,本篇将介绍一下mysql数据库在Linux环境的安装。
一、安装前准备
(1)linux服务器:本次采用centos6.5系统的虚拟机进行安装;
(2)mysql安装包:本次选择mysql5.6版本进行安装,下载网址:https://dev.mysql.com/downloads/mysql/5.6.html#downloads
一直拉到最下面,选择版本后点击下载:
linux系统安装mysql数据库_第1张图片
会跳转到一个页面,点击“No thanks, just start my download.”开始下载
linux系统安装mysql数据库_第2张图片
(3)上传安装包到服务器
下载完成后,使用FileZilla等ftp工具,将下载的安装包mysql-5.6.42-linux-glibc2.12-x86_64.tar.gz上传到服务器(虚拟机指定目录下),本次上传目录是/install,如图:
在这里插入图片描述
二、开始安装
(1)创建mysql文件夹:

mkdir /usr/local/mysql
mkdir /usr/local/mysql/error-log
mkdir /usr/local/mysql/data
echo ''>/usr/local/mysql/error-log/error.log

(2)创建mysql用户组和用户:

groupadd mysql
useradd mysql -g mysql -d /home/mysql

(3)解压安装包

cd /install/
tar -zxvf mysql-5.6.42-linux-glibc2.12-x86_64.tar.gz

(4)移动解压文件到mysql目录:

cd mysql-5.6.42-linux-glibc2.12-x86_64
mv * /usr/local/mysql/

(5)修改mysql目录用户权限

cd /usr/local/mysql/
chown -R mysql:mysql ./

(6)执行安装脚本:

./scripts/mysql_install_db --user=mysql --datadir=/usr/local/mysql/data --basedir=/usr/local/mysql

具体输出内容如下:

[root@localhost scripts]# ./mysql_install_db --user=mysql --datadir=/usr/local/mysql/data --basedir=/usr/local/mysql
Installing MySQL system tables...2019-01-06 20:17:22 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-01-06 20:17:22 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
2019-01-06 20:17:22 0 [Note] /usr/local/mysql/bin/mysqld (mysqld 5.6.42) starting as process 10306 ...
2019-01-06 20:17:22 10306 [Note] InnoDB: Using atomics to ref count buffer pool pages
2019-01-06 20:17:22 10306 [Note] InnoDB: The InnoDB memory heap is disabled
2019-01-06 20:17:22 10306 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2019-01-06 20:17:22 10306 [Note] InnoDB: Memory barrier is not used
2019-01-06 20:17:22 10306 [Note] InnoDB: Compressed tables use zlib 1.2.11
2019-01-06 20:17:22 10306 [Note] InnoDB: Using Linux native AIO
2019-01-06 20:17:22 10306 [Note] InnoDB: Using CPU crc32 instructions
2019-01-06 20:17:22 10306 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2019-01-06 20:17:22 10306 [Note] InnoDB: Completed initialization of buffer pool
2019-01-06 20:17:22 10306 [Note] InnoDB: The first specified data file ./ibdata1 did not exist: a new database to be created!
2019-01-06 20:17:22 10306 [Note] InnoDB: Setting file ./ibdata1 size to 12 MB
2019-01-06 20:17:22 10306 [Note] InnoDB: Database physically writes the file full: wait...
2019-01-06 20:17:23 10306 [Note] InnoDB: Setting log file ./ib_logfile101 size to 48 MB
2019-01-06 20:17:23 10306 [Note] InnoDB: Setting log file ./ib_logfile1 size to 48 MB
2019-01-06 20:17:24 10306 [Note] InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0
2019-01-06 20:17:24 10306 [Warning] InnoDB: New log files created, LSN=45781
2019-01-06 20:17:24 10306 [Note] InnoDB: Doublewrite buffer not found: creating new
2019-01-06 20:17:24 10306 [Note] InnoDB: Doublewrite buffer created
2019-01-06 20:17:24 10306 [Note] InnoDB: 128 rollback segment(s) are active.
2019-01-06 20:17:24 10306 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-01-06 20:17:24 10306 [Note] InnoDB: Foreign key constraint system tables created
2019-01-06 20:17:24 10306 [Note] InnoDB: Creating tablespace and datafile system tables.
2019-01-06 20:17:24 10306 [Note] InnoDB: Tablespace and datafile system tables created.
2019-01-06 20:17:24 10306 [Note] InnoDB: Waiting for purge to start
2019-01-06 20:17:24 10306 [Note] InnoDB: 5.6.42 started; log sequence number 0
2019-01-06 20:17:25 10306 [Note] Binlog end
2019-01-06 20:17:25 10306 [Note] InnoDB: FTS optimize thread exiting.
2019-01-06 20:17:25 10306 [Note] InnoDB: Starting shutdown...
2019-01-06 20:17:26 10306 [Note] InnoDB: Shutdown completed; log sequence number 1625977
OK

Filling help tables...2019-01-06 20:17:26 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-01-06 20:17:26 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
2019-01-06 20:17:26 0 [Note] /usr/local/mysql/bin/mysqld (mysqld 5.6.42) starting as process 10328 ...
2019-01-06 20:17:26 10328 [Note] InnoDB: Using atomics to ref count buffer pool pages
2019-01-06 20:17:26 10328 [Note] InnoDB: The InnoDB memory heap is disabled
2019-01-06 20:17:26 10328 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2019-01-06 20:17:26 10328 [Note] InnoDB: Memory barrier is not used
2019-01-06 20:17:26 10328 [Note] InnoDB: Compressed tables use zlib 1.2.11
2019-01-06 20:17:26 10328 [Note] InnoDB: Using Linux native AIO
2019-01-06 20:17:26 10328 [Note] InnoDB: Using CPU crc32 instructions
2019-01-06 20:17:26 10328 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2019-01-06 20:17:26 10328 [Note] InnoDB: Completed initialization of buffer pool
2019-01-06 20:17:26 10328 [Note] InnoDB: Highest supported file format is Barracuda.
2019-01-06 20:17:26 10328 [Note] InnoDB: 128 rollback segment(s) are active.
2019-01-06 20:17:26 10328 [Note] InnoDB: Waiting for purge to start
2019-01-06 20:17:26 10328 [Note] InnoDB: 5.6.42 started; log sequence number 1625977
2019-01-06 20:17:27 10328 [Note] Binlog end
2019-01-06 20:17:27 10328 [Note] InnoDB: FTS optimize thread exiting.
2019-01-06 20:17:27 10328 [Note] InnoDB: Starting shutdown...
2019-01-06 20:17:28 10328 [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 localhost.localdomain 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

New default config file was created as /usr/local/mysql/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

[root@localhost scripts]# 

(7)拷贝support-files目录下一些文件到对应目录:

cp support-files/mysql.server /etc/init.d/mysqld
cp support-files/my-default.cnf /etc/my.cnf   (如果原系统有my.cnf,则选择覆盖)

(8)修改my.cnf配置文件:

vim /etc/my.cnf

设置如下配置信息:

basedir = /usr/local/mysql/
datadir = /usr/local/mysql/data
port = 3308
character_set_server=utf8
同时加入如下配置:
[client]
default-character-set=utf8
[mysqld_safe]
log-error=/usr/local/mysql/error-log/error.log

修改完之后的my.cnf配置如下图:
linux系统安装mysql数据库_第3张图片
(9)启动mysql

service mysqld start
service mysqld status

回显信息如下:
在这里插入图片描述
如上图,说明mysql已经安装成功了
(10)将mysql命令加入环境变量,使得任何地方都可以调用mysql命令。
编辑/etc/profile:

vim /etc/profile
在最后面加入如下命令:
export PATH=$PATH:/usr/local/mysql/bin
重新加载/etc/profile:
source /etc/profile

(11)设置mysqld开机启动
检查开机启动项里边有没有mysqld

chkconfig --list|grep -i mysqld

如果没有,则将mysqld加入开机启动项:

chkconfig --add mysqld
chkconfig mysqld on

(12)登录数据库:
首次登录数据库,需要设置root用户的密码:

/usr/local/mysql/bin/mysqladmin -u root password '123456'

设置成功后,使用123456这个密码j就可以登录数据库:

mysql -uroot -p123456

回显信息如下,说明登录成功:
linux系统安装mysql数据库_第4张图片
(13)设置mysql远程登录(使用navicat等客户端可以连接)
首先设置防火墙,允许3308端口访问

vim /etc/sysconfig/iptables

加入如下规则:

-A INPUT -m state --state NEW -m tcp -p tcp --dport 3308 -j ACCEPT

linux系统安装mysql数据库_第5张图片
保存后重启防火墙:

service iptables restart

使用如下命令查看防火墙策略,验证是否添加成功:

iptables -L -n

linux系统安装mysql数据库_第6张图片
登录mysql,设置远程访问权限:

  > set password=password('你的密码');
  >grant all privileges on *.* to 'root'@'%' identified by 'root用户密码';
  >flush privileges;
  >exit;

这时候,mysql数据库安装就完成了,可以本地使用mysql -uroot -proot -u123456命令登录,也可以使用navicate工具进行连接。
注:
对于mysql5.7.25及其他版本,执行第(6)步的时候,没有./scripts/mysql_install_db文件,这时候可以使用bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data命令初始化配置,执行命令后输出如下:

[root@localhost mysql]# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
2019-05-31T00:34:11.100864Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-05-31T00:34:12.976550Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-05-31T00:34:13.348870Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-05-31T00:34:13.443499Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: d06a1b8e-833b-11e9-9185-000c29f06939.
2019-05-31T00:34:13.445482Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2019-05-31T00:34:13.453644Z 1 [Note] A temporary password is generated for root@localhost: d7(Zgh+mwfVb
[root@localhost mysql]# 

可以看到生成了一个临时密码d7(Zgh+mwfVb
然后执行命令bin/mysql_ssl_rsa_setup --datadir=/usr/local/mysql/data进行安装,执行完后回显信息如下:

[root@localhost mysql]# bin/mysql_ssl_rsa_setup  --datadir=/usr/local/mysql/data
Generating a 2048 bit RSA private key
..............+++
..........................................................+++
writing new private key to 'ca-key.pem'
-----
Generating a 2048 bit RSA private key
........................+++
..........................................+++
writing new private key to 'server-key.pem'
-----
Generating a 2048 bit RSA private key
......................................................................................................................................................................+++
........................................................+++
writing new private key to 'client-key.pem'
-----
[root@localhost mysql]# 

然后执行修改配置:

 cd /usr/local/mysql/support-files/
 cp mysql.server /etc/init.d/mysqld
 vim /etc/my.cnf

my.cnf内容如下:

[mysqld]
basedir=/usr/local/mysql/
datadir=/usr/local/mysql/data
port=3308
character_set_server=utf8
socket=/usr/local/mysql/mysql.sock
#user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
#symbolic-links=0
#[client]
#default-character-set=utf8
[mysqld_safe]
log-error=/usr/local/mysql/error-log/error.log
pid-file=/usr/local/mysql/mysqld.pid

然后使用service mysqld start启动mysql服务,使用mysql -uroot -h 127.0.0.1 -P3308 -p命令登录mysql(如果提示“-bash: mysql: command not found”,可以使用ln -s /usr/local/mysql/bin/mysql /usr/bin命令添加软连接),输入前面生成的默认密码,登录后修改密码:

set password=password('你的密码');

修改root用户账户host地址:

grant all privileges on *.* to 'root'@'%' identified by 'root用户密码';
flush privileges;

其余设置与mysql5.6安装一样

你可能感兴趣的:(mysql)