一、下载mysql
1、官网下载:MySQL :: Download MySQL Community Server (Archived Versions)
2、卸载系统自带的Mariadb
[root@localhost ~]# rpm -qa|grep mariadb // 查询出来已安装的mariadb
[root@localhost ~]# rpm -e --nodeps 文件名 // 卸载mariadb,文件名为上述命令查询出来的文件
3、删除etc目录下的my.cnf
[root@localhost ~]# rm /etc/my.cnf
4、执行以下命令来创建mysql用户组
[root@localhost ~]# groupadd mysql
5、执行以下命令来创建一个用户名为mysql的用户并加入mysql用户组
[root@localhost ~]# useradd -g mysql mysql
6、将下载的压缩包放到 /usr/local/ 目录下(通过mv 要移动的文件 /usr/local/)
7、解压安装包
[root@localhost local]# tar -xvf mysql-5.6.51-linux-glibc2.5-x86_64.tar
注:如果压缩包为:mysql-5.6.51-linux-glibc2.5-x86_64.tar.gz,
则解压命令为: tar -zxvf mysql-5.6.51-linux-glibc2.5-x86_64.tar.gz
8、将解压好的文件夹重命名为mysql
[root@localhost local]# mv 解压出来的文件夹名 mysql
9、在 etc 下新建配置文件my.cnf,并在该文件中添加一下代码:
当然,也有简单方式:直接copy一份my.cnf文件到/etc下,然后再修改即可。
e g:copy一份/usr/local/mysql/support-files/下的my-default.cnf文件到/etc下
命令为:[root@localhost support-files]# cp my-default.cnf /etc/my.cnf
然后,配置/etc目录下的my.cnf文件
[root@localhost support-files]# vim /etc/my.cnf
通过vim编辑器编辑my.cnf代码如下:
[root@localhost env]# cat /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[mysqld]
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
########basic settings########
#basedir = /usr
datadir = /data/mysql
port = 3306
# server_id = .....
socket = /data/mysql/mysql.sock
log-error = /data/mysql/error.log
pid-file = /data/mysql/mysql.pid
user = mysql
tmpdir = /tmp
character_set_server = utf8
default-storage-engine = INNODB
lower_case_table_name = 1
#log-queries-not-using-indexes
skip-host-cache
skip-name-resolve
# skip-external-locking
query_cache_size = 128M
query_cache_type = 1
query_cache_limit = 4M
query_cache_min_res_unit = 4K
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
#performance_schema = 0
#explicit_defaults_for_timestamp
#lower_case_table_names = 1
interactive_timeout = 28800
wait_timeout = 28800
default-time-zone = '+8:00'
back_log = 500
max_connections = 3000
max_connect_errors = 6000
open_files_limit = 65535
table_open_cache = 4096
binlog_cache_size = 1M
max_heap_table_size = 256M
tmp_table_size = 256M
max_allowed_packet = 32M
########buffer settings########
read_buffer_size = 16M
read_rnd_buffer_size = 64M
sort_buffer_size = 32M
join_buffer_size = 16M
key_buffer_size = 128M
thread_cache_size = 100
bulk_insert_buffer_size = 16M
########log settings########
#log_bin = mysql-bin
#binlog_format = mixed
#expire_logs_days = 30
slow_query_log=1
slow_query_log_file = /data/mysql/mysql-slow.log
long_query_time = 1
min_examined_row_limit = 100
log_queries_not_using_indexes = 1
log_throttle_queries_not_using_indexes = 10
log-slow-admin-statements = 1
log_slow_slave_statements = 0
#log_timestamps=system
log_output = FILE
########innodb settings########
innodb_file_per_table = 1
innodb_open_files = 2000
innodb_buffer_pool_size = 4G
innodb_write_io_threads = 4
innodb_read_io_threads = 4
innodb_thread_concurrency = 0
innodb_purge_threads = 1
innodb_flush_log_at_trx_commit = 1
innodb_log_buffer_size = 8M
innodb_log_file_size = 128M
innodb_log_files_in_group = 2
innodb_max_dirty_pages_pct = 80
innodb_lock_wait_timeout = 50
innodb_flush_method = O_DIRECT
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
[mysqldump]
quick
max_allowed_packet = 16M
[client]
default-character-set=utf8
socket = /data/mysql/mysql.sock
10、进入安装mysql软件目录
[root@localhost ~]# cd /usr/local/mysql
[root@localhost mysql]# chown -R mysql:mysql ./ 修改当前目录拥有着为mysql用户
[root@localhost mysql]# ./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/data/mysql安装数据库
注:若执行以上最后一个命令出现以下问题:
FATAL ERROR: please install the following Perl modules before executing
./scripts/mysql_install_db:Data::Dumper
解决方法 :安装autoconf库
命令: yum -y install autoconf //此包安装时会安装Data:Dumper模块
安装完成重新执行上述最后一个命令
./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/data/mysql
[root@localhost mysql]# chown -R mysql:mysql data 修改当前data目录的拥有者为mysql用户
二、配置MySQL
1、授予my.cnf最大权限
[root@localhost ~]# chmod 777 /etc/my.cnf
设置开机自启动服务控制脚本:
2、复制启动脚本到资源目录
[root@localhost mysql]# cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld
3、增加mysqld服务控制脚本执行权限
[root@localhost mysql]# chmod +x /etc/rc.d/init.d/mysqld
4、将mysqld服务加入到系统服务
[root@localhost mysql]# chkconfig --add mysqld
5、检查mysqld服务是否已经生效
[root@localhost mysql]# chkconfig --list mysqld
命令输出类似下面的结果:
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
表明mysqld服务已经生效,在2、3、4、5运行级别随系统启动而自动启动,以后可以使用service命令控制mysql的启动和停止
命令为:service mysqld start和service mysqld stop
6、启动mysqld
[root@localhost mysql]# service mysqld start
7、将mysql的bin目录加入PATH环境变量,编辑 ~/.bash_profile文件
[root@localhost mysql]# vim ~/.bash_profile
在文件最后添加如下信息:
export PATH=$PATH:/usr/local/mysql/bin
然后按ESC键
继续 shift键加冒号打出来=> :
接下来输入wq回车即可
执行下面的命令是修改的内容立即生效:
[root@localhost mysql]# source ~/.bash_profile
8、以root账户登录mysql,默认是没有密码的
[root@localhost mysql]# mysql -uroot -p
要输入密码的时候直接回车即可。
9、设置root账户密码为root(也可以修改成你要的密码)
mysql>use mysql;
mysql>update user set password=password('root') where user='root' and host='localhost';
mysql>flush privileges;
10、设置远程主机登录,注意下面的your username 和 your password改成你需要设置的用户和密码
mysql>GRANT ALL PRIVILEGES ON *.* TO 'your username'@'%' IDENTIFIED BY 'your password' WITH
GRANT OPTION;