先下载好mysql的linux安装包,从官网下,我下载的是5.6社区版, 下载后传到ubuntu上去。
包放在~/download目录下,全部安装命令如下:
1、解压tar.gz
tar –xzf mysql-5.6.26-linux-glibc2.5-x86_64.tar.gz
2、重命名解压的文件夹
mv mysql-5.6.26-linux-glibc2.5-x86_64 mysql
3、将mysql文件夹移动到/usr/local目录下
sudo mv ~/download/mysql /usr/local
4、进入mysql目录
cd /usr/local/mysql
5、增加mysql用户及组
sudo useradd -r mysql
6、将mysql文件夹own及grp变更为mysql
sudo chown -R mysql:mysql mysql/
7、执行mysql安装脚本
sudo scripts/mysql_install_db --user=mysql
(若未安装libaio包,会有一个报错提示,安装libaio-dev后,再运行脚本即可。如果还是出错可以删除rm -rf /etc/my.cnf)
sudo apt-get install libaio-dev
8、将目录权限变更回来,仅保留data目录为mysql用户
sudo chown -R root:root mysql .
sudo chown -R mysql:mysql data
9、将mysql配置文件拷贝到etc目录(全局配置)
注意:5.6版本的默认配置文件名称由原先的my-medium变更为了my-default。
sudo cp support-files/my-default.cnf /etc/my.cnf
10、启动mysql
sudo bin/mysqld_safe --user=mysql &
11、初始化mysql root用户密码
sudo bin/mysqladmin -u root -p '用户自定义密码';
#ps -A|grep mysql
显示类似:
1829 ? 00:00:00 mysqld_safe
1876 ? 00:00:31 mysqld
2.#kill -9 1829
3.#kill -9 1876
12、复制mysql.server脚本到/etc/init.d(初始化服务,有些人喜欢改成mysql,在这里改就可以)
sudo cp support-files/mysql.server /etc/init.d/mysql.server
14、查看mysql运行状态
sudo service mysql.server status
如果运行正常,会显示 MySQL running。
如果显示 not running,应该是前面没有启动服务,可直接用service mysql.server start启动
sudo service mysql.server [status|start|stop]
15、让mysql开机启动[defaults],取消开机启动[remove]
sudo update-rc.d -f mysql.server defaults [remove]
16、将mysql/bin/mysql命令加入到用户命令中,或将mysql/bin目录加入path
加入用户命令:
sudo ln -s /usr/local/mysql/bin/mysql /usr/local/bin/mysql
加入环境变量:
export PATH=$PATH:/usr/local/mysql/bin
17、允许root用户远程登录
1>进入mysql: mysql –u root –p
2>改变数据库: use mysql;
3>从任意主机登录: grant all privileges on *.* to root@"%" identified by "密码文字" with grant option;
4>从指定主机登录: grant all privileges on *.* to root@"192.168.1.101" identified by "passw0rd" with grant option;
5>授权生效: flush privileges;
6>查看host为%授权是否添加: select * from user;
7>查看数据库字符集: show variables like 'character%';
启动完mysql后,我们接着可以测试一下,使用“mysql”命令来进入mysql数据库的控制台
$mysql -u root
在这里之所以用-u root是因为我现在是一般用户(firehare),如果不加-u root的话,mysql会以为是firehare在登录。注意,我在这里没有进入根用户模式,因为没必要。一般来说,对mysql中的数据库进行操作,根本没必要进入根用户模式,只有在设置时才有这种可能。
进入mysql之后,最要紧的就是要设置Mysql中的root用户密码了,否则,Mysql服务无安全可言了。
mysql> GRANT ALL PRIVILEGES ON *.* TO root@localhost IDENTIFIED BY "123456";
如果需要使用root从其他机器远程访问可以使用
mysql> GRANT ALL PRIVILEGES ON *.* TO root@“%” IDENTIFIED BY "123456";
注意,我这儿用的是123456做为root用户的密码,但是该密码是不安全的,请大家最好使用大小写字母与数字混合的密码,且不少于8位。
配置文件参考:
# 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.
[client]
port = 3306
default-character-set=utf8
# Here is entries for some specific programs
# The following values assume you have at least 32M ram
[mysqld]
character_set_server=utf8
lower_case_table_names=1
init_connect='SET NAMES utf8'
lower_case_table_names=1
max_connections=3000
max_allowed_packet = 32M
thread_cache_size = 16
thread_concurrency = 8
query_cache_size = 128M
# 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
# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = .....
# port = .....
# server_id = .....
# socket = .....
# 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 = 16M
sort_buffer_size = 16M
# read_rnd_buffer_size = 2M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
[mysqldump]
quick
quote-names
max_allowed_packet = 32M
[mysql]
no-auto-rehash
参考文章: http://blog.csdn.net/njchenyi/article/details/17615391