本文档主要介绍了如何在CentOS6.8上安装MySQL。详细步骤以下:mysql
#yum list installed | grep mysqlweb
[root@iZwz9hy7gff0kp0rba9p6vZ ~]# yum list installed | grep mysql mysql-libs.x86_64 5.1.73-8.el6_8 @base
#yum -y remove mysql-libs.x86_64sql
[root@iZwz9hy7gff0kp0rba9p6vZ ~]# yum -y remove mysql-libs.x86_64 Loaded plugins: fastestmirror Setting up Remove Process Resolving Dependencies --> Running transaction check ---> Package mysql-libs.x86_64 0:5.1.73-8.el6_8 will be erased --> Processing Dependency: libmysqlclient.so.16()(64bit) for package: 2:postfix-2.6.6-8.el6.x86_64 --> Processing Dependency: libmysqlclient.so.16(libmysqlclient_16)(64bit) for package: 2:postfix-2.6.6-8.el6.x86_64 --> Processing Dependency: mysql-libs for package: 2:postfix-2.6.6-8.el6.x86_64 --> Running transaction check ---> Package postfix.x86_64 2:2.6.6-8.el6 will be erased --> Processing Dependency: /usr/sbin/sendmail for package: redhat-lsb-core-4.0-7.el6.centos.x86_64 --> Processing Dependency: /usr/sbin/sendmail for package: cronie-1.4.4-16.el6_8.2.x86_64 --> Running transaction check ---> Package cronie.x86_64 0:1.4.4-16.el6_8.2 will be erased --> Processing Dependency: cronie = 1.4.4-16.el6_8.2 for package: cronie-anacron-1.4.4-16.el6_8.2.x86_64 ---> Package redhat-lsb-core.x86_64 0:4.0-7.el6.centos will be erased --> Running transaction check ---> Package cronie-anacron.x86_64 0:1.4.4-16.el6_8.2 will be erased --> Processing Dependency: /etc/cron.d for package: sysstat-9.0.4-33.el6.x86_64 --> Processing Dependency: /etc/cron.d for package: crontabs-1.10-33.el6.noarch --> Restarting Dependency Resolution with new changes. --> Running transaction check ---> Package crontabs.noarch 0:1.10-33.el6 will be erased ---> Package sysstat.x86_64 0:9.0.4-33.el6 will be erased --> Finished Dependency Resolution Dependencies Resolved ================================================================================================================================================================================================================= Package Arch Version Repository Size ================================================================================================================================================================================================================= Removing: mysql-libs x86_64 5.1.73-8.el6_8 @base 4.0 M Removing for dependencies: cronie x86_64 1.4.4-16.el6_8.2 @base 174 k cronie-anacron x86_64 1.4.4-16.el6_8.2 @base 43 k crontabs noarch 1.10-33.el6 @anaconda-CentOS-201605220104.x86_64/6.8 2.4 k postfix x86_64 2:2.6.6-8.el6 @base 9.7 M redhat-lsb-core x86_64 4.0-7.el6.centos @base 22 k sysstat x86_64 9.0.4-33.el6 @base 826 k Transaction Summary ================================================================================================================================================================================================================= Remove 7 Package(s) Installed size: 15 M Downloading Packages: Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Erasing : redhat-lsb-core-4.0-7.el6.centos.x86_64 1/7 /var/tmp/rpm-tmp.JQYEIW: line 1: lsb_release: command not found Erasing : sysstat-9.0.4-33.el6.x86_64 2/7 Erasing : cronie-1.4.4-16.el6_8.2.x86_64 3/7 Erasing : cronie-anacron-1.4.4-16.el6_8.2.x86_64 4/7 Erasing : crontabs-1.10-33.el6.noarch 5/7 Erasing : 2:postfix-2.6.6-8.el6.x86_64 6/7 Erasing : mysql-libs-5.1.73-8.el6_8.x86_64 7/7 Verifying : cronie-anacron-1.4.4-16.el6_8.2.x86_64 1/7 Verifying : cronie-1.4.4-16.el6_8.2.x86_64 2/7 Verifying : 2:postfix-2.6.6-8.el6.x86_64 3/7 Verifying : redhat-lsb-core-4.0-7.el6.centos.x86_64 4/7 Verifying : crontabs-1.10-33.el6.noarch 5/7 Verifying : sysstat-9.0.4-33.el6.x86_64 6/7 Verifying : mysql-libs-5.1.73-8.el6_8.x86_64 7/7 Removed: mysql-libs.x86_64 0:5.1.73-8.el6_8 Dependency Removed: cronie.x86_64 0:1.4.4-16.el6_8.2 cronie-anacron.x86_64 0:1.4.4-16.el6_8.2 crontabs.noarch 0:1.10-33.el6 postfix.x86_64 2:2.6.6-8.el6 redhat-lsb-core.x86_64 0:4.0-7.el6.centos sysstat.x86_64 0:9.0.4-33.el6 Complete!
# wget http://repo.mysql.com/mysql-community-release-el6-5.noarch.rpm数据库
[root@iZwz9hy7gff0kp0rba9p6vZ ~]# wget http://repo.mysql.com/mysql-community-release-el6-5.noarch.rpm --2017-11-04 14:57:59-- http://repo.mysql.com/mysql-community-release-el6-5.noarch.rpm Resolving repo.mysql.com... 23.211.97.88 Connecting to repo.mysql.com|23.211.97.88|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 5824 (5.7K) [application/x-redhat-package-manager] Saving to: “mysql-community-release-el6-5.noarch.rpm” 100%[=======================================================================================================================================================================>] 5,824 --.-K/s in 0s 2017-11-04 14:57:59 (412 MB/s) - “mysql-community-release-el6-5.noarch.rpm” saved [5824/5824]
# rpm -ivh mysql-community-release-el6-5.noarch.rpmbootstrap
[root@iZwz9hy7gff0kp0rba9p6vZ ~]# rpm -ivh mysql-community-release-el6-5.noarch.rpm Preparing... ########################################### [100%] 1:mysql-community-release########################################### [100%]
# yum repolist all | grep mysqlcentos
[root@iZwz9hy7gff0kp0rba9p6vZ ~]# yum repolist all | grep mysql mysql-connectors-community MySQL Connectors Community enabled: 42 mysql-connectors-community-source MySQL Connectors Community - S disabled mysql-tools-community MySQL Tools Community enabled: 51 mysql-tools-community-source MySQL Tools Community - Source disabled mysql55-community MySQL 5.5 Community Server disabled mysql55-community-source MySQL 5.5 Community Server - S disabled mysql56-community MySQL 5.6 Community Server enabled: 396 mysql56-community-source MySQL 5.6 Community Server - S disabled mysql57-community-dmr MySQL 5.7 Community Server Dev disabled mysql57-community-dmr-source MySQL 5.7 Community Server Dev disabled
安装MYSQL数据库
# yum install mysql-community-server -y安全
[root@iZwz9hy7gff0kp0rba9p6vZ ~]# yum install mysql-community-server -y Loaded plugins: fastestmirror Setting up Install Process Loading mirror speeds from cached hostfile Resolving Dependencies --> Running transaction check ---> Package mysql-community-server.x86_64 0:5.6.38-2.el6 will be installed --> Processing Dependency: mysql-community-common(x86-64) = 5.6.38-2.el6 for package: mysql-community-server-5.6.38-2.el6.x86_64 --> Processing Dependency: mysql-community-client(x86-64) >= 5.6.10 for package: mysql-community-server-5.6.38-2.el6.x86_64 --> Processing Dependency: perl(DBI) for package: mysql-community-server-5.6.38-2.el6.x86_64 --> Running transaction check ---> Package mysql-community-client.x86_64 0:5.6.38-2.el6 will be installed --> Processing Dependency: mysql-community-libs(x86-64) >= 5.6.10 for package: mysql-community-client-5.6.38-2.el6.x86_64 ---> Package mysql-community-common.x86_64 0:5.6.38-2.el6 will be installed ---> Package perl-DBI.x86_64 0:1.609-4.el6 will be installed --> Running transaction check ---> Package mysql-community-libs.x86_64 0:5.6.38-2.el6 will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================================================================================================================================================= Package Arch Version Repository Size ================================================================================================================================================================================================================= Installing: mysql-community-server x86_64 5.6.38-2.el6 mysql56-community 55 M Installing for dependencies: mysql-community-client x86_64 5.6.38-2.el6 mysql56-community 18 M mysql-community-common x86_64 5.6.38-2.el6 mysql56-community 308 k mysql-community-libs x86_64 5.6.38-2.el6 mysql56-community 1.9 M perl-DBI x86_64 1.609-4.el6 base 705 k Transaction Summary ================================================================================================================================================================================================================= Install 5 Package(s) Total download size: 76 M Installed size: 338 M Downloading Packages: (1/5): mysql-community-client-5.6.38-2.el6.x86_64.rpm | 18 MB 00:02 (2/5): mysql-community-common-5.6.38-2.el6.x86_64.rpm | 308 kB 00:00 (3/5): mysql-community-libs-5.6.38-2.el6.x86_64.rpm | 1.9 MB 00:00 (4/5): mysql-community-server-5.6.38-2.el6.x86_64.rpm | 55 MB 00:05 (5/5): perl-DBI-1.609-4.el6.x86_64.rpm | 705 kB 00:00 ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Total 8.9 MB/s | 76 MB 00:08 warning: rpmts_HdrFromFdno: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY Retrieving key from file:/etc/pki/rpm-gpg/RPM-GPG-KEY-mysql Importing GPG key 0x5072E1F5: Userid : MySQL Release Engineering <mysql-build@oss.oracle.com> Package: mysql-community-release-el6-5.noarch (installed) From : file:/etc/pki/rpm-gpg/RPM-GPG-KEY-mysql Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Warning: RPMDB altered outside of yum. Installing : mysql-community-common-5.6.38-2.el6.x86_64 1/5 Installing : mysql-community-libs-5.6.38-2.el6.x86_64 2/5 Installing : mysql-community-client-5.6.38-2.el6.x86_64 3/5 Installing : perl-DBI-1.609-4.el6.x86_64 4/5 Installing : mysql-community-server-5.6.38-2.el6.x86_64 5/5 Verifying : mysql-community-client-5.6.38-2.el6.x86_64 1/5 Verifying : mysql-community-libs-5.6.38-2.el6.x86_64 2/5 Verifying : mysql-community-common-5.6.38-2.el6.x86_64 3/5 Verifying : mysql-community-server-5.6.38-2.el6.x86_64 4/5 Verifying : perl-DBI-1.609-4.el6.x86_64 5/5 Installed: mysql-community-server.x86_64 0:5.6.38-2.el6 Dependency Installed: mysql-community-client.x86_64 0:5.6.38-2.el6 mysql-community-common.x86_64 0:5.6.38-2.el6 mysql-community-libs.x86_64 0:5.6.38-2.el6 perl-DBI.x86_64 0:1.609-4.el6 Complete!
设置为开机启动(二、三、4都是on表明开机自动启动)
# chkconfig --list | grep mysqld服务器
[root@iZwz9hy7gff0kp0rba9p6vZ ~]# chkconfig --list | grep mysqld mysqld 0:off 1:off 2:off 3:on 4:on 5:on 6:off
# chkconfig mysqld onoracle
[root@iZwz9hy7gff0kp0rba9p6vZ ~]# chkconfig mysqld on
启动mysql
# service mysqld startapp
[root@iZwz9hy7gff0kp0rba9p6vZ ~]# service mysqld start Initializing MySQL database: 2017-11-04 15:00:04 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2017-11-04 15:00:04 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap. 2017-11-04 15:00:04 0 [Note] /usr/sbin/mysqld (mysqld 5.6.38) starting as process 2021 ... 2017-11-04 15:00:04 2021 [Note] InnoDB: Using atomics to ref count buffer pool pages 2017-11-04 15:00:04 2021 [Note] InnoDB: The InnoDB memory heap is disabled 2017-11-04 15:00:04 2021 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins 2017-11-04 15:00:04 2021 [Note] InnoDB: Memory barrier is not used 2017-11-04 15:00:04 2021 [Note] InnoDB: Compressed tables use zlib 1.2.3 2017-11-04 15:00:04 2021 [Note] InnoDB: Using Linux native AIO 2017-11-04 15:00:04 2021 [Note] InnoDB: Using CPU crc32 instructions 2017-11-04 15:00:04 2021 [Note] InnoDB: Initializing buffer pool, size = 128.0M 2017-11-04 15:00:04 2021 [Note] InnoDB: Completed initialization of buffer pool 2017-11-04 15:00:04 2021 [Note] InnoDB: The first specified data file ./ibdata1 did not exist: a new database to be created! 2017-11-04 15:00:04 2021 [Note] InnoDB: Setting file ./ibdata1 size to 12 MB 2017-11-04 15:00:04 2021 [Note] InnoDB: Database physically writes the file full: wait... 2017-11-04 15:00:04 2021 [Note] InnoDB: Setting log file ./ib_logfile101 size to 48 MB 2017-11-04 15:00:05 2021 [Note] InnoDB: Setting log file ./ib_logfile1 size to 48 MB 2017-11-04 15:00:06 2021 [Note] InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0 2017-11-04 15:00:06 2021 [Warning] InnoDB: New log files created, LSN=45781 2017-11-04 15:00:06 2021 [Note] InnoDB: Doublewrite buffer not found: creating new 2017-11-04 15:00:06 2021 [Note] InnoDB: Doublewrite buffer created 2017-11-04 15:00:06 2021 [Note] InnoDB: 128 rollback segment(s) are active. 2017-11-04 15:00:06 2021 [Warning] InnoDB: Creating foreign key constraint system tables. 2017-11-04 15:00:06 2021 [Note] InnoDB: Foreign key constraint system tables created 2017-11-04 15:00:06 2021 [Note] InnoDB: Creating tablespace and datafile system tables. 2017-11-04 15:00:06 2021 [Note] InnoDB: Tablespace and datafile system tables created. 2017-11-04 15:00:06 2021 [Note] InnoDB: Waiting for purge to start 2017-11-04 15:00:06 2021 [Note] InnoDB: 5.6.38 started; log sequence number 0 2017-11-04 15:00:07 2021 [Note] Binlog end 2017-11-04 15:00:07 2021 [Note] InnoDB: FTS optimize thread exiting. 2017-11-04 15:00:07 2021 [Note] InnoDB: Starting shutdown... 2017-11-04 15:00:08 2021 [Note] InnoDB: Shutdown completed; log sequence number 1625977 2017-11-04 15:00:08 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2017-11-04 15:00:08 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap. 2017-11-04 15:00:08 0 [Note] /usr/sbin/mysqld (mysqld 5.6.38) starting as process 2043 ... 2017-11-04 15:00:08 2043 [Note] InnoDB: Using atomics to ref count buffer pool pages 2017-11-04 15:00:08 2043 [Note] InnoDB: The InnoDB memory heap is disabled 2017-11-04 15:00:08 2043 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins 2017-11-04 15:00:08 2043 [Note] InnoDB: Memory barrier is not used 2017-11-04 15:00:08 2043 [Note] InnoDB: Compressed tables use zlib 1.2.3 2017-11-04 15:00:08 2043 [Note] InnoDB: Using Linux native AIO 2017-11-04 15:00:08 2043 [Note] InnoDB: Using CPU crc32 instructions 2017-11-04 15:00:08 2043 [Note] InnoDB: Initializing buffer pool, size = 128.0M 2017-11-04 15:00:08 2043 [Note] InnoDB: Completed initialization of buffer pool 2017-11-04 15:00:08 2043 [Note] InnoDB: Highest supported file format is Barracuda. 2017-11-04 15:00:08 2043 [Note] InnoDB: 128 rollback segment(s) are active. 2017-11-04 15:00:08 2043 [Note] InnoDB: Waiting for purge to start 2017-11-04 15:00:08 2043 [Note] InnoDB: 5.6.38 started; log sequence number 1625977 2017-11-04 15:00:08 2043 [Note] Binlog end 2017-11-04 15:00:08 2043 [Note] InnoDB: FTS optimize thread exiting. 2017-11-04 15:00:08 2043 [Note] InnoDB: Starting shutdown... 2017-11-04 15:00:10 2043 [Note] InnoDB: Shutdown completed; log sequence number 1625987 PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER ! To do so, start the server, then issue the following commands: /usr/bin/mysqladmin -u root password 'new-password' /usr/bin/mysqladmin -u root -h iZwz9hy7gff0kp0rba9p6vZ password 'new-password' Alternatively you can run: /usr/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. 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 Note: new default config file not created. Please make sure your config file is current 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 [ OK ] Starting mysqld: [ OK ]
设置root密码
# mysql_secure_installation
安装完mysql-server 会提示能够运行mysql_secure_installation。运行mysql_secure_installation会执行几个设置:
a)为root用户设置密码
b)删除匿名帐号
c)取消root用户远程登陆
d)删除test库和对test库的访问权限
e)刷新受权表使修改生效
经过这几项的设置可以提升mysql库的安全。建议生产环境中mysql安装这完成后必定要运行一次mysql_secure_installation,详细步骤请参看下面的命令:
[root@iZwz9hy7gff0kp0rba9p6vZ ~]# mysql_secure_installation NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY! In order to log into MySQL to secure it, we'll need the current password for the root user. If you've just installed MySQL, and you haven't set the root password yet, the password will be blank, so you should just press enter here. Enter current password for root (enter for none): ##<–初次运行直接回车 OK, successfully used password, moving on... Setting the root password ensures that nobody can log into the MySQL root user without the proper authorisation. Set root password? [Y/n] y ##<– 是否设置root用户密码,输入y并回车或直接回车 New password: ##<– 设置root用户的密码 Re-enter new password: ##<– 再输入一次你设置的密码 Password updated successfully! Reloading privilege tables.. ... Success! By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment. Remove anonymous users? [Y/n] y ##<– 是否删除匿名用户,生产环境建议删除,因此直接回车 ... Success! Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network. Disallow root login remotely? [Y/n] n ##<–是否禁止root远程登陆,根据本身的需求选择Y/n并回车,建议禁止 ... skipping. By default, MySQL comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment. Remove test database and access to it? [Y/n] n ##<– 是否删除test数据库,直接回车 ... skipping. Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? [Y/n] ##<– 是否从新加载权限表,直接回车 ... Success! All done! If you've completed all of the above steps, your MySQL installation should now be secure. Thanks for using MySQL! Cleaning up...
登录root帐号
# mysql -uroot -p
[root@iZwz9hy7gff0kp0rba9p6vZ ~]# mysql -uroot -p Enter password: ##输入上一步设定的密码 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 23 Server version: 5.6.38 MySQL Community Server (GPL) Copyright (c) 2000, 2017, 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>
创建远程root用户
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '你设置的密码' WITH GRANT OPTION;
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'Lv216816' WITH GRANT OPTION; Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
经过以上步骤就完成了在CentOS上安装Mysql,本人是使用阿里云的服务器上安装的,若是还想经过外部访问,还必须在阿里云上开通3306的端口才能够。若是是本身设备必须在防火墙上配置上3306端口的访问才能够。
启动:# service mysqld start
[root@iZwz9hy7gff0kp0rba9p6vZ ~]# service mysqld start Starting mysqld: [ OK ]
关闭:# service mysqld stop
[root@iZwz9hy7gff0kp0rba9p6vZ ~]# service mysqld stop Stopping mysqld: [ OK ]
查看进程:ps -ef | grep mysqld
若是有mysqld_safe和mysqld两个进程,说明MySQL服务当前在启动状态;
[root@iZwz9hy7gff0kp0rba9p6vZ ~]# ps -ef | grep mysqld root 2604 1 0 22:45 pts/0 00:00:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --socket=/var/lib/mysql/mysql.sock --pid-file=/var/run/mysqld/mysqld.pid --basedir=/usr --user=mysql mysql 2808 2604 0 22:45 pts/0 00:00:00 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock root 2839 1846 0 22:50 pts/0 00:00:00 grep mysqld