mysql:多配置文件安装新端口实例

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011665746/article/details/52193048

1.查找 my.cnf 配置文件,新建文件 my3309.cnf 文件并修改。
/*
[root@localhost home]# mysql --help | grep my.cnf
                      order of preference, my.cnf, $MYSQL_TCP_PORT,
/etc/my.cnf /etc/mysql/my.cnf /usr/etc/my.cnf ~/.my.cnf
*/

[root@localhost home]#cp /etc/my.cnf /etc/my3309.cnf    -->修改相应参数  可以不用  [mysqld_safe]


/*
[root@localhost home]# cat /etc/my3309.cnf 
[mysqld]
datadir=/var/lib/mysql3309
socket=/var/lib/mysql3309/mysql3309.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0


lower_case_table_names = 1
character_set_server = utf8
collation-server=utf8_bin
transaction_isolation = READ-COMMITTED
log_bin=mysql-bin
binlog_format=ROW
log_slave_updates=1
relay-log=relay-log




port = 3309
slow_query_log=on
long_query_time=1
slow_query_log_file=mysqld-slow.log


sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES


max_connections=1000
wait_timeout=7200
interactive_timeout=7200


##可参考配置
skip-name-resolve


server_id = 15
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
binlog-ignore-db=mysql
binlog-ignore-db=test


#query_cache_type=1
#query_cache_size=104857600
#rpl_semi_sync_master_enabled =1
#rpl_semi_sync_slave_enabled =1
*/



2.创建 datadir 目录 并 授权
[root@localhost home]#mkdir /var/lib/mysql3309
[root@localhost home]#chown -R mysql:mysql  /var/lib/mysql3309      --  -R 参数:该文件夹内的所有文件/文件夹都改为指定的权限.


[root@localhost home]# ll /var/lib
总用量 100
drwxr-xr-x.  2 root    root    4096 8月  11 2015 alternatives
drwxr-xr-x  55 mysql   mysql   4096 8月   8 10:36 mysql
drwxr-xr-x   5 mysql   mysql   4096 8月   8 15:55 mysql3309


3.mysql_install_db 初始化数据库
/*
[root@localhost home]#whereis mysql_install_db     -- > 查找出 mysql_install_db 的路径
[root@localhost home]#which mysql_install_db


[root@localhost init.d]# whereis mysql3309
mysql3309: /usr/lib/mysql3309)
[root@localhost home]# ps -ef | grep mysql      -- > 得到 basedir 数据库安装路径
root      1299     1  0 Jun03 ?        00:00:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --pid-file=/var/lib/mysql/localhost.localdomain.pid
mysql     1753  1299 46 Jun03 ?        30-14:57:14 /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/lib/mysql/localhost.localdomain.pid --socket=/var/lib/mysql/mysql.sock --port=3306
*/

[root@localhost home]# mysql_install_db --basedir=/usr --datadir=/var/lib/mysql3309 --use=mysql


4.创建 mysql3309服务,设置开机自启动,开启mysql3309服务   修改相应的参数
[root@localhost init.d]# vim /etc/init.d/mysql3309


#!/bin/bash
#
# chkconfig: 2345 64 36
# description: MYSQL instance 2.
mysql_port=3309
mysql_username="root"
mysql_password="baidu_tenxu"
function_start_mysql()
{
printf "Starting MySQL...\n"
/bin/sh /usr/bin/mysqld_safe --defaults-file=/etc/my${mysql_port}.cnf 2>&1 > /dev/null &
}
function_stop_mysql()
{
printf "Stoping MySQL...\n"
/usr/bin/mysqladmin -u ${mysql_username} -p${mysql_password} -S /var/lib/mysql${mysql_port}/mysql3309.sock shutdown
}
function_restart_mysql()
{
printf "Restarting MySQL...\n"
function_stop_mysql
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}')
}
case $1 in
start)
function_start_mysql;;
stop)
function_stop_mysql;;
kill)
function_kill_mysql;;
restart)
function_stop_mysql
function_start_mysql;;
*)
echo "Usage: /var/lib/mysql${mysql_port}/mysqld {start|stop|restart|kill}";;
esac


[root@localhost init.d]# chkconfig --list mysql
mysql           0:关闭1:关闭2:启用3:启用4:启用5:启用6:关闭
You have new mail in /var/spool/mail/root


[root@localhost init.d]#chkconfig --add mysql3309
[root@localhost init.d]#chkconfig --level 2345 mysql3309 on       --> mysql3309 开机自启动
[root@localhost init.d]# chkconfig --list mysql3309
mysql3309       0:关闭1:关闭2:启用3:启用4:启用5:启用6:关闭


[root@localhost init.d]sh mysql3309 start      -->启动3309实例
[root@localhost init.d]ps -ef | grep mysql     -->验证3309 是否启动


5.使用root无密码登录,并修改root密码
[root@localhost init.d]# mysql -uroot  -P3309 -S /var/lib/mysql/mysql3309.sock 
mysql> show variables like 'port';     -->验证是否 3309 实例
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port          | 3309  |
+---------------+-------+
1 row in set (0.00 sec)


mysql> use mysql;
mysql> update user set password=password('baidu_tenxu') where user='root';  -- 修改root用户密码
Query OK, 0 rows affected (0.00 sec)
Rows matched: 4  Changed: 0  Warnings: 0
mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)



附一个逻辑流程:







阅读更多
换一批

没有更多推荐了,返回首页