Linux安装mysql

我这里是安装在/usr/local/下面

1、新建一个mysql文件夹

mkdir mysql

2、将要解压的mysql复制到刚才建立的文件夹中

cp ./mysql-5.5.27-linux2.6-x86_64.tar.gz /usr/local/mysql/

3、进入刚才建立的文件夹中

cd /usr/local/mysql/

4、将文件附有执行权限

chmod 777 ./mysql-5.5.27-linux2.6-x86_64.tar.gz 

5、解压文件

tar -zxvf mysql-5.5.27-linux2.6-x86_64.tar.gz

6、将解压出来的文件移动到mysql下面
mv ./mysql-5.5.27-linux2.6-x86_64/* ./


7、删除mysql的压缩文件,与文件夹

rm -rf mysql-*


8、建立mysql用户

/usr/sbin/useradd mysql -s /sbin/nologin


9、建立放置数据库与日志的文件夹

mkdir -p /home/mysql/3306/data/
mkdir -p /home/mysql/3306/binlog/
mkdir -p /home/mysql/3306/relaylog/
chown -R mysql:mysql /home/mysql/


10、安装mysql

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

11、建立mysql配置文件

vim /home/mysql/3306/my.cnf

[client]
character-set-server = utf8
port    = 3306
socket  = /tmp/mysql.sock   --注意,如果要配置多个端口,这里需要更改

[mysqld]
event_scheduler = 1
character-set-server = utf8
replicate-ignore-db = mysql
replicate-ignore-db = information_schema
#replicate-ignore-table = yccms.YcLog
user    = mysql
port    = 3306
socket  = /tmp/mysql.sock   --同上
basedir = /usr/local/mysql
datadir = /home/mysql/3306/data
log-error = /home/mysql/3306/mysql_error.log
pid-file = /home/mysql/3306/mysql.pid
open_files_limit    = 10240
back_log = 600
max_connections = 5000
max_connect_errors = 6000
table_cache = 614
external-locking = FALSE
max_allowed_packet = 32M
sort_buffer_size = 1M
join_buffer_size = 1M
thread_cache_size = 300
#thread_concurrency = 8
query_cache_size = 512M
query_cache_limit = 2M
query_cache_min_res_unit = 2k
default-storage-engine = MyISAM
thread_stack = 192K
transaction_isolation = READ-COMMITTED
tmp_table_size = 246M
max_heap_table_size = 246M
long_query_time = 3
log-slave-updates
#log-bin = /home/mysql/3306/binlog/binlog
binlog_cache_size = 4M
binlog_format = MIXED
binlog-ignore-db = mysql
binlog-ignore-db = information_schema
max_binlog_cache_size = 8M
max_binlog_size = 1G
relay-log-index = /home/mysql/3306/relaylog/relaylog
relay-log-info-file = /home/mysql/3306/relaylog/relaylog
relay-log = /home/mysql/3306/relaylog/relaylog
expire_logs_days = 30
key_buffer_size = 256M
read_buffer_size = 1M
read_rnd_buffer_size = 16M
bulk_insert_buffer_size = 64M
myisam_sort_buffer_size = 128M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
myisam_recover


interactive_timeout = 120
wait_timeout = 1814400


skip-name-resolve
#master-connect-retry = 10
slave-skip-errors = 1032,1062,126,1114,1146,1048,1396


server-id = 250


innodb_additional_mem_pool_size = 16M
innodb_buffer_pool_size = 512M
innodb_data_file_path = ibdata1:256M:autoextend
innodb_file_io_threads = 4
innodb_thread_concurrency = 8
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 16M
innodb_log_file_size = 128M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
innodb_file_per_table = 1


[mysqldump]
quick
max_allowed_packet = 32M

12、建立mysql启动文件

vim /home/mysql/3306/mysql

#!/bin/sh

mysql_port=3306
mysql_username="admin"
mysql_password="12345678"

function_start_mysql()
{
    printf "Starting MySQL...\n"
    /bin/sh /usr/local/mysql/bin/mysqld_safe --defaults-file=/home/mysql/${mysql_port}/my.cnf 2>&1 > /dev/null &
}

function_stop_mysql()
{
    printf "Stoping MySQL...\n"
    /usr/local/mysql/bin/mysqladmin -u ${mysql_username} -p${mysql_password} -S /tmp/mysql.sock shutdown
}

function_restart_mysql()
{
    printf "Restarting MySQL...\n"
    function_stop_mysql
    sleep 5
    function_start_mysql
}

function_kill_mysql()
{
    kill -9 $(ps -ef | grep 'bin/mysqld_safe' | grep ${mysql_port} | awk '{printf $2}')
    kill -9 $(ps -ef | grep 'libexec/mysqld' | grep ${mysql_port} | awk '{printf $2}')
}

if [ "$1" = "start" ]; then
    function_start_mysql
elif [ "$1" = "stop" ]; then
    function_stop_mysql
elif [ "$1" = "restart" ]; then
function_restart_mysql
elif [ "$1" = "kill" ]; then
function_kill_mysql
else
    printf "Usage: /home/mysql/${mysql_port}/mysql {start|stop|restart|kill}\n"
fi


13、赋予shell脚本可执行权限

chmod +x /home/mysql/3306/mysql

14、启动mysql

/home/mysql/3306/mysql start

15、通过命令行登录MySql服务器(提示输入密码时直接回车)
/usr/local/mysql/bin/mysql -u root -p -S /tmp/mysql.sock

16、输入以下SQL语句,创建一个具有root权限的用户(admin)与密码(123456)

GRANT ALL PRIVILEGES ON *.* TO 'admin'@'localhost' IDENTIFIED BY '12345678';
GRANT ALL PRIVILEGES ON *.* TO 'admin'@'127.0.0.1' IDENTIFIED BY '12345678';

你可能感兴趣的:(mysql,linux,数据库,centos)